#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fr first
#define sc second
using namespace std;
const int N = 2e5+5;
const int M = 5e5+5;
vector<int> adj[N];
vector<int> opp[N];
vector<int> marker;
vector<int> par;
vector<int> keep;
vector<bool> vis;
bool dfs(int x) {
marker[x] = 1;
for (auto item : adj[x]) {
if (!marker[item]) {
par[item] = x;
if (dfs(item)) return true;
} else if (marker[item] == 1) return true;
}
marker[x] = 2;
return false;
}
signed main() {
int n, m; cin >> n >> m;
marker.assign(n+1, 0);
par.assign(n+1, -1);
vis.assign(n+1, 0);
vector<pii> v;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
adj[b].pb(a);
opp[a].pb(b);
}
for (int i = 1; i <= n; i++) {
if (!marker[i]) {
if (dfs(i)) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
queue<int> q;
q.push(i);
while(!q.empty()) {
int cur = q.front(); q.pop();
vis[cur] = 1;
if (adj[cur].size() == 0) {
for (auto item : opp[cur]) {
if (!vis[item]) {
q.push(item);
v.pb({item, 1});
}
}
} else if (opp[cur].size() == 0) {
for (auto item : adj[cur]) {
if (!vis[item]) {
q.push(item);
v.pb({item, 0});
}
}
}
else {
bool pushed = false;
for (auto item : adj[cur]) {
if (!vis[item]) {
pushed = true;
q.push(item);
v.pb({item, 0});
}
}
if (pushed) {
for (auto item : opp[cur]) {
q.push(item);
v.pb({item, 1});
}
}
}
}
}
}
cout << v.size() << endl;
for (auto item : v) cout << item.fr << " " << item.sc << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9616 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9604 KB |
Output is correct |
5 |
Correct |
5 ms |
9692 KB |
Output is correct |
6 |
Correct |
5 ms |
9700 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
409 ms |
27732 KB |
Integer parameter [name=k] equals to 213106, violates the range [-1, 200000] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9616 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9604 KB |
Output is correct |
5 |
Correct |
5 ms |
9692 KB |
Output is correct |
6 |
Correct |
5 ms |
9700 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |