Pairs CodeForces - 1169B

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值