#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define cy cout<<"YES"<<endl
#define cn cout<<"NO"<<endl
#define repp(i,n,k) for(int i=1;i<=n;i+=k)
#define repo(i,n,k) for(int i=0;i<n;i+=k)
#define pb push_back
using namespace std;
ll arr[100005];
vector<ll> adj[100005];
vector<ll> ans;
int main(){
ll m,n;cin>>m>>n;
queue<ll> q;
for(int i=1;i<=n;i++){
ll a,b;cin>>a>>b;
adj[a].push_back(b);
arr[b]++;
}
repp(i,n,1){
if(arr[i]==0) q.push(i);
}
while(!q.empty()){
ll cur=q.front();
ans.pb(cur);
q.pop();
for(ll nx:adj[cur]){
arr[nx]--;
if(arr[nx]==0) q.push(nx);
}
}
if(ans.size()!=m) cout<<"-1";
else {
cout<<ans.size()<<endl;
for(ll nx:ans){
cout<<nx<<" 0"<<endl;
}
}
}
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:36:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
36 | if(ans.size()!=m) cout<<"-1";
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2656 KB |
Output is correct |
4 |
Correct |
1 ms |
2656 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2656 KB |
Output is correct |
4 |
Correct |
1 ms |
2656 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |