time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Copy
4 6 1 2 1 3 1 4 2 3 2 4 3 4
output
Copy
NO
input
Copy
5 4 1 2 2 3 3 4 4 5
output
Copy
YES
input
Copy
300000 5 1 2 1 2 1 2 1 2 1 2
output
Copy
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.
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=3000001;
int a1[maxn],b1[maxn];
int a,b,x,y,x1,x2;
int main()
{
int y1,y2;
x1=y1=1;
x2=y2=0;
memset(a1,0,sizeof(a1));
memset(b1,0,sizeof(b1));
int n,m;
scanf("%d%d",&n,&m);
scanf("%d%d",&x,&y);
for(int i=1; i<m; i++)
{
scanf("%d%d",&a,&b);
if(x==a||x==b)x1++;
else x2=max(x2,max(++a1[a],++a1[b]));
if(y==a||y==b)y1++;
else y2=max(y2,max(++b1[a],++b1[b]));
}
if(x1+x2==m||y1+y2==m)puts("YES");
else puts("NO");
return 0;
}