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函数中的变量重了,导致多花了好长时间,以后切记注意变量的范围,尽量让变量不要重复。

不要把无所谓的事放在心上,如果好像做得不是很好,那也不要在意,下次注意些就行了。怎样做,下次才能做好呢?答案是做好当下正在做的事,因为你无法跳出当下去做未来的事,顺其自然才是最好的选择,过去的事就让它随风飘散吧。

原创文章 99 获赞 15 访问量 7332

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/103296862
今日推荐