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 ll long long
#define fi first
#define se second
#define endl "\n"
#define pii pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define pque priority_queue
#define pqueg priority_queue<ll,vector<ll>,greater<ll>>
#define que queue<ll>
#define FOR(m,i,n) for(int i=(m); i<=(n); i++)
#define FORM(m,i,n) for(int i=(m); i>=(n); i--)
ll n,m,u,v;
bool vis[200200];
vector<ll> adj[200200],ans;
int top[200200];
set<ll> st;
void dfs(ll x) {
vis[x] = true;
for(auto i : adj[x]) {
top[i]--;
if(top[i] == 0 && !vis[i]) {
ans.pb(i);
dfs(i);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
FOR(1,i,m) {
cin >> u >> v;
st.insert(u);
adj[v].pb(u);
top[u]++;
}
FOR(1,i,n) {
if(top[i] == 0) {
dfs(i);
}
}
if(ans.size() != st.size()) {
cout << -1 << endl;
return 0;
}
for(auto i : ans) {
cout << i << " 1" << endl;
}
}
/*
5 5
3 1
2 3
4 2
5 2
5 4
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |