//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 5e5 + 3e2;
set<int> g[N], gr[N];
bool removed[N];
void solve(){
int n, m;
cin >> n >> m;
while (m--){
int u, v;
cin >> u >> v;
g[u].insert(v);
gr[v].insert(u);
}
priority_queue<pair<int, int>> pq;
for (int i = 1; i <= n; i++) pq.emplace(-1 * (int)g[i].size(), i);
vector<int> ans;
while (!pq.empty()){
auto [_sz, v] = pq.top();
pq.pop();
if ((int) g[v].size() != _sz * -1 || removed[v]) continue;
ans.push_back(v);
removed[v] = true;
for (int u : g[v]){ // v -> u
if (removed[u]) continue;
removed[u] = true;
for (int x : gr[u]){
if (removed[x]) continue;
g[x].erase(u);
pq.emplace(-1 * (int)g[x].size(), x);
}
}
for (int u : gr[v]){ // u -> v
removed[u] = true;
}
}
cout << ans.size() << endl;
for (int v : ans) cout << v << ' ';
cout << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:71:13: warning: unused variable 'startTime' [-Wunused-variable]
71 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
106192 KB |
Output is correct |
2 |
Correct |
187 ms |
105864 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
95 ms |
58600 KB |
Output is correct |
5 |
Correct |
141 ms |
82064 KB |
Output is correct |
6 |
Correct |
261 ms |
105536 KB |
Output is correct |
7 |
Correct |
264 ms |
102524 KB |
Output is correct |
8 |
Correct |
278 ms |
104616 KB |
Output is correct |
9 |
Correct |
447 ms |
109412 KB |
Output is correct |
10 |
Correct |
324 ms |
104212 KB |
Output is correct |
11 |
Correct |
391 ms |
108824 KB |
Output is correct |
12 |
Correct |
518 ms |
104172 KB |
Output is correct |
13 |
Correct |
374 ms |
105764 KB |
Output is correct |
14 |
Correct |
355 ms |
106052 KB |
Output is correct |
15 |
Correct |
317 ms |
106116 KB |
Output is correct |
16 |
Correct |
301 ms |
105912 KB |
Output is correct |
17 |
Correct |
127 ms |
71648 KB |
Output is correct |
18 |
Correct |
244 ms |
97852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
106188 KB |
Output is correct |
2 |
Correct |
261 ms |
106824 KB |
Output is correct |
3 |
Correct |
251 ms |
99840 KB |
Output is correct |
4 |
Correct |
383 ms |
107468 KB |
Output is correct |
5 |
Correct |
545 ms |
110200 KB |
Output is correct |
6 |
Correct |
375 ms |
107112 KB |
Output is correct |
7 |
Correct |
371 ms |
107344 KB |
Output is correct |
8 |
Correct |
332 ms |
107188 KB |
Output is correct |
9 |
Correct |
296 ms |
106660 KB |
Output is correct |
10 |
Correct |
141 ms |
70256 KB |
Output is correct |
11 |
Correct |
88 ms |
61528 KB |
Output is correct |
12 |
Correct |
77 ms |
62128 KB |
Output is correct |
13 |
Correct |
185 ms |
103796 KB |
Output is correct |
14 |
Correct |
195 ms |
107124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47896 KB |
Output is correct |
2 |
Correct |
22 ms |
47828 KB |
Output is correct |
3 |
Correct |
21 ms |
47788 KB |
Output is correct |
4 |
Correct |
23 ms |
47828 KB |
Output is correct |
5 |
Correct |
23 ms |
47876 KB |
Output is correct |
6 |
Correct |
23 ms |
47940 KB |
Output is correct |
7 |
Correct |
23 ms |
47844 KB |
Output is correct |
8 |
Correct |
26 ms |
47740 KB |
Output is correct |
9 |
Correct |
26 ms |
47572 KB |
Output is correct |
10 |
Correct |
20 ms |
47280 KB |
Output is correct |
11 |
Correct |
20 ms |
47296 KB |
Output is correct |
12 |
Correct |
22 ms |
47860 KB |
Output is correct |
13 |
Correct |
23 ms |
47948 KB |
Output is correct |
14 |
Correct |
20 ms |
47316 KB |
Output is correct |
15 |
Correct |
23 ms |
47828 KB |
Output is correct |
16 |
Correct |
23 ms |
47956 KB |
Output is correct |
17 |
Correct |
22 ms |
47848 KB |
Output is correct |
18 |
Correct |
21 ms |
47472 KB |
Output is correct |
19 |
Correct |
22 ms |
47768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
106192 KB |
Output is correct |
2 |
Correct |
187 ms |
105864 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
95 ms |
58600 KB |
Output is correct |
5 |
Correct |
141 ms |
82064 KB |
Output is correct |
6 |
Correct |
261 ms |
105536 KB |
Output is correct |
7 |
Correct |
264 ms |
102524 KB |
Output is correct |
8 |
Correct |
278 ms |
104616 KB |
Output is correct |
9 |
Correct |
447 ms |
109412 KB |
Output is correct |
10 |
Correct |
324 ms |
104212 KB |
Output is correct |
11 |
Correct |
391 ms |
108824 KB |
Output is correct |
12 |
Correct |
518 ms |
104172 KB |
Output is correct |
13 |
Correct |
374 ms |
105764 KB |
Output is correct |
14 |
Correct |
355 ms |
106052 KB |
Output is correct |
15 |
Correct |
317 ms |
106116 KB |
Output is correct |
16 |
Correct |
301 ms |
105912 KB |
Output is correct |
17 |
Correct |
127 ms |
71648 KB |
Output is correct |
18 |
Correct |
244 ms |
97852 KB |
Output is correct |
19 |
Correct |
190 ms |
106188 KB |
Output is correct |
20 |
Correct |
261 ms |
106824 KB |
Output is correct |
21 |
Correct |
251 ms |
99840 KB |
Output is correct |
22 |
Correct |
383 ms |
107468 KB |
Output is correct |
23 |
Correct |
545 ms |
110200 KB |
Output is correct |
24 |
Correct |
375 ms |
107112 KB |
Output is correct |
25 |
Correct |
371 ms |
107344 KB |
Output is correct |
26 |
Correct |
332 ms |
107188 KB |
Output is correct |
27 |
Correct |
296 ms |
106660 KB |
Output is correct |
28 |
Correct |
141 ms |
70256 KB |
Output is correct |
29 |
Correct |
88 ms |
61528 KB |
Output is correct |
30 |
Correct |
77 ms |
62128 KB |
Output is correct |
31 |
Correct |
185 ms |
103796 KB |
Output is correct |
32 |
Correct |
195 ms |
107124 KB |
Output is correct |
33 |
Correct |
21 ms |
47896 KB |
Output is correct |
34 |
Correct |
22 ms |
47828 KB |
Output is correct |
35 |
Correct |
21 ms |
47788 KB |
Output is correct |
36 |
Correct |
23 ms |
47828 KB |
Output is correct |
37 |
Correct |
23 ms |
47876 KB |
Output is correct |
38 |
Correct |
23 ms |
47940 KB |
Output is correct |
39 |
Correct |
23 ms |
47844 KB |
Output is correct |
40 |
Correct |
26 ms |
47740 KB |
Output is correct |
41 |
Correct |
26 ms |
47572 KB |
Output is correct |
42 |
Correct |
20 ms |
47280 KB |
Output is correct |
43 |
Correct |
20 ms |
47296 KB |
Output is correct |
44 |
Correct |
22 ms |
47860 KB |
Output is correct |
45 |
Correct |
23 ms |
47948 KB |
Output is correct |
46 |
Correct |
20 ms |
47316 KB |
Output is correct |
47 |
Correct |
23 ms |
47828 KB |
Output is correct |
48 |
Correct |
23 ms |
47956 KB |
Output is correct |
49 |
Correct |
22 ms |
47848 KB |
Output is correct |
50 |
Correct |
21 ms |
47472 KB |
Output is correct |
51 |
Correct |
22 ms |
47768 KB |
Output is correct |
52 |
Correct |
207 ms |
106132 KB |
Output is correct |
53 |
Correct |
267 ms |
106840 KB |
Output is correct |
54 |
Correct |
265 ms |
99972 KB |
Output is correct |
55 |
Correct |
387 ms |
107408 KB |
Output is correct |
56 |
Correct |
537 ms |
110276 KB |
Output is correct |
57 |
Correct |
379 ms |
107080 KB |
Output is correct |
58 |
Correct |
362 ms |
107360 KB |
Output is correct |
59 |
Correct |
339 ms |
107400 KB |
Output is correct |
60 |
Correct |
151 ms |
72340 KB |
Output is correct |
61 |
Correct |
74 ms |
61660 KB |
Output is correct |
62 |
Correct |
75 ms |
62180 KB |
Output is correct |
63 |
Correct |
185 ms |
103968 KB |
Output is correct |
64 |
Correct |
190 ms |
105560 KB |
Output is correct |
65 |
Correct |
103 ms |
64160 KB |
Output is correct |
66 |
Correct |
529 ms |
106216 KB |
Output is correct |
67 |
Correct |
372 ms |
107928 KB |
Output is correct |
68 |
Correct |
353 ms |
106168 KB |
Output is correct |
69 |
Correct |
128 ms |
70896 KB |
Output is correct |
70 |
Correct |
204 ms |
101180 KB |
Output is correct |