-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCheckBipartiteGraph.cpp
More file actions
107 lines (96 loc) · 1.67 KB
/
CheckBipartiteGraph.cpp
File metadata and controls
107 lines (96 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define LD long double
#define gc getchar_unlocked
#define pc putchar_unlocked
#define MOD 1000000007
#define MAXN 1000003
#define bitcount __builtin_popcount
#define INF 2000000000
#define EPS 1e-9
template<typename T>T absll(T X)
{
if(X<0)
return -1*X;
else
return X;
}
map<string,map<string,int> >M;
map<string,bool> isPresent;
vector<string> names;
bool isBipartite(string src)
{
map<string,int> color;
for(vector<string>::iterator it=names.begin();it!=names.end();it++)
{
color[*it]=-1;
}
color[src]=1;
queue<string> Q;
Q.push(src);
while(!Q.empty())
{
string str=Q.front();
Q.pop();
for(vector<string>::iterator it=names.begin();it!=names.end();it++)
{
if(M[str][*it]&&color[*it]==-1)
{
color[*it]=1-color[str];
Q.push(*it);
}
else if(M[str][*it]&&color[*it]==color[str])
{
return false;
}
}
}
return true;
}
int main()
{
int T;
freopen("input.txt","r",stdin);//redirects standard input
freopen("output.txt","w",stdout);//redirects standard output
scanf("%d",&T);
for(int test=1;test<=T;test++)
{
int N;
scanf("%d",&N);
names.clear();
M.clear();
isPresent.clear();
for(int i=0;i<N;i++)
{
string A,B;
cin>>A>>B;
M[A][B]=1;
M[B][A]=1;
if(!isPresent[A])
{
names.pb(A);
isPresent[A]=true;
}
if(!isPresent[B])
{
names.pb(B);
isPresent[B]=true;
}
}
string src=names[0];
bool flag=isBipartite(src);
if(flag)
{
printf("Case #%d: Yes\n",test);
}
else
{
printf("Case #%d: No\n",test);
}
}
return 0;
}