제출 #782581

#제출 시각아이디문제언어결과실행 시간메모리
782581devariaotaNaboj (COCI22_naboj)C++17
110 / 110
657 ms24116 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> st; void toposort(int u) { if(vis[u]) return; vis[u] = 1; st.push(u); // cout << "::" << u << endl; for(auto v : adj[u]) { // cout << v << endl; cnt[v]--; } for(auto v : adj[u]) { if(!vis[v] && cnt[v] == 0) toposort(v); } // cout << u << endl; } 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 << st.size() << endl; while(!st.empty()) { int u = st.front(); st.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...