Problem
As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself. That what he's got you -- his loyal henchman -- for.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a positive integer M on a line by itself -- the number of troublesome pairs of League members. The next M lines each contain a pair of names, separated by a single space.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.
Limits
1 ≤ T ≤ 100.
Each member name will consist of only letters and the underscore character.
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.
Small dataset
1 ≤ M ≤ 10.
Large dataset
1 ≤ M ≤ 100.
Sample
Input
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself. That what he's got you -- his loyal henchman -- for.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a positive integer M on a line by itself -- the number of troublesome pairs of League members. The next M lines each contain a pair of names, separated by a single space.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.
Limits
1 ≤ T ≤ 100.
Each member name will consist of only letters and the underscore character.
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.
Small dataset
1 ≤ M ≤ 10.
Large dataset
1 ≤ M ≤ 100.
Sample
Input
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
Fury_Leika Dead_Bowie
Output
Case #2: No
备注:先构建图,然后判断图是否二分。构建图时用map建立名字的映射。注意图不连通时的判断(代码已修正,感谢tommyhu111朋友的指正)
#include<stdio.h>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int MAXSIZE = 1000;
typedef struct Node NODE;
struct Node
{
int color;
vector<int> neigh_list;
};
bool checkAllNodesVisited(NODE *graph, int numNodes, int &index)
{
for (int i=0; i<numNodes; i++)
{
if (graph[i].color == -1)
{
index = i;
return false;
}
}
return true;
}
bool JudgeBiGraph(NODE *graph, int numNodes)
{
int start = 0;
do
{
queue<int> Myqueue;
Myqueue.push(start);
graph[start].color = 0;
while(!Myqueue.empty())
{
int gid = Myqueue.front();
for(int i=0; i<graph[gid].neigh_list.size(); i++)
{
int neighid = graph[gid].neigh_list[i];
if(graph[neighid].color == -1)
{
graph[neighid].color = (graph[gid].color+1)%2; // assign to another group
Myqueue.push(neighid);
}
else
{
if(graph[neighid].color == graph[gid].color) // touble pair in the same group
return false;
}
}
Myqueue.pop();
}
}while (!checkAllNodesVisited(graph, numNodes, start));
return true;
}
int main()
{
int T;
int M;
int caseid = 0;
scanf("%d",&T);
while(caseid<T)
{
scanf("%d",&M);
map<string,int> Mymap;
NODE graph[MAXSIZE];
int id = 0;
for(int i=0;i<M;i++)
{
char s1[MAXSIZE],s2[MAXSIZE];
scanf("%s %s",s1,s2);
string str_s1(s1),str_s2(s2);
NODE node1,node2;
int id1,id2;
if(Mymap.count(str_s1)==0)
{
Mymap[str_s1] = id;
id1 = id;
id++;
}
else
id1 = Mymap[str_s1];
if(Mymap.count(str_s2)==0)
{
Mymap[str_s2] = id;
id2 = id;
id++;
}
else
id2 = Mymap[str_s2];
graph[id1].neigh_list.push_back(id2);
graph[id2].neigh_list.push_back(id1);
}
//judge whether it is a bipartite graph
for(int i=0;i<id;i++)
{
graph[i].color = -1;
}
if(JudgeBiGraph(graph,id))
printf("Case #%d: Yes\n",caseid+1);
else
printf("Case #%d: No\n",caseid+1);
caseid++;
}
return 0;
}