This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 (stderr)
Main.cpp: In function 'int main()':
Main.cpp:71:13: warning: unused variable 'startTime' [-Wunused-variable]
71 | clock_t startTime = clock();
| ^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |