#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000
#define mod 998244353
set<int> s;
vector<set<int>> v, vr;
void check(int x){
if ((v[x].size() + vr[x].size()) == 0) return;
if (v[x].size() == 0) s.insert(x);
if (vr[x].size() == 0) s.insert(x);
}
int main(){
int n, m, a, b;
cin>>n>>m;
v.assign(n + 1, set<int>()), vr.assign(n + 1, set<int>());
for (int i = 0; i < m; i++){
cin>>a>>b;
v[b].insert(a);
vr[a].insert(b);
}
for (int i = 1; i <= n; i++){
check(i);
}
vector<vector<int>> ord;
while (!s.empty()){
int x = *s.begin();
s.erase(s.begin());
if ((v[x].size() + vr[x].size()) == 0) continue;
if (v[x].size() == 0){
ord.push_back({x, 1});
for (auto el : vr[x]){
v[el].erase(x);
check(el);
}
vr[x].clear();
}
else{
ord.push_back({x, 0});
for (auto el : v[x]){
vr[el].erase(x);
check(el);
}
v[x].clear();
}
}
bool can = 1;
for (int i = 1; i <= n; i++){
if (v[i].size() != 0 || vr[i].size() != 0){
can = 0;
}
}
if (can){
reverse(ord.begin(), ord.end());
cout<<ord.size()<<"\n";
for (auto el : ord){
cout<<el[0]<<" "<<el[1]<<"\n";
}
}
else{
cout<<-1;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |