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 4Output
NOInput
5 4 1 2 2 3 3 4 4 5Output
YESInput
300000 5 1 2 1 2 1 2 1 2 1 2Output
YESNote
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組數(shù),每組兩個(gè)。問(wèn)你是否能找到兩個(gè)數(shù),使得這m組數(shù)中每組至少含有這兩個(gè)數(shù)中的一個(gè)。
思路:如果能找到這樣的兩個(gè)數(shù)<x,y>,那第一組<t1,t2>中必然含有其中一個(gè)數(shù)x。這個(gè)數(shù)有可能是t1,也有可能是t2。
遍歷尋找這m組中和第一組完全不同的一組<t3,t4>。如果能找到滿足題意的兩個(gè)數(shù),那這一組中必然含有另外那個(gè)數(shù)y。如果找不到,直接輸出YES。
至于x是<t1,t2>中的哪一個(gè),y是<t3,t4>中的哪一個(gè),現(xiàn)在還不能確定,但是我們可以遍歷尋找。現(xiàn)在可能的情況有<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;//判斷是否有與第一對(duì)完全不同的組合 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; }寫代碼的時(shí)候因?yàn)閙ain函數(shù)和check函數(shù)中的變量重了,導(dǎo)致多花了好長(zhǎng)時(shí)間,以后切記注意變量的范圍,盡量讓變量不要重復(fù)。
?
?
?
不要把無(wú)所謂的事放在心上,如果好像做得不是很好,那也不要在意,下次注意些就行了。怎樣做,下次才能做好呢?答案是做好當(dāng)下正在做的事,因?yàn)槟銦o(wú)法跳出當(dāng)下去做未來(lái)的事,順其自然才是最好的選擇,過(guò)去的事就讓它隨風(fēng)飄散吧。
總結(jié)
以上是生活随笔為你收集整理的Pairs(暴力,超详细简单)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: git 查看自己秘钥_git生成和检查秘
- 下一篇: 驱动你做一件事的动力来源是什么?