Submission #635435

#TimeUsernameProblemLanguageResultExecution timeMemory
635435ghostwriterNaboj (COCI22_naboj)C++14
110 / 110
427 ms41536 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...