This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |