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;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 2e5+5;
int n, m, in[N];
vector<int> adj[N];
vector<pair<int, int>> ans;
int cnt = 0;
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].pb(v);
in[v]++;
}
queue<int> q;
for (int i = 0; i < n; i++) {
if (!in[i])q.push(i);
}
while (!q.empty()) {
int u = q.front();
q.pop();
cnt++;
for (auto &v : adj[u]) {
if (--in[v] == 0)q.push(v);
}
if (adj[u].empty())ans.pb({u, 0});
else ans.pb({u, 1});
}
if (cnt != n) {
cout << -1;
return 0;
}
cout << sz(ans) << "\n";
reverse(all(ans));
for (auto &[x, y] : ans)cout << x+1 << " " << y << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |