#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2e5+5;
int in[N];
vector<int> g[N];
vector<pll> ans;
queue<int> Q;
int main(void)
{ IO
int n, i, m;
cin >> n >> m;
for (i=0; i < m; ++i)
{
int x, y; cin >> y >> x;
g[x].pb(y), ++in[y];
}
int cnt = n;
for (i=1; i <= n; ++i)
{
if (!in[i]) Q.push(i);
}
while (!Q.empty())
{
int x = Q.front(); Q.pop();
--cnt;
ans.pb(x, 0);
for (int v : g[x])
{
--in[v];
if (!in[v]) Q.push(v);
}
}
if (cnt > 0) cout << "-1\n";
else {
cout << SZ(ans) << '\n';
reverse(all(ans));
for (auto p : ans) cout << p.F << ' ' << p.S << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5160 KB |
Output is correct |
3 |
Correct |
1 ms |
5164 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 |
5112 KB |
Output is correct |
8 |
Correct |
1 ms |
5160 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
16004 KB |
Output is correct |
2 |
Correct |
68 ms |
16068 KB |
Output is correct |
3 |
Correct |
35 ms |
10440 KB |
Output is correct |
4 |
Correct |
68 ms |
16476 KB |
Output is correct |
5 |
Correct |
72 ms |
16332 KB |
Output is correct |
6 |
Correct |
69 ms |
15224 KB |
Output is correct |
7 |
Correct |
67 ms |
15820 KB |
Output is correct |
8 |
Correct |
70 ms |
14192 KB |
Output is correct |
9 |
Correct |
67 ms |
15304 KB |
Output is correct |
10 |
Correct |
90 ms |
16376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5160 KB |
Output is correct |
3 |
Correct |
1 ms |
5164 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 |
5112 KB |
Output is correct |
8 |
Correct |
1 ms |
5160 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
77 ms |
16004 KB |
Output is correct |
16 |
Correct |
68 ms |
16068 KB |
Output is correct |
17 |
Correct |
35 ms |
10440 KB |
Output is correct |
18 |
Correct |
68 ms |
16476 KB |
Output is correct |
19 |
Correct |
72 ms |
16332 KB |
Output is correct |
20 |
Correct |
69 ms |
15224 KB |
Output is correct |
21 |
Correct |
67 ms |
15820 KB |
Output is correct |
22 |
Correct |
70 ms |
14192 KB |
Output is correct |
23 |
Correct |
67 ms |
15304 KB |
Output is correct |
24 |
Correct |
90 ms |
16376 KB |
Output is correct |
25 |
Correct |
87 ms |
15180 KB |
Output is correct |
26 |
Correct |
67 ms |
16920 KB |
Output is correct |
27 |
Correct |
79 ms |
17356 KB |
Output is correct |
28 |
Correct |
92 ms |
18456 KB |
Output is correct |
29 |
Correct |
58 ms |
19932 KB |
Output is correct |
30 |
Correct |
89 ms |
18392 KB |
Output is correct |
31 |
Correct |
15 ms |
7004 KB |
Output is correct |
32 |
Correct |
78 ms |
21448 KB |
Output is correct |
33 |
Correct |
119 ms |
24004 KB |
Output is correct |
34 |
Correct |
81 ms |
20680 KB |
Output is correct |
35 |
Correct |
126 ms |
23288 KB |
Output is correct |
36 |
Correct |
76 ms |
20932 KB |
Output is correct |
37 |
Correct |
123 ms |
23500 KB |
Output is correct |
38 |
Correct |
120 ms |
22772 KB |
Output is correct |
39 |
Correct |
118 ms |
23092 KB |
Output is correct |
40 |
Correct |
111 ms |
23240 KB |
Output is correct |
41 |
Correct |
111 ms |
22104 KB |
Output is correct |
42 |
Correct |
134 ms |
24776 KB |
Output is correct |
43 |
Correct |
98 ms |
22136 KB |
Output is correct |
44 |
Correct |
131 ms |
24256 KB |
Output is correct |
45 |
Correct |
79 ms |
20424 KB |
Output is correct |
46 |
Correct |
115 ms |
22728 KB |
Output is correct |