Problem link
One of x,y is obviously one of a1,b1. For example, let's fix who is x from the first pair. Then you need to check that there exists some y such that all pairs that don't contain x, contain y. For this, you can remember in the array for each value how many pairs that don't contain x contain this value, and then you need to check that in the array there exists some value that is equal to the number of pairs left.
A solution in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma GCC optimize("Ofast,no-stack-protector") | |
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native" | |
#include<bits/stdc++.h> | |
using namespace std; | |
/// Typedef | |
typedef long long ll; | |
#define sc1(a) scanf("%lld",&a) | |
#define sc2(a,b) scanf("%lld %lld",&a,&b) | |
#define pf1(a) printf("%lld\n", a) | |
#define pf2(a,b) printf("%lld %lld\n",a,b) | |
#define size1 300005 | |
ll first[size1], sec[size1], n, m; | |
bool calculate(ll x, ll y){ | |
for(ll i = 0; i < m; i++){ | |
if(first[i] == x || first[i] == y || sec[i] == x || sec[i] == y) continue; | |
else return false; | |
} | |
return true; | |
} | |
int main() { | |
//seive(); | |
//preCal(); | |
ll tc, num, t = 1, choose; | |
//freopen("/opt/Coding/clion code/input.txt", "r", stdin); | |
//freopen("/opt/Coding/clion code/output.txt", "w", stdout); | |
sc2(n, m); | |
for(ll i = 0; i < m; i++){ | |
sc2(first[i], sec[i]); | |
} | |
for(ll i = 1; i < m; i++){ | |
if(first[i] == first[0] || first[i] == sec[0] || sec[i] == first[0] ||sec[i] == sec[0]){ | |
continue; | |
} | |
if(calculate(first[i], first[0])) return cout << "YES" << endl,0; | |
if(calculate(first[i], sec[0])) return cout << "YES" << endl,0; | |
if(calculate(sec[i], first[0])) return cout << "YES" << endl,0; | |
if(calculate(sec[i], sec[0])) return cout << "YES" << endl,0; | |
return cout << "NO" << endl, 0; | |
} | |
cout << "YES" << endl; | |
return 0; | |
} |
0 Comments
If you have any doubts, Please let me know