B. Pairs
memory limit per test256 megabytes
inputstandard input
outputstandard output
Toad Ivan has m pairs of integers, each integer is between 1 and n, inclusive. The pairs are (a1,b1),(a2,b2),…,(am,bm).
He asks you to check if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y.
Input
The first line contains two space-separated integers n and m (2≤n≤300000, 1≤m≤300000) — the upper bound on the values of integers in the pairs, and the number of given pairs.
The next m lines contain two integers each, the i-th of them contains two space-separated integers ai and bi (1≤ai,bi≤n,ai≠bi) — the integers in the i-th pair.
Output
Output “YES” if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y. 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 x, y 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=2 and y=4.
In the third example, you can choose x=1 and y=2.
题意:
找两个数,要求这两个数在输入的每组中至少有一个数是等于这两个数的其中一个的。如果存在这两个数,就输出YES,否则就输出NO
思路:
看第一组中的两个数,一定是有一个数是那两个数中的其中一个,先看第一组的第一个数z,看看下面几组,找到与这个数都不相同的两个数v1和v2,再暴力看看z与v1或z和v2两个数在输入的每组中至少有一个数是等于这两个数的其中一个,如果是就输出YES,同理,看看第一组的第二个数n,进行与上面相同的操作。如果都没有就输出NO。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<sstream>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
ll a[300020],b[300020];
ll x,y,i,j,z,v1,v2;
bool qvq(ll n,ll m){//这两个数在输入的每组中至少有一个数是等于这两个数的其中一个
for(i=0;i<y;i++){
if(n!=a[i]&&n!=b[i]&&m!=a[i]&&m!=b[i])return false;
}
return true;
}
int main(){
std::ios::sync_with_stdio(false);
while(cin>>x>>y){
mes(a,0);mes(b,0);
for(i=0;i<y;i++)cin>>a[i]>>b[i];
z=a[0];
for(i=0;i<y;i++){
if(z!=a[i]&&z!=b[i]){
v1=a[i];v2=b[i];break;
}
}//看第一组的第一个数z,看看下面几组,找到与这个数都不相同的两个数v1和v2
if(qvq(z,v1)||qvq(z,v2)){
cout<<"YES"<<endl;continue;
}//暴力看看z与v1或z和v2两个数在输入的每组中至少有一个数是等于这两个数的其中一个,如果是就输出YES.
z=b[0];
for(i=0;i<y;i++){
if(z!=a[i]&&z!=b[i]){
v1=a[i];v2=b[i];break;
}
}//看第一组的第二个数z,看看下面几组,找到与这个数都不相同的两个数v1和v2
if(qvq(z,v1)||qvq(z,v2)){
cout<<"YES"<<endl;continue;
}//暴力看看z与v1或z和v2两个数在输入的每组中至少有一个数是等于这两个数的其中一个,如果是就输出YES.
cout<<"NO"<<endl;//如果都没有就输出NO。
}
}
精简代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<sstream>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
struct node{
ll v1,v2;
}flag[4];
struct node1{
ll zz,v1,v2;
}z[2];
int main(){
std::ios::sync_with_stdio(false);
ll x,y,i,j,a,b;
while(cin>>x>>y){
for(i=0;i<4;i++)flag[i].v1=flag[i].v2=1;
cin>>z[0].zz>>z[1].zz;
for(i=1;i<y;i++){
cin>>a>>b;
if(z[0].zz!=a&&z[0].zz!=b&&flag[0].v1==1&&flag[1].v1==1){
z[0].v1=a;z[0].v2=b;flag[0].v1=flag[1].v1=0;
}//看第一组的第一个数z[0].zz,看看下面几组,找到与这个数都不相同的两个数z[0].v1和z[0].v2
if(z[0].zz!=b&&z[0].zz!=a&&z[0].v1!=b&&z[0].v1!=a&&flag[0].v1==0) flag[0].v2=0;//判断z[0].zz和z[0].v1在录入z[0].v1后是否为那两个数
if(z[0].zz!=a&&z[0].zz!=b&&z[0].v2!=b&&z[0].v2!=a&&flag[1].v1==0) flag[1].v2=0;//判断z[0].zz和z[0].v2在录入z[0].v2后是否为那两个数
if(z[1].zz!=a&&z[1].zz!=b&&flag[2].v1==1&&flag[3].v1==1){
z[1].v1=a;z[1].v2=b;flag[2].v1=flag[3].v1=0;
}//看第一组的第二个数z[1].zz,看看下面几组,找到与这个数都不相同的两个数z[1].v1和z[1].v2
if(z[1].zz!=b&&z[1].zz!=a&&z[1].v1!=b&&z[1].v1!=a&&flag[2].v1==0) flag[2].v2=0;//判断z[1].zz和z[1].v1在录入z[1].v1后是否为那两个数
if(z[1].zz!=a&&z[1].zz!=b&&z[1].v2!=b&&z[1].v2!=a&&flag[3].v1==0) flag[3].v2=0;//判断z[1].zz和z[1].v2在录入z[0].v2后是否为那两个数
}
ll k=0;
for(i=0;i<4;i++)
if(flag[i].v2==1){
cout<<"YES"<<endl;k=1;break;
}//看看是否存在那两个数
if(k==0)
cout<<"NO"<<endl;
}
}