#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 + 1;
const int N = 2e5 + 1;
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';
| ^~~
# |
Verdict |
Execution time |
Memory |
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 |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 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 |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
32612 KB |
Output is correct |
2 |
Correct |
275 ms |
32468 KB |
Output is correct |
3 |
Correct |
103 ms |
21796 KB |
Output is correct |
4 |
Correct |
252 ms |
32520 KB |
Output is correct |
5 |
Correct |
237 ms |
32544 KB |
Output is correct |
6 |
Correct |
233 ms |
32456 KB |
Output is correct |
7 |
Correct |
238 ms |
32368 KB |
Output is correct |
8 |
Correct |
181 ms |
27460 KB |
Output is correct |
9 |
Correct |
250 ms |
32468 KB |
Output is correct |
10 |
Correct |
233 ms |
32620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 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 |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
233 ms |
32612 KB |
Output is correct |
16 |
Correct |
275 ms |
32468 KB |
Output is correct |
17 |
Correct |
103 ms |
21796 KB |
Output is correct |
18 |
Correct |
252 ms |
32520 KB |
Output is correct |
19 |
Correct |
237 ms |
32544 KB |
Output is correct |
20 |
Correct |
233 ms |
32456 KB |
Output is correct |
21 |
Correct |
238 ms |
32368 KB |
Output is correct |
22 |
Correct |
181 ms |
27460 KB |
Output is correct |
23 |
Correct |
250 ms |
32468 KB |
Output is correct |
24 |
Correct |
233 ms |
32620 KB |
Output is correct |
25 |
Runtime error |
222 ms |
51236 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |