#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
const int M = 5e5 + 5;
const int N = 2e5 + 5;
int n, m, c[N], c1[N], cnt0[N], cnt1[N], cnt = 0;
vi adj0[N], adj1[N];
pi e[M];
multiset<int> s;
vpi ans;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> m;
WHILE(m--) {
int a, b;
cin >> a >> b;
e[++cnt] = {a, b};
adj1[a].pb(cnt);
adj0[b].pb(cnt);
}
FOR(i, 1, n) {
cnt0[i] = sz(adj0[i]);
cnt1[i] = sz(adj1[i]);
if (!cnt0[i] || !cnt1[i]) {
s.insert(i);
c[i] = 1;
}
}
WHILE(!s.empty()) {
int u = *s.begin();
ans.pb({u, cnt1[u]? 1 : 0});
s.erase(s.begin());
c[u] = 1;
EACH(i, adj0[u]) {
if (c1[i]) continue;
int v = e[i].st;
--cnt1[v];
if (!c[v] && !cnt1[v]) {
s.insert(v);
c[v] = 1;
}
c1[i] = 1;
}
EACH(i, adj1[u]) {
if (c1[i]) continue;
int v = e[i].nd;
--cnt0[v];
if (!c[v] && !cnt0[v]) {
s.insert(v);
c[v] = 1;
}
c1[i] = 1;
}
}
reverse(all(ans));
if (sz(ans) < n) {
cout << -1;
return 0;
}
cout << n << '\n';
FOR(i, 0, n - 1) cout << ans[i].st << ' ' << ans[i].nd << '\n';
return 0;
}
/*
5 10
2 4
3 4
1 4
4 5
3 2
2 1
5 2
1 3
5 3
1 5
*/
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
naboj.cpp:55:5: note: in expansion of macro 'FOR'
55 | FOR(i, 1, n) {
| ^~~
naboj.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
naboj.cpp:68:6: note: in expansion of macro 'EACH'
68 | EACH(i, adj0[u]) {
| ^~~~
naboj.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
naboj.cpp:78:6: note: in expansion of macro 'EACH'
78 | EACH(i, adj1[u]) {
| ^~~~
naboj.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
naboj.cpp:95:5: note: in expansion of macro 'FOR'
95 | FOR(i, 0, n - 1) cout << ans[i].st << ' ' << ans[i].nd << '\n';
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
32488 KB |
Output is correct |
2 |
Correct |
232 ms |
32424 KB |
Output is correct |
3 |
Correct |
116 ms |
21720 KB |
Output is correct |
4 |
Correct |
227 ms |
32468 KB |
Output is correct |
5 |
Correct |
261 ms |
32488 KB |
Output is correct |
6 |
Correct |
240 ms |
32580 KB |
Output is correct |
7 |
Correct |
273 ms |
32404 KB |
Output is correct |
8 |
Correct |
178 ms |
27392 KB |
Output is correct |
9 |
Correct |
266 ms |
32412 KB |
Output is correct |
10 |
Correct |
256 ms |
32444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
248 ms |
32488 KB |
Output is correct |
16 |
Correct |
232 ms |
32424 KB |
Output is correct |
17 |
Correct |
116 ms |
21720 KB |
Output is correct |
18 |
Correct |
227 ms |
32468 KB |
Output is correct |
19 |
Correct |
261 ms |
32488 KB |
Output is correct |
20 |
Correct |
240 ms |
32580 KB |
Output is correct |
21 |
Correct |
273 ms |
32404 KB |
Output is correct |
22 |
Correct |
178 ms |
27392 KB |
Output is correct |
23 |
Correct |
266 ms |
32412 KB |
Output is correct |
24 |
Correct |
256 ms |
32444 KB |
Output is correct |
25 |
Runtime error |
211 ms |
48988 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |