#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 500005;
int n, m;
int deg[MAXN], removed[MAXN], bio[MAXN];
vector <int> v[MAXN], r[MAXN], sol, st, curr_comp;
vector < vector <int> > comp;
queue <int> q;
/////////////////////////////////////////////////////////////
void dfs (int node) {
if (bio[node]) return;
bio[node] = 1;
for (auto sus : r[node]) {
if (!bio[sus]) dfs(sus);
}
st.push_back(node);
}
void dfs2 (int node) {
if (bio[node] == 2) return;
bio[node] = 2;
curr_comp.push_back(node);
for (auto sus : v[node]) {
if (bio[sus] == 1) dfs2(sus);
}
}
void kosaraju () {
for (int i = 1; i <= n; i++) {
if (!bio[i]) dfs(i);
}
while (!st.empty()) {
int x = st.back();
st.pop_back();
if (bio[x] == 2) continue;
curr_comp.clear();
dfs2(x);
comp.push_back(curr_comp);
}
}
/////////////////////////////////////////////////////////////
void makni (int node, int flg) {
removed[node] = 1;
for (auto sus : r[node]) {
deg[sus]--;
if (deg[sus] == 0 && flg) q.push(sus);
}
}
void izaberi (int node, int flg) {
removed[node] = 1;
sol.push_back(node);
for (auto sus : r[node]) {
if (!removed[sus]) makni(sus, flg);
}
}
void remove_terminal_nodes (vector <int> nodes) {
for (auto node : nodes) {
if (removed[node]) continue;
if (deg[node] == 0) q.push(node);
}
while (!q.empty()) {
int node = q.front();
q.pop();
if (removed[node]) continue;
izaberi(node, 1);
}
}
void oboji (int node, int boja) {
if (bio[node] || removed[node]) return;
bio[node] = 1;
if (boja == 0) st.push_back(node);
for (auto sus : v[node]) oboji(sus, !boja);
for (auto sus : r[node]) oboji(sus, !boja);
}
void solve_bipartite (vector <int> nodes) {
st.clear();
for (auto node : nodes) bio[node] = 0;
for (auto node : nodes) {
oboji(node, 0);
}
for (auto node : st) {
izaberi(node, 0);
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
deg[a]++;
v[a].push_back(b);
r[b].push_back(a);
}
kosaraju();
for (int i = 0; i < comp.size(); i++) {
remove_terminal_nodes(comp[i]);
solve_bipartite(comp[i]);
}
cout << sol.size() << endl;
for (auto x : sol) cout << x << " ";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int i = 0; i < comp.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
97972 KB |
Output is correct |
2 |
Correct |
166 ms |
97476 KB |
Output is correct |
3 |
Correct |
7 ms |
25188 KB |
Output is correct |
4 |
Correct |
82 ms |
62688 KB |
Output is correct |
5 |
Correct |
127 ms |
81820 KB |
Output is correct |
6 |
Correct |
145 ms |
84520 KB |
Output is correct |
7 |
Correct |
132 ms |
80496 KB |
Output is correct |
8 |
Correct |
154 ms |
83832 KB |
Output is correct |
9 |
Correct |
442 ms |
92092 KB |
Output is correct |
10 |
Correct |
364 ms |
88236 KB |
Output is correct |
11 |
Correct |
439 ms |
90156 KB |
Output is correct |
12 |
Correct |
446 ms |
83848 KB |
Output is correct |
13 |
Correct |
272 ms |
84132 KB |
Output is correct |
14 |
Correct |
283 ms |
84396 KB |
Output is correct |
15 |
Correct |
237 ms |
84452 KB |
Output is correct |
16 |
Correct |
220 ms |
84652 KB |
Output is correct |
17 |
Correct |
40 ms |
33108 KB |
Output is correct |
18 |
Correct |
61 ms |
36276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
97512 KB |
Output is correct |
2 |
Correct |
148 ms |
85384 KB |
Output is correct |
3 |
Correct |
116 ms |
78648 KB |
Output is correct |
4 |
Correct |
465 ms |
89092 KB |
Output is correct |
5 |
Correct |
563 ms |
90668 KB |
Output is correct |
6 |
Correct |
363 ms |
87628 KB |
Output is correct |
7 |
Correct |
298 ms |
87380 KB |
Output is correct |
8 |
Correct |
255 ms |
86964 KB |
Output is correct |
9 |
Correct |
289 ms |
86572 KB |
Output is correct |
10 |
Correct |
43 ms |
32336 KB |
Output is correct |
11 |
Correct |
36 ms |
32980 KB |
Output is correct |
12 |
Correct |
41 ms |
33096 KB |
Output is correct |
13 |
Correct |
166 ms |
103484 KB |
Output is correct |
14 |
Correct |
141 ms |
77256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
28248 KB |
Output is correct |
2 |
Correct |
8 ms |
27740 KB |
Output is correct |
3 |
Correct |
7 ms |
27784 KB |
Output is correct |
4 |
Correct |
9 ms |
27740 KB |
Output is correct |
5 |
Correct |
8 ms |
27740 KB |
Output is correct |
6 |
Correct |
8 ms |
27740 KB |
Output is correct |
7 |
Correct |
8 ms |
27736 KB |
Output is correct |
8 |
Correct |
7 ms |
27740 KB |
Output is correct |
9 |
Correct |
6 ms |
27280 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
27228 KB |
Output is correct |
12 |
Correct |
8 ms |
27992 KB |
Output is correct |
13 |
Correct |
8 ms |
27740 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
9 ms |
27788 KB |
Output is correct |
16 |
Correct |
9 ms |
27736 KB |
Output is correct |
17 |
Correct |
8 ms |
27736 KB |
Output is correct |
18 |
Correct |
8 ms |
27228 KB |
Output is correct |
19 |
Correct |
7 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
97972 KB |
Output is correct |
2 |
Correct |
166 ms |
97476 KB |
Output is correct |
3 |
Correct |
7 ms |
25188 KB |
Output is correct |
4 |
Correct |
82 ms |
62688 KB |
Output is correct |
5 |
Correct |
127 ms |
81820 KB |
Output is correct |
6 |
Correct |
145 ms |
84520 KB |
Output is correct |
7 |
Correct |
132 ms |
80496 KB |
Output is correct |
8 |
Correct |
154 ms |
83832 KB |
Output is correct |
9 |
Correct |
442 ms |
92092 KB |
Output is correct |
10 |
Correct |
364 ms |
88236 KB |
Output is correct |
11 |
Correct |
439 ms |
90156 KB |
Output is correct |
12 |
Correct |
446 ms |
83848 KB |
Output is correct |
13 |
Correct |
272 ms |
84132 KB |
Output is correct |
14 |
Correct |
283 ms |
84396 KB |
Output is correct |
15 |
Correct |
237 ms |
84452 KB |
Output is correct |
16 |
Correct |
220 ms |
84652 KB |
Output is correct |
17 |
Correct |
40 ms |
33108 KB |
Output is correct |
18 |
Correct |
61 ms |
36276 KB |
Output is correct |
19 |
Correct |
172 ms |
97512 KB |
Output is correct |
20 |
Correct |
148 ms |
85384 KB |
Output is correct |
21 |
Correct |
116 ms |
78648 KB |
Output is correct |
22 |
Correct |
465 ms |
89092 KB |
Output is correct |
23 |
Correct |
563 ms |
90668 KB |
Output is correct |
24 |
Correct |
363 ms |
87628 KB |
Output is correct |
25 |
Correct |
298 ms |
87380 KB |
Output is correct |
26 |
Correct |
255 ms |
86964 KB |
Output is correct |
27 |
Correct |
289 ms |
86572 KB |
Output is correct |
28 |
Correct |
43 ms |
32336 KB |
Output is correct |
29 |
Correct |
36 ms |
32980 KB |
Output is correct |
30 |
Correct |
41 ms |
33096 KB |
Output is correct |
31 |
Correct |
166 ms |
103484 KB |
Output is correct |
32 |
Correct |
141 ms |
77256 KB |
Output is correct |
33 |
Correct |
7 ms |
28248 KB |
Output is correct |
34 |
Correct |
8 ms |
27740 KB |
Output is correct |
35 |
Correct |
7 ms |
27784 KB |
Output is correct |
36 |
Correct |
9 ms |
27740 KB |
Output is correct |
37 |
Correct |
8 ms |
27740 KB |
Output is correct |
38 |
Correct |
8 ms |
27740 KB |
Output is correct |
39 |
Correct |
8 ms |
27736 KB |
Output is correct |
40 |
Correct |
7 ms |
27740 KB |
Output is correct |
41 |
Correct |
6 ms |
27280 KB |
Output is correct |
42 |
Correct |
6 ms |
27228 KB |
Output is correct |
43 |
Correct |
7 ms |
27228 KB |
Output is correct |
44 |
Correct |
8 ms |
27992 KB |
Output is correct |
45 |
Correct |
8 ms |
27740 KB |
Output is correct |
46 |
Correct |
6 ms |
27228 KB |
Output is correct |
47 |
Correct |
9 ms |
27788 KB |
Output is correct |
48 |
Correct |
9 ms |
27736 KB |
Output is correct |
49 |
Correct |
8 ms |
27736 KB |
Output is correct |
50 |
Correct |
8 ms |
27228 KB |
Output is correct |
51 |
Correct |
7 ms |
27484 KB |
Output is correct |
52 |
Correct |
177 ms |
97968 KB |
Output is correct |
53 |
Correct |
183 ms |
85284 KB |
Output is correct |
54 |
Correct |
117 ms |
77876 KB |
Output is correct |
55 |
Correct |
420 ms |
89256 KB |
Output is correct |
56 |
Correct |
548 ms |
90404 KB |
Output is correct |
57 |
Correct |
326 ms |
87876 KB |
Output is correct |
58 |
Correct |
301 ms |
87724 KB |
Output is correct |
59 |
Correct |
262 ms |
86964 KB |
Output is correct |
60 |
Correct |
42 ms |
32852 KB |
Output is correct |
61 |
Correct |
36 ms |
33064 KB |
Output is correct |
62 |
Correct |
40 ms |
32984 KB |
Output is correct |
63 |
Correct |
178 ms |
103712 KB |
Output is correct |
64 |
Correct |
136 ms |
75856 KB |
Output is correct |
65 |
Correct |
42 ms |
33548 KB |
Output is correct |
66 |
Correct |
402 ms |
84668 KB |
Output is correct |
67 |
Correct |
274 ms |
87304 KB |
Output is correct |
68 |
Correct |
251 ms |
84908 KB |
Output is correct |
69 |
Correct |
42 ms |
32880 KB |
Output is correct |
70 |
Correct |
106 ms |
46100 KB |
Output is correct |