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>
#define int long long
#define fi first
#define se second
#define keish ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int n, m, u, v;
signed main(){
keish;
cin >> n >> m;
vector<vector<int>> e(n);
vector<int> deg(n);
for(int i = 0; i < m; i++){
cin >> u >> v; u--, v--;
e[v].push_back(u);
deg[u]++;
}
queue<int> q;
vector<int> ans;
for(int i = 0; i < n; i++){
if(!deg[i]){
q.push(i);
ans.push_back(i);
}
}
while(!q.empty()){
int u = q.front(); q.pop();
for(auto v : e[u]){
deg[v]--;
if(!deg[v]){
q.push(v);
ans.push_back(v);
}
}
}
for(int i = 0; i < n; i++){
if(deg[i]){
cout << -1 << '\n';
return 0;
}
}
cout << ans.size() << '\n';
for(auto x : ans) cout << x + 1 << " " << 1 << '\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... |