Submission #782582

#TimeUsernameProblemLanguageResultExecution timeMemory
782582devariaotaNaboj (COCI22_naboj)C++17
110 / 110
680 ms22704 KiB
#include<bits/stdc++.h> using namespace std; #define ioss ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define pii pair<int, int> #define fi first #define se second #define pb push_back int n, m; vector<int> adj[200004]; bool vis[200004]; int cnt[200004]; queue<int> q; void toposort(int u) { if(vis[u]) return; q.push(u); vis[u] = 1; for(auto v : adj[u]) cnt[v]--; for(auto v : adj[u]) { if(!vis[v] && cnt[v] == 0) toposort(v); } } int main() { ioss; cin >> n >> m; set<int> tmp; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; tmp.insert(a); adj[b].pb(a); cnt[a]++; } for(int i = 1; i <= n; i++) { if(!vis[i] && cnt[i] == 0) toposort(i); // cout << cnt[i] << endl; } bool ok = 1; for(int i = 1; i <= n; i++) { if(!vis[i]) { ok = 0; break; } } if(!ok) { cout << -1 << endl; return 0; } cout << q.size() << endl; while(!q.empty()) { int u = q.front(); q.pop(); if(tmp.count(u)) cout << u << " " << 1 << endl; else cout << u << " " << 0 << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...