Submission #635435

# Submission time Handle Problem Language Result Execution time Memory
635435 2022-08-26T09:06:11 Z ghostwriter Naboj (COCI22_naboj) C++14
110 / 110
427 ms 41536 KB
#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[M], 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 4 ms 9720 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 9708 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 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 32440 KB Output is correct
2 Correct 239 ms 32452 KB Output is correct
3 Correct 100 ms 21764 KB Output is correct
4 Correct 222 ms 32448 KB Output is correct
5 Correct 252 ms 32596 KB Output is correct
6 Correct 232 ms 32432 KB Output is correct
7 Correct 244 ms 32448 KB Output is correct
8 Correct 168 ms 27484 KB Output is correct
9 Correct 227 ms 32436 KB Output is correct
10 Correct 230 ms 32476 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 9720 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 9708 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 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9652 KB Output is correct
15 Correct 231 ms 32440 KB Output is correct
16 Correct 239 ms 32452 KB Output is correct
17 Correct 100 ms 21764 KB Output is correct
18 Correct 222 ms 32448 KB Output is correct
19 Correct 252 ms 32596 KB Output is correct
20 Correct 232 ms 32432 KB Output is correct
21 Correct 244 ms 32448 KB Output is correct
22 Correct 168 ms 27484 KB Output is correct
23 Correct 227 ms 32436 KB Output is correct
24 Correct 230 ms 32476 KB Output is correct
25 Correct 196 ms 25932 KB Output is correct
26 Correct 237 ms 35376 KB Output is correct
27 Correct 253 ms 35840 KB Output is correct
28 Correct 276 ms 37796 KB Output is correct
29 Correct 236 ms 34504 KB Output is correct
30 Correct 284 ms 37508 KB Output is correct
31 Correct 29 ms 13516 KB Output is correct
32 Correct 251 ms 35064 KB Output is correct
33 Correct 390 ms 40376 KB Output is correct
34 Correct 265 ms 35596 KB Output is correct
35 Correct 394 ms 40612 KB Output is correct
36 Correct 246 ms 34880 KB Output is correct
37 Correct 363 ms 39224 KB Output is correct
38 Correct 320 ms 37484 KB Output is correct
39 Correct 397 ms 40356 KB Output is correct
40 Correct 355 ms 38600 KB Output is correct
41 Correct 395 ms 38360 KB Output is correct
42 Correct 427 ms 41332 KB Output is correct
43 Correct 287 ms 35984 KB Output is correct
44 Correct 413 ms 41536 KB Output is correct
45 Correct 264 ms 35400 KB Output is correct
46 Correct 347 ms 38480 KB Output is correct