# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
782195 | devariaota | Naboj (COCI22_naboj) | C++17 | 519 ms | 40088 KiB |
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 sherina ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define int long long
#define INF 1e18
using namespace std;
int n , m , a[500005] , b[500005] , jmlhbapa[500005];
vector < int > adj[500005];
vector < pair < int , int > > ans;
void dfs(){
queue < int > q;
for(int i = 1 ; i <= n ; i++){
if(jmlhbapa[i] == 0){
q.push(i);
ans.pb(make_pair(i , 0));
}
}
while(!q.empty()){
int u = q.front(); q.pop();
//cout << u << endl;
for(int i : adj[u]){
jmlhbapa[i]--;
if(jmlhbapa[i] == 0){
ans.pb(make_pair(i , 0));
q.push(i);
}
}
}
}
signed main(){
cin >> n >> m;
for(int i = 1 ; i <= m ; i++){
cin >> a[i] >> b[i];
adj[a[i]].pb(b[i]);
jmlhbapa[b[i]]++;
}
dfs();
bool bs = true;
for(int i = 1 ; i <= n ; i++){
if(jmlhbapa[i] > 0) bs = false;
}
if(!bs){
cout << "-1" << endl;
return 0;
}
cout << ans.size() << endl;
for(int i = 0 ; i < ans.size() ; i++){
cout << ans[i].first << " " << ans[i].second << endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |