日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pairs(暴力,超详细简单)

發布時間:2023/12/14 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pairs(暴力,超详细简单) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送門

Toad Ivan has?mm?pairs of integers, each integer is between?11?and?nn, inclusive. The pairs are?(a1,b1),(a2,b2),…,(am,bm)(a1,b1),(a2,b2),…,(am,bm).

He asks you to check if there exist two integers?xx?and?yy?(1≤x<y≤n1≤x<y≤n) such that in each given pair at least one integer is equal to?xx?or?yy.

Input

The first line contains two space-separated integers?nn?and?mm?(2≤n≤3000002≤n≤300000,?1≤m≤3000001≤m≤300000)?— the upper bound on the values of integers in the pairs, and the number of given pairs.

The next?mm?lines contain two integers each, the?ii-th of them contains two space-separated integers?aiai?and?bibi?(1≤ai,bi≤n,ai≠bi1≤ai,bi≤n,ai≠bi)?— the integers in the?ii-th pair.

Output

Output "YES" if there exist two integers?xx?and?yy?(1≤x<y≤n1≤x<y≤n) such that in each given pair at least one integer is equal to?xx?or?yy. Otherwise, print "NO". You can print each letter in any case (upper or lower).

Examples

Input

4 6 1 2 1 3 1 4 2 3 2 4 3 4

Output

NO

Input

5 4 1 2 2 3 3 4 4 5

Output

YES

Input

300000 5 1 2 1 2 1 2 1 2 1 2

Output

YES

Note

In the first example, you can't choose any?xx,?yy?because for each such pair you can find a given pair where both numbers are different from chosen integers.

In the second example, you can choose?x=2x=2?and?y=4y=4.

In the third example, you can choose?x=1x=1?and?y=2y=2.

題意:給你m組數,每組兩個。問你是否能找到兩個數,使得這m組數中每組至少含有這兩個數中的一個。

思路:如果能找到這樣的兩個數<x,y>,那第一組<t1,t2>中必然含有其中一個數x。這個數有可能是t1,也有可能是t2。

遍歷尋找這m組中和第一組完全不同的一組<t3,t4>。如果能找到滿足題意的兩個數,那這一組中必然含有另外那個數y。如果找不到,直接輸出YES。

至于x是<t1,t2>中的哪一個,y是<t3,t4>中的哪一個,現在還不能確定,但是我們可以遍歷尋找。現在可能的情況有<t1,t3>是<x,y>、<t1,t4>是<x,y>、<t2,t3>是<x,y>、<t2,t4>是<x,y>,<x,y>必然只有唯一的一組,所以我們把每一種情況代入檢查一下就行了,如果這四種情況中有滿足題意的,就輸出YES,否則輸出NO。

#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> #include <vector> #include <queue> #include <map> #include <string>using namespace std;typedef long long ll; const int maxn=3e5+7;int n,m; pair<int,int> p[maxn];bool check(int a,int b) {for(int i=0;i<m;i++){if(p[i].first!=a&&p[i].first!=b&&p[i].second!=a&&p[i].second!=b)return 0;}return 1; }int main() {cin>>n>>m;for(int i=0;i<m;i++) cin>>p[i].first>>p[i].second;int t1=p[0].first,t2=p[0].second,t3,t4;int flag1=0;//判斷是否有與第一對完全不同的組合 for(int i=1;i<m;i++){if(p[i].first!=t1&&p[i].first!=t2&&p[i].second!=t1&&p[i].second!=t2){flag1=1;t3=p[i].first,t4=p[i].second;break;}}if(!flag1) cout<<"YES"<<endl;else {if(check(t1,t3)||check(t1,t4)||check(t2,t3)||check(t2,t4)) cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0; }

寫代碼的時候因為main函數和check函數中的變量重了,導致多花了好長時間,以后切記注意變量的范圍,盡量讓變量不要重復。

?

?

?

不要把無所謂的事放在心上,如果好像做得不是很好,那也不要在意,下次注意些就行了。怎樣做,下次才能做好呢?答案是做好當下正在做的事,因為你無法跳出當下去做未來的事,順其自然才是最好的選擇,過去的事就讓它隨風飄散吧。

總結

以上是生活随笔為你收集整理的Pairs(暴力,超详细简单)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。