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 fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;
vector<vector<pair<int,int>>> graph;
int indeg[MAXN], outdeg[MAXN], vis[MAXN];
int main() {
fast
int n, m, a, b;
cin >> n >> m;
graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
for (int i = 0; i < m; i++) {
cin >> a >> b;
graph[a].push_back({b, 0});
graph[b].push_back({a, 1});
indeg[a]++;
outdeg[b]++;
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if (indeg[i] == 0 || outdeg[i] == 0)
q.push(i);
}
stack<pair<int,int>> ans;
while (!q.empty()) {
int node = q.front();
vis[node] = true;
q.pop();
int state = -1;
for (auto u : graph[node]) {
if (vis[u.f])
continue;
if (u.s == 1)
indeg[u.f]--;
else
outdeg[u.f]--;
if (indeg[u.f] == 0 || outdeg[u.f] == 0)
q.push(u.f);
state = u.s;
}
if (state != -1)
ans.push({node, !state});
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "-1\n";
return 0;
}
}
cout << ans.size() << "\n";
while (!ans.empty()) {
cout << ans.top().f << " " << ans.top().s << "\n";
ans.pop();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |