#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 N = 2e5 + 1;
int n, m, c[N], c1[N], cnt0[N], cnt1[N], cnt = 0;
vi adj0[N], adj1[N];
pi e[N];
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:54:5: note: in expansion of macro 'FOR'
54 | 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:67:6: note: in expansion of macro 'EACH'
67 | 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:77:6: note: in expansion of macro 'EACH'
77 | 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:94:5: note: in expansion of macro 'FOR'
94 | 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 |
4 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 |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 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 |
6 ms |
9732 KB |
Output is correct |
12 |
Correct |
5 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 |
248 ms |
32420 KB |
Output is correct |
2 |
Correct |
238 ms |
35016 KB |
Output is correct |
3 |
Correct |
147 ms |
22924 KB |
Output is correct |
4 |
Correct |
243 ms |
34976 KB |
Output is correct |
5 |
Correct |
260 ms |
34952 KB |
Output is correct |
6 |
Correct |
262 ms |
34988 KB |
Output is correct |
7 |
Correct |
294 ms |
34892 KB |
Output is correct |
8 |
Correct |
171 ms |
29300 KB |
Output is correct |
9 |
Correct |
235 ms |
34988 KB |
Output is correct |
10 |
Correct |
263 ms |
34876 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 |
4 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 |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 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 |
6 ms |
9732 KB |
Output is correct |
12 |
Correct |
5 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 |
248 ms |
32420 KB |
Output is correct |
16 |
Correct |
238 ms |
35016 KB |
Output is correct |
17 |
Correct |
147 ms |
22924 KB |
Output is correct |
18 |
Correct |
243 ms |
34976 KB |
Output is correct |
19 |
Correct |
260 ms |
34952 KB |
Output is correct |
20 |
Correct |
262 ms |
34988 KB |
Output is correct |
21 |
Correct |
294 ms |
34892 KB |
Output is correct |
22 |
Correct |
171 ms |
29300 KB |
Output is correct |
23 |
Correct |
235 ms |
34988 KB |
Output is correct |
24 |
Correct |
263 ms |
34876 KB |
Output is correct |
25 |
Runtime error |
106 ms |
38532 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |