forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
capcity.cpp
85 lines (84 loc) · 1.41 KB
/
capcity.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
// 2010-06-20
//Kosaraju.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
stack<int> S;
bool vis[100000]={0};
bool bad[100000]={0};
int sc[100000];
vector<int> adj[100000];
vector<int> tr[100000];
int cnt=0;
int scnt=0;
void dfs(int x)
{
if (vis[x]) return;
vis[x]=true;
for (int i=0; i<adj[x].size(); i++)
dfs(adj[x][i]);
// cout << x << endl;
S.push(x);
}
void dfs2(int x)
{
if (~sc[x]) return;
sc[x]=scnt;
for (int i=0; i<tr[x].size(); i++)
dfs2(tr[x][i]);
}
int main()
{
int V,E,x,y,i,j;
scanf("%d %d",&V,&E);
while (E--)
{
scanf("%d %d",&x,&y);
adj[x-1].push_back(y-1);
tr[y-1].push_back(x-1);
}
for (i=0; i<V; i++)
dfs(i);
memset(sc,-1,sizeof(sc));
while (!S.empty())
{
if (sc[S.top()]==-1)
{
dfs2(S.top());
scnt++;
}
S.pop();
}
//DEBUG
// for (i=0; i<V; i++)
// printf("sc[%d]=%d\n",i,sc[i]);
for (i=0; i<V; i++)
for (j=0; j<adj[i].size(); j++)
if (sc[adj[i][j]]!=sc[i])
bad[sc[i]]=true;
int ok=-1;
// printf("scnt=%d\n",scnt);
for (i=0; i<scnt; i++)
if (!bad[i])
if (~ok)
{
printf("0\n");
return 0;
}
else
ok=i;
//cout << ok << endl;
int c=0;
for (i=0; i<V; i++)
if (sc[i]==ok)
c++;
printf("%d\n",c);
for (i=0; i<V; i++)
if (sc[i]==ok)
printf("%d ",i+1);
printf("\n");
return 0;
}