题目链接:http://codeforces.com/contest/994/problem/D
思路:很明显找了一遍只有一个共享数字才是第一种情况,对于第二种,因为要双方都知道,又因为双方知道自己那组的数字,所以只要跑两遍,如果两遍下来每一组中的一对对应另外一组所有对都不出现包含当前这对两个数字,那就是第二种情况,其他的都是第三种。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define pb push_back
#define lc (d<<1)
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,m,c[18];
P a[18],b[18];
int as(int x,int y,int q,int w)
{
if(x!=q&&x!=w&&y!=q&&y!=w) return 0;
if((x==q&&y==w)||(x==w&&y==q)) return 0;
if(x==q||x==w) return x;
if(y==q||y==w) return y;
return 0;
}
int main()
{
cin.tie(0);
cout.tie(0);
cin>>n>>m;
FOR(i,1,n) cin>>a[i].first>>a[i].second;
FOR(i,1,m) cin>>b[i].first>>b[i].second;
int s=0,x,y,qw=0,z;
FOR(i,1,n)
{
x=inf,y=inf;
FOR(j,1,m)
{
z=as(a[i].first,a[i].second,b[j].first,b[j].second);
if(z)
{
c[z]++;
if(x==inf) x=z;
else if(y==inf&&z!=x) y=z;
}
}
if(x!=inf&&y==inf&&qw==0) qw=1;
if(x!=inf&&y!=inf) qw=3;
}
FOR(i,1,m)
{
x=inf,y=inf;
FOR(j,1,n)
{
z=as(b[i].first,b[i].second,a[j].first,a[j].second);
if(z)
{
c[z]++;
if(x==inf) x=z;
else if(y==inf&&z!=x) y=z;
}
}
if(x!=inf&&y==inf&&qw==1) qw=2;
if(x!=inf&&y!=inf) qw=3;
}
FOR(i,1,9) if(c[i]) s++;
if(s==1)
{
FOR(i,1,9) if(c[i]) cout<<i<<endl;
return 0;
}
if(qw==2) puts("0");
else puts("-1");
return 0;
}