#include <bits/stdc++.h>
#define ll long long
#define vc vector
using namespace std;
int main() {
ll n, m;cin >> n >> m;
vc<vc<ll>> out(n+1), in(n+1);
for(ll i=0;i<m;++i){
ll a, b;cin >> a >> b;
out[a].push_back(b);
in[b].push_back(a);
}
vc<ll> cntin(n+1, 0), cntout(n+1, 0);
for(ll i=1;i<=n;++i) cntin[i]=in[i].size(), cntout[i]=out[i].size();
queue<ll> q;
vc<ll> vis(n+1, 0), ch(n+1, 0);
for(ll i=1;i<=n;++i) if(cntin[i]==0 or cntout[i]==0) vis[i]=1, ch[i]=(cntin[i]==0)? 1LL:0LL, q.push(i);//, cout <<i << " ";cout << endl;
vc<ll> ans;
while(q.size()){
ll u=q.front();q.pop();
ans.emplace_back(u);
for(auto v: in[u])if(!vis[v]){
--cntout[v];
if(cntin[v]==0 or cntout[v]==0) q.push(v), ch[v]=(cntin[v]==0)? 1LL:0LL, vis[v]=1;
}
for(auto v: out[u])if(!vis[v]){
--cntin[v];
if(cntin[v]==0 or cntout[v]==0) q.push(v), ch[v]=(cntout[v]==0)? 0LL:1LL, vis[v]=1;
}
}
if(ans.size()==n){
cout << ans.size()<< endl;
reverse(ans.begin(), ans.end());
for(auto v: ans) cout << v << " " << ch[v] <<'\n';
}
else cout << -1 <<endl;
}
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:35:18: 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]
35 | if(ans.size()==n){
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
31848 KB |
Output is correct |
2 |
Correct |
260 ms |
31792 KB |
Output is correct |
3 |
Correct |
157 ms |
16900 KB |
Output is correct |
4 |
Correct |
283 ms |
31884 KB |
Output is correct |
5 |
Correct |
263 ms |
31824 KB |
Output is correct |
6 |
Correct |
283 ms |
31840 KB |
Output is correct |
7 |
Correct |
270 ms |
31844 KB |
Output is correct |
8 |
Correct |
204 ms |
24868 KB |
Output is correct |
9 |
Correct |
329 ms |
31872 KB |
Output is correct |
10 |
Correct |
279 ms |
31824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
275 ms |
31848 KB |
Output is correct |
16 |
Correct |
260 ms |
31792 KB |
Output is correct |
17 |
Correct |
157 ms |
16900 KB |
Output is correct |
18 |
Correct |
283 ms |
31884 KB |
Output is correct |
19 |
Correct |
263 ms |
31824 KB |
Output is correct |
20 |
Correct |
283 ms |
31840 KB |
Output is correct |
21 |
Correct |
270 ms |
31844 KB |
Output is correct |
22 |
Correct |
204 ms |
24868 KB |
Output is correct |
23 |
Correct |
329 ms |
31872 KB |
Output is correct |
24 |
Correct |
279 ms |
31824 KB |
Output is correct |
25 |
Correct |
396 ms |
29128 KB |
Output is correct |
26 |
Correct |
351 ms |
33900 KB |
Output is correct |
27 |
Correct |
390 ms |
35392 KB |
Output is correct |
28 |
Correct |
538 ms |
38896 KB |
Output is correct |
29 |
Correct |
309 ms |
32192 KB |
Output is correct |
30 |
Correct |
502 ms |
38484 KB |
Output is correct |
31 |
Correct |
55 ms |
5340 KB |
Output is correct |
32 |
Correct |
301 ms |
33348 KB |
Output is correct |
33 |
Correct |
533 ms |
40512 KB |
Output is correct |
34 |
Correct |
304 ms |
33904 KB |
Output is correct |
35 |
Correct |
490 ms |
40668 KB |
Output is correct |
36 |
Correct |
287 ms |
33280 KB |
Output is correct |
37 |
Correct |
458 ms |
38960 KB |
Output is correct |
38 |
Correct |
416 ms |
36660 KB |
Output is correct |
39 |
Correct |
488 ms |
40328 KB |
Output is correct |
40 |
Correct |
440 ms |
37932 KB |
Output is correct |
41 |
Correct |
440 ms |
37764 KB |
Output is correct |
42 |
Correct |
556 ms |
41512 KB |
Output is correct |
43 |
Correct |
335 ms |
34504 KB |
Output is correct |
44 |
Correct |
563 ms |
41904 KB |
Output is correct |
45 |
Correct |
296 ms |
33768 KB |
Output is correct |
46 |
Correct |
438 ms |
38016 KB |
Output is correct |