概述
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
题意:找出是否存在两个数至少有一个存在于每一对pair中。
思路:暴力肯定超时,既然每个序列都至少存在两个数中的一个数,那么合法序列中这两个数一定存在于两对完全不同的pair中。然后在将其进行判断。
#include<iostream>
using namespace std;
struct node{
int x,y;
}r[300010];
int n,m;
bool judge(int x,int y){
for(int i=0;i<m;i++){
if(r[i].x!=x&&r[i].y!=y&&r[i].x!=y&&r[i].y!=x) return false;
}
return true;
}
int main(){
cin>>n>>m;
cin>>r[0].x>>r[0].y;
int x1=r[0].x,y1=r[0].y,x2,y2;
for(int i=1;i<m;i++){
cin>>r[i].x>>r[i].y;
if(r[i].x!=x1&&r[i].y!=x1&&r[i].x!=y1&&r[i].y!=y1){
x2=r[i].x;
y2=r[i].y;
}
}
// cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
if(judge(x1,y2)||judge(x1,x2)||judge(x2,y1)||judge(y1,y2)||judge(x1,y1)||judge(x2,y2)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
最后
以上就是儒雅小馒头为你收集整理的Pairs CodeForces - 1169B的全部内容,希望文章能够帮你解决Pairs CodeForces - 1169B所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复