-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOTTOM.cpp
95 lines (79 loc) · 1.7 KB
/
BOTTOM.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
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
using namespace std;
#define MAX 5000
vector< int > graph[MAX+1], graphT[MAX+1], sorted;
bool visited [MAX+1];
int ans[MAX+1], sol[MAX+1];
void reinit(int v)
{
sorted.clear();
for(int i=0; i<v; ++i)
{
graph[i].clear();
graphT[i].clear();
visited[i]=false;
sol[i]=false;
}
}
void dfs1(int S)
{
int i=0;
visited[S]=true;
for(i=0; i<graph[S].size(); ++i)
if(!visited[graph[S][i]])
dfs1(graph[S][i]);
sorted.push_back(S);
}
void dfs2(int S, int component)
{
int i=0;
visited[S]=false;
ans[S]=component;
for(i=0; i<graphT[S].size(); ++i)
if(visited[graphT[S][i]])
dfs2(graphT[S][i], component);
}
int main(void)
{
int v, e, x, y, i;
while(true)
{
scanf("%d", &v);
if(v==0)
break;
scanf("%d",&e);
reinit(v);
while(e--)
{
scanf("%d""%d", &x, &y);
graph[x-1].push_back(y-1);
graphT[y-1].push_back(x-1);
}
for(i=0; i<v; ++i)
if(!visited[i])
dfs1(i);
int component=0;
//read this one (which was encountered) : http://stackoverflow.com/questions/30395205/why-are-unsigned-integers-error-prone
for(int j=sorted.size()-1; j>=0; --j)
{
if(visited[sorted[j]])
dfs2(sorted[j], component++);
sol[component-1]=true;
}
for(i=0; i<v; ++i)
for(int j=0; j<graph[i].size(); ++j)
if(ans[i]!=ans[graph[i][j]])
{
sol[ans[i]]=false;
break;
}
for(i=0; i<v; ++i)
if(sol[ans[i]])
printf("%d ", i+1);
printf("\n");
}
return 0;
}