就是简单的判断二分图,存下代码,不用每次都重新写
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define MAXN 21
#define maxn 205
#define mod 1000000007
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int color[maxn];
vector<int> G[maxn];
map<string,int> mp;
int cnt;
int insert(string s)
{
if(mp.find(s) == mp.end()) mp[s] = cnt++;
return mp[s];
}
bool dfs(int u)
{
rep(i,(int)G[u].size())
{
int v = G[u][i];
if(color[v] == color[u]) return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!dfs(v)) return false;
}
}
return true;
}
bool solve()
{
rep(i,cnt)
{
if(!color[i])
{
color[i] = 1;
if(!dfs(i)) return false;
}
}
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,n;
cin>>T;
int cas = 1;
while(T--)
{
cin>>n;
string s,t;
mp.clear();
ini(color);
rep(i,2*n) G[i].clear();
cnt = 0;
rep(i,n)
{
cin>>s>>t;
int a = insert(s);
int b = insert(t);
G[a].push_back(b);
G[b].push_back(a);
}
printf("Case #%d: ",cas++);
if(solve()) puts("Yes");
else puts("No");
}
return 0;
}