#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 |
212 ms |
90892 KB |
Output is correct |
2 |
Correct |
210 ms |
90464 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
120 ms |
62764 KB |
Output is correct |
5 |
Correct |
164 ms |
78148 KB |
Output is correct |
6 |
Correct |
197 ms |
77708 KB |
Output is correct |
7 |
Correct |
166 ms |
76016 KB |
Output is correct |
8 |
Correct |
200 ms |
79580 KB |
Output is correct |
9 |
Correct |
548 ms |
85900 KB |
Output is correct |
10 |
Correct |
386 ms |
81820 KB |
Output is correct |
11 |
Correct |
535 ms |
83732 KB |
Output is correct |
12 |
Correct |
476 ms |
76344 KB |
Output is correct |
13 |
Correct |
328 ms |
77600 KB |
Output is correct |
14 |
Correct |
301 ms |
77956 KB |
Output is correct |
15 |
Correct |
290 ms |
78132 KB |
Output is correct |
16 |
Correct |
268 ms |
78196 KB |
Output is correct |
17 |
Correct |
59 ms |
27468 KB |
Output is correct |
18 |
Correct |
96 ms |
29092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
91024 KB |
Output is correct |
2 |
Correct |
217 ms |
78724 KB |
Output is correct |
3 |
Correct |
175 ms |
73768 KB |
Output is correct |
4 |
Correct |
503 ms |
82456 KB |
Output is correct |
5 |
Correct |
631 ms |
83872 KB |
Output is correct |
6 |
Correct |
409 ms |
80728 KB |
Output is correct |
7 |
Correct |
494 ms |
80976 KB |
Output is correct |
8 |
Correct |
325 ms |
80524 KB |
Output is correct |
9 |
Correct |
282 ms |
79056 KB |
Output is correct |
10 |
Correct |
91 ms |
26880 KB |
Output is correct |
11 |
Correct |
50 ms |
28516 KB |
Output is correct |
12 |
Correct |
60 ms |
28520 KB |
Output is correct |
13 |
Correct |
233 ms |
97064 KB |
Output is correct |
14 |
Correct |
299 ms |
70736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24532 KB |
Output is correct |
2 |
Correct |
14 ms |
24388 KB |
Output is correct |
3 |
Correct |
14 ms |
24424 KB |
Output is correct |
4 |
Correct |
15 ms |
24404 KB |
Output is correct |
5 |
Correct |
15 ms |
24376 KB |
Output is correct |
6 |
Correct |
14 ms |
24404 KB |
Output is correct |
7 |
Correct |
15 ms |
24436 KB |
Output is correct |
8 |
Correct |
14 ms |
24276 KB |
Output is correct |
9 |
Correct |
14 ms |
23892 KB |
Output is correct |
10 |
Correct |
15 ms |
23832 KB |
Output is correct |
11 |
Correct |
15 ms |
23792 KB |
Output is correct |
12 |
Correct |
14 ms |
24512 KB |
Output is correct |
13 |
Correct |
15 ms |
24276 KB |
Output is correct |
14 |
Correct |
13 ms |
23844 KB |
Output is correct |
15 |
Correct |
15 ms |
24404 KB |
Output is correct |
16 |
Correct |
14 ms |
24404 KB |
Output is correct |
17 |
Correct |
15 ms |
24320 KB |
Output is correct |
18 |
Correct |
15 ms |
23892 KB |
Output is correct |
19 |
Correct |
14 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
90892 KB |
Output is correct |
2 |
Correct |
210 ms |
90464 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
120 ms |
62764 KB |
Output is correct |
5 |
Correct |
164 ms |
78148 KB |
Output is correct |
6 |
Correct |
197 ms |
77708 KB |
Output is correct |
7 |
Correct |
166 ms |
76016 KB |
Output is correct |
8 |
Correct |
200 ms |
79580 KB |
Output is correct |
9 |
Correct |
548 ms |
85900 KB |
Output is correct |
10 |
Correct |
386 ms |
81820 KB |
Output is correct |
11 |
Correct |
535 ms |
83732 KB |
Output is correct |
12 |
Correct |
476 ms |
76344 KB |
Output is correct |
13 |
Correct |
328 ms |
77600 KB |
Output is correct |
14 |
Correct |
301 ms |
77956 KB |
Output is correct |
15 |
Correct |
290 ms |
78132 KB |
Output is correct |
16 |
Correct |
268 ms |
78196 KB |
Output is correct |
17 |
Correct |
59 ms |
27468 KB |
Output is correct |
18 |
Correct |
96 ms |
29092 KB |
Output is correct |
19 |
Correct |
214 ms |
91024 KB |
Output is correct |
20 |
Correct |
217 ms |
78724 KB |
Output is correct |
21 |
Correct |
175 ms |
73768 KB |
Output is correct |
22 |
Correct |
503 ms |
82456 KB |
Output is correct |
23 |
Correct |
631 ms |
83872 KB |
Output is correct |
24 |
Correct |
409 ms |
80728 KB |
Output is correct |
25 |
Correct |
494 ms |
80976 KB |
Output is correct |
26 |
Correct |
325 ms |
80524 KB |
Output is correct |
27 |
Correct |
282 ms |
79056 KB |
Output is correct |
28 |
Correct |
91 ms |
26880 KB |
Output is correct |
29 |
Correct |
50 ms |
28516 KB |
Output is correct |
30 |
Correct |
60 ms |
28520 KB |
Output is correct |
31 |
Correct |
233 ms |
97064 KB |
Output is correct |
32 |
Correct |
299 ms |
70736 KB |
Output is correct |
33 |
Correct |
15 ms |
24532 KB |
Output is correct |
34 |
Correct |
14 ms |
24388 KB |
Output is correct |
35 |
Correct |
14 ms |
24424 KB |
Output is correct |
36 |
Correct |
15 ms |
24404 KB |
Output is correct |
37 |
Correct |
15 ms |
24376 KB |
Output is correct |
38 |
Correct |
14 ms |
24404 KB |
Output is correct |
39 |
Correct |
15 ms |
24436 KB |
Output is correct |
40 |
Correct |
14 ms |
24276 KB |
Output is correct |
41 |
Correct |
14 ms |
23892 KB |
Output is correct |
42 |
Correct |
15 ms |
23832 KB |
Output is correct |
43 |
Correct |
15 ms |
23792 KB |
Output is correct |
44 |
Correct |
14 ms |
24512 KB |
Output is correct |
45 |
Correct |
15 ms |
24276 KB |
Output is correct |
46 |
Correct |
13 ms |
23844 KB |
Output is correct |
47 |
Correct |
15 ms |
24404 KB |
Output is correct |
48 |
Correct |
14 ms |
24404 KB |
Output is correct |
49 |
Correct |
15 ms |
24320 KB |
Output is correct |
50 |
Correct |
15 ms |
23892 KB |
Output is correct |
51 |
Correct |
14 ms |
23892 KB |
Output is correct |
52 |
Correct |
212 ms |
90832 KB |
Output is correct |
53 |
Correct |
193 ms |
78736 KB |
Output is correct |
54 |
Correct |
151 ms |
73716 KB |
Output is correct |
55 |
Correct |
522 ms |
82408 KB |
Output is correct |
56 |
Correct |
612 ms |
83828 KB |
Output is correct |
57 |
Correct |
334 ms |
80848 KB |
Output is correct |
58 |
Correct |
322 ms |
81024 KB |
Output is correct |
59 |
Correct |
309 ms |
80556 KB |
Output is correct |
60 |
Correct |
60 ms |
27228 KB |
Output is correct |
61 |
Correct |
51 ms |
28520 KB |
Output is correct |
62 |
Correct |
52 ms |
28540 KB |
Output is correct |
63 |
Correct |
198 ms |
97360 KB |
Output is correct |
64 |
Correct |
176 ms |
69520 KB |
Output is correct |
65 |
Correct |
55 ms |
28668 KB |
Output is correct |
66 |
Correct |
544 ms |
78228 KB |
Output is correct |
67 |
Correct |
336 ms |
79716 KB |
Output is correct |
68 |
Correct |
295 ms |
78184 KB |
Output is correct |
69 |
Correct |
53 ms |
27340 KB |
Output is correct |
70 |
Correct |
137 ms |
37940 KB |
Output is correct |