-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1003.cpp
119 lines (94 loc) · 1.81 KB
/
loj1003.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
using namespace std;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#define BLaCK 3
#define GRaY 2
#define WHiTE 1
#define SIZE 10010
#define dinka cout<<"enter"<<endl
vector<int>graph[SIZE];
bool taken[SIZE],cyc;
int check[SIZE],ans;
void cycle(int s)
{
int i;
check[s]=GRaY;
for(i=0;i<graph[s].size();i++)
{
if(check[graph[s][i]]==GRaY)
{
cyc=true;
return;
}
cycle(graph[s][i]);
}
check[s]=BLaCK;
}
void dfs(int s)
{
int i;
if(taken[s]==false)
{
for(i=0;i<graph[s].size();i++)
dfs(graph[s][i]);
ans++;
taken[s]=true;
}
}
int main()
{
int t,i,mp,Case=1,n;
string s1,s2;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
map<string,int>m;
mp=1;
for(i=0;i<n;i++)
{
cin>>s1>>s2;
if(!m[s1])
m[s1]=mp++;
if(!m[s2])
m[s2]=mp++;
graph[m[s1]].push_back(m[s2]);
}
for(i=1;i<mp;i++)
check[i]=WHiTE;
cyc=false;
for(i=1;i<mp;i++)
{
if(check[i]==WHiTE)
{
cycle(i);
if(cyc)
break;
}
}
if(cyc)
{
printf("Case %d: No\n",Case++);
}
else
{
memset(taken,false,sizeof(taken));
ans=0;
for(i=1;i<mp;i++)
{
dfs(i);
}
if(ans==mp-1)
printf("Case %d: Yes\n",Case++);
else
printf("Case %d: No\n",Case++);
}
for(i=1;i<mp;i++)
graph[i].clear();
}
return 0;
}