forked from sdssudhu/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCPC11.cpp
More file actions
76 lines (74 loc) · 1.73 KB
/
Copy pathGCPC11.cpp
File metadata and controls
76 lines (74 loc) · 1.73 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
// Indiana Jones and the lost Soccer Cup
#include<bits/stdc++.h>
using namespace std;
int n,h,visited[10001],indeg[10001],outdeg[10001],mark[10001];
int dfs(vector<int> graph[],int s,int p)
{
int i;
mark[s]=visited[s]=1;
for (i=0; i<graph[s].size(); i++)
{
if (p==0)
{
if (visited[graph[s][i]]==1 || (visited[graph[s][i]]==0 && dfs(graph,graph[s][i],p)==-1))
return -1;
}
else if (p==1 && mark[graph[s][i]]==0)
dfs(graph,graph[s][i],p);
}
visited[s]=2;
if (p==1)
cout<<s<<endl;
return 0;
}
int main()
{
int c;
cin>>c;
vector<int> graph[10001];
while (c--)
{
scanf("%d%d",&n,&h);
int i,z,y,f=0;
z=y=n;
for (i=1; i<=n; i++)
{
indeg[i]=outdeg[i]=mark[i]=visited[i]=0;
graph[i].clear();
}
while(h--)
{
int a,b;
cin>>a>>b;
graph[b].push_back(a);
if (indeg[b]==0)
z--;
if (outdeg[a]==0)
y--;
indeg[b]++;
outdeg[a]++;
}
for (i=1; i<=n; i++)
{
if (visited[i]==0 && dfs(graph,i,0)==-1)
{
f=1;
break;
}
}
if (f==1)
cout<<"recheck hints"<<endl;
else if (z!=1 || y!=1)
cout<<"missing hints"<<endl;
else
{
for (i=1; i<=n; i++)
mark[i]=0;
for (i=1; i<=n; i++)
if (mark[i]==0)
dfs(graph,i,1);
cout<<endl;
}
}
return 0;
}