#include <bits/stdc++.h>
using namespace std;
# define int long long
# define fir first
# define sec second
# define pb push_back
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
int x[cnst];
vector<int> vec[cnst];
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i<=m; i++) {
int a, b; cin >> a >> b;
vec[b].pb(a);
x[a]++;
}
queue<int> q;
int done = 0;
for(int i = 1; i<=n; i++) {
if(!x[i]) q.push(i), done++;
}
vector<pair<int, int>> ans;
while(!q.empty()) {
int a = q.front(); q.pop();
for(auto v: vec[a]) {
x[v]--;
if(!x[v]) {
q.push(v);
ans.pb({v, 1});
done++;
}
}
if(done == n) break;
}
if(done == n) {
cout << ans.size() << endl;
for(auto v: ans) cout << v.fir << " " << v.sec << endl;
}
else cout << -1 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
15784 KB |
Output is correct |
2 |
Correct |
202 ms |
15784 KB |
Output is correct |
3 |
Correct |
110 ms |
10696 KB |
Output is correct |
4 |
Correct |
196 ms |
15808 KB |
Output is correct |
5 |
Correct |
195 ms |
15744 KB |
Output is correct |
6 |
Correct |
199 ms |
15808 KB |
Output is correct |
7 |
Correct |
191 ms |
15780 KB |
Output is correct |
8 |
Correct |
156 ms |
12708 KB |
Output is correct |
9 |
Correct |
193 ms |
15800 KB |
Output is correct |
10 |
Correct |
199 ms |
15812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
192 ms |
15784 KB |
Output is correct |
16 |
Correct |
202 ms |
15784 KB |
Output is correct |
17 |
Correct |
110 ms |
10696 KB |
Output is correct |
18 |
Correct |
196 ms |
15808 KB |
Output is correct |
19 |
Correct |
195 ms |
15744 KB |
Output is correct |
20 |
Correct |
199 ms |
15808 KB |
Output is correct |
21 |
Correct |
191 ms |
15780 KB |
Output is correct |
22 |
Correct |
156 ms |
12708 KB |
Output is correct |
23 |
Correct |
193 ms |
15800 KB |
Output is correct |
24 |
Correct |
199 ms |
15812 KB |
Output is correct |
25 |
Correct |
97 ms |
12704 KB |
Output is correct |
26 |
Correct |
82 ms |
13700 KB |
Output is correct |
27 |
Correct |
119 ms |
13984 KB |
Output is correct |
28 |
Correct |
106 ms |
14928 KB |
Output is correct |
29 |
Correct |
62 ms |
14168 KB |
Output is correct |
30 |
Correct |
138 ms |
14824 KB |
Output is correct |
31 |
Correct |
22 ms |
6572 KB |
Output is correct |
32 |
Correct |
183 ms |
16360 KB |
Output is correct |
33 |
Correct |
283 ms |
19136 KB |
Output is correct |
34 |
Correct |
208 ms |
16460 KB |
Output is correct |
35 |
Correct |
306 ms |
19184 KB |
Output is correct |
36 |
Correct |
170 ms |
16304 KB |
Output is correct |
37 |
Correct |
265 ms |
18684 KB |
Output is correct |
38 |
Correct |
226 ms |
18160 KB |
Output is correct |
39 |
Correct |
273 ms |
19028 KB |
Output is correct |
40 |
Correct |
289 ms |
18476 KB |
Output is correct |
41 |
Correct |
274 ms |
18384 KB |
Output is correct |
42 |
Correct |
281 ms |
19392 KB |
Output is correct |
43 |
Correct |
212 ms |
16616 KB |
Output is correct |
44 |
Correct |
288 ms |
19564 KB |
Output is correct |
45 |
Correct |
216 ms |
16396 KB |
Output is correct |
46 |
Correct |
248 ms |
18376 KB |
Output is correct |