// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 2e5+5;
int n, m, cnt[maxn];
vector <int> adj[maxn], vec;
queue <int> q;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
adj[--b].pb(--a);
cnt[a]++;
}
for (int i = 0; i < n; i++) {
if (!cnt[i]) q.push(i);
}
while (q.size()) {
int v = q.front(); q.pop();
vec.pb(v);
for (int u : adj[v]) {
cnt[u]--;
if (!cnt[u]) q.push(u);
}
}
if (vec.size() != n) cout << -1 << '\n';
else {
cout << n << '\n';
for (int v : vec) cout << v+1 << ' ' << 1 << '\n';
}
return 0;
}
Compilation message
naboj.cpp: In function 'int32_t main()':
naboj.cpp:42:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
42 | if (vec.size() != n) cout << -1 << '\n';
| ~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
12952 KB |
Output is correct |
2 |
Correct |
69 ms |
13004 KB |
Output is correct |
3 |
Correct |
33 ms |
9192 KB |
Output is correct |
4 |
Correct |
66 ms |
13004 KB |
Output is correct |
5 |
Correct |
70 ms |
13004 KB |
Output is correct |
6 |
Correct |
82 ms |
13112 KB |
Output is correct |
7 |
Correct |
69 ms |
12996 KB |
Output is correct |
8 |
Correct |
51 ms |
11208 KB |
Output is correct |
9 |
Correct |
69 ms |
13000 KB |
Output is correct |
10 |
Correct |
66 ms |
13000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4952 KB |
Output is correct |
15 |
Correct |
79 ms |
12952 KB |
Output is correct |
16 |
Correct |
69 ms |
13004 KB |
Output is correct |
17 |
Correct |
33 ms |
9192 KB |
Output is correct |
18 |
Correct |
66 ms |
13004 KB |
Output is correct |
19 |
Correct |
70 ms |
13004 KB |
Output is correct |
20 |
Correct |
82 ms |
13112 KB |
Output is correct |
21 |
Correct |
69 ms |
12996 KB |
Output is correct |
22 |
Correct |
51 ms |
11208 KB |
Output is correct |
23 |
Correct |
69 ms |
13000 KB |
Output is correct |
24 |
Correct |
66 ms |
13000 KB |
Output is correct |
25 |
Correct |
80 ms |
10320 KB |
Output is correct |
26 |
Correct |
65 ms |
11724 KB |
Output is correct |
27 |
Correct |
75 ms |
11860 KB |
Output is correct |
28 |
Correct |
91 ms |
12116 KB |
Output is correct |
29 |
Correct |
54 ms |
11736 KB |
Output is correct |
30 |
Correct |
88 ms |
12116 KB |
Output is correct |
31 |
Correct |
18 ms |
5976 KB |
Output is correct |
32 |
Correct |
79 ms |
14956 KB |
Output is correct |
33 |
Correct |
125 ms |
15212 KB |
Output is correct |
34 |
Correct |
81 ms |
15052 KB |
Output is correct |
35 |
Correct |
136 ms |
15116 KB |
Output is correct |
36 |
Correct |
89 ms |
15052 KB |
Output is correct |
37 |
Correct |
109 ms |
15152 KB |
Output is correct |
38 |
Correct |
98 ms |
15048 KB |
Output is correct |
39 |
Correct |
120 ms |
15048 KB |
Output is correct |
40 |
Correct |
117 ms |
15304 KB |
Output is correct |
41 |
Correct |
107 ms |
15044 KB |
Output is correct |
42 |
Correct |
125 ms |
15060 KB |
Output is correct |
43 |
Correct |
88 ms |
15056 KB |
Output is correct |
44 |
Correct |
151 ms |
15220 KB |
Output is correct |
45 |
Correct |
83 ms |
15056 KB |
Output is correct |
46 |
Correct |
109 ms |
14952 KB |
Output is correct |