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函數中的變量重了,導致多花了好長時間,以後切記注意變量的範圍,儘量讓變量不要重複。

 

 

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章