Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- #define S(a) scanf("%d",&a)
- #define LS(a) scanf("%lld",&a)
- #define D(a) scanf("%lf",&a)
- #define LD(a) scanf("%lf",&a)
- #define FOR(i, a, b) for(int i = a;i <= b;++i)
- #define DOW(i, b, a) for(int i = b; i >= a;--i)
- #define Sort(a) sort(a.begin(),a.end())
- #define eb emplace_back
- #define fi first
- #define se second
- const ld DINF = 1e45;
- const ll INF = 1e17;
- const ll MOD = 1e9 + 7;
- const int MAXF = 500010;
- int getCommon(pair<int, int> a, pair<int, int> b) {
- if (a.first == b.first) return a.first;
- if (a.second == b.first) return a.second;
- if (a.first == b.second) return a.first;
- if (a.second == b.second) return a.second;
- return -1;
- }
- vector<int> check(vector<pair<int, int>> a, vector<pair<int, int>> b) {
- set<int> com;
- for (int i = 0; i < a.size(); ++i) {
- set<int> cme;
- for (int j = 0; j < b.size(); ++j) {
- if (a[i] != b[j]) {
- int cm = getCommon(a[i], b[j]);
- if (cm != -1) {
- cme.insert(cm);
- if (cme.size() >= 2) return {-1};
- }
- }
- }
- if (cme.size() > 0)
- com.insert(*cme.begin());
- }
- vector<int> ans(com.begin(), com.end());
- return ans;
- }
- int main() {
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt","w",stdout);
- int t;
- //S(t);
- t = 1;
- for (int tc = 1; tc <= t; ++tc) {
- int n,m;
- S(n),S(m);
- vector<pair<int, int>> a(n),b(m);
- for (int i = 0; i < n ; ++i) {
- S(a[i].first);
- S(a[i].second);
- int x = max(a[i].first,a[i].second);
- int y = min(a[i].first,a[i].second);
- a[i] = {x,y};
- }
- for (int j = 0; j < m ; ++j) {
- S(b[j].first);
- S(b[j].second);
- int x = max(b[j].first,b[j].second);
- int y = min(b[j].first,b[j].second);
- b[j] = {x,y};
- }
- vector<int> res1 = check(a,b);
- vector<int> res2 = check(b,a);
- if(res1.size() == 0 || res2.size() == 0 ||res1[0] == -1 || res2[0] == -1 || res1 != res2) {
- printf("-1\n");
- }else if(res1 == res2 && res1.size() >= 2){
- printf("0\n");
- }else {
- printf("%d\n",res1[0]);
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment