#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define uh pair<int,pii>
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
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<bool> sequenced;
vector<vector<int>> ano;
queue<int> q;
vector<int> bfsfront(int x);
vector<int> bfsback(int x) {
vector<int> vv;
vv.pb(x);
q.push(x);
while(!q.empty()) {
int cur = q.front(); q.pop();
sequenced[cur] = 1;
for (auto item : opp[cur]) {
if (!sequenced[item] and opp[item].size()) {
if (adj[item].size() > 1) {
vector<int> neww;
for (auto item : bfsfront(item)) {
neww.pb(item);
}
reverse(neww.begin(), neww.end());
for (auto item2 : neww) vv.pb(item2);
q.push(item);
} else {
q.push(item);
vv.pb(item);
}
}
}
}
return vv;
}
vector<int> bfsfront(int x) {
vector<int> vv;
vv.pb(x);
q.push(x);
while(!q.empty()) {
int cur = q.front(); q.pop();
sequenced[cur] = 1;
for (auto item : adj[cur]) {
if (!sequenced[item] and adj[item].size()) {
if(opp[item].size() > 1) {
vector<int> neww;
for (auto item : bfsback(item)) {
neww.pb(item);
}
reverse(neww.begin(), neww.end());
for (auto item2 : neww) vv.pb(item2);
q.push(item);
} else {
q.push(item);
vv.pb(item);
}
}
}
}
return vv;
}
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);
sequenced.assign(n+1, 0);
// if going forward then
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 (opp[i].size() and adj[i].size() and !sequenced[i]) {
vector<int> v;
for (auto item : bfsback(i)) {
v.pb(item);
}
reverse(all(v));
v.pop_back();
for (auto item : bfsfront(i)) {
v.pb(item);
}
ano.pb(v);
}
}
cout << n << endl;
for (auto vec : ano) {
for (auto item : vec) cout << item << " " << 1 << endl;
}
for (int i = n; i >= 1; i--) {
if (!sequenced[i]) {
cout << i << " " << (adj[i].size() == 0) << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
414 ms |
24836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |