-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpoj_CAPCITY.cpp
166 lines (147 loc) · 2.57 KB
/
Spoj_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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
maneesh(maik)
*/
// SCC problem
// Kosaraju algo + DFS
#include<iostream>
#include<algorithm>
#include<string>
#include<bitset>
#include<deque>
#include<iterator>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);
#define mx 200000
stack<ll>s;
vector<ll>G[mx],Gt[mx];
ll A[mx]={0};
ll in[mx]={0};
void DFS1(ll p, ll vis[] )
{
vis[p]=1;
for(ll i=0; i<G[p].size(); i++)
{
ll it=G[p][i];
if(vis[it]==0)
{
DFS1(it, vis);
}
}
s.push(p);
//cout<<p<<" ";
}
void DFS2(ll p, ll vis[], ll comp )
{
vis[p]=1; A[p]=comp; //cout<<p<<" ";
for(ll i=0; i<Gt[p].size(); i++)
{
ll it=Gt[p][i];
if(vis[it]==0)
{
DFS2(it, vis, comp);
}
}
}
int main()
{
ll n,m;
scanf("%lld %lld",&n,&m);
ll i,j,x,y;
ll vis[n+5];
for(i=1; i<=n; i++)
{
vis[i]=0;
in[i]=0;
}
for(i=0; i<m; i++)
{
scanf("%lld %lld",&x,&y);
G[x].pb(y);
Gt[y].pb(x);
}
/* for(i=1; i<=n; i++)
{
cout<<i<<"--> ";
for(j=0; j<Gt[i].size(); j++)
{
cout<<Gt[i][j]<<" ";
}cout<<endl;
}*/
for(i=1; i<=n; i++)
{
if(vis[i]==0)
{
DFS1(i, vis);
}
}
for(i=1; i<=n; i++)
vis[i]=0;
ll comp=0;
while(s.size())
{
ll p=s.top();
s.pop();
if(vis[p]==0)
{
comp++;
DFS2(p, vis, comp);
//cout<<endl;
}
}
/*for(i=1; i<=n; i++)
cout<<A[i]<<" ";
cout<<endl;*/
ll n1=comp;
for(i=1; i<=n; i++)
{
for(j=0; j<Gt[i].size(); j++)
{
ll it=Gt[i][j];
if(A[i]!=A[it])
{
in[A[it]]+=1;
}
}
}
ll cnt=0;
for(i=1; i<=n1; i++)
{
if(in[i]==0)
cnt++;
}
if(cnt>1)
{
cout<<printf("0");
}
else
{
cnt=0;
for(i=1; i<=n; i++)
{
if(in[A[i]]==0)
cnt++;
}
printf("%lld\n",cnt);
for(i=1; i<=n; i++)
{
if(in[A[i]]==0)
{
printf("%lld ",i);
}
}
printf("\n");
}
return 0;
}