Submission #1234225

#TimeUsernameProblemLanguageResultExecution timeMemory
1234225franuchBrought Down the Grading Server? (CEOI23_balance)C++20
10 / 100
2094 ms17040 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vc vector
#define st first
#define nd second
#define pll pair<ll, ll>
#define sz(a) (ll)a.size()
#define all(a) a.begin(), a.end()
#define pub push_back
#define pob pop_back

ll E, n, V;
vc<vc<ll>> a;

void input() {
	cin >> E >> n >> V;
	a.assign(E, vc<ll>(n));
	for (ll i = 0; i < E; i++)
		for (ll j = 0; j < n; j++) {
			cin >> a[i][j];
			a[i][j]--;
		}
}

struct Edge {
	ll v, i;
};

void solve() {
	vc<vc<Edge>> g(V, vc<Edge>());
	for (ll i = 0; i < E; i++) {
		ll v = a[i][0];
		ll w = a[i][1];
		g[v].pub({w, i});
		g[w].pub({v, i});
	}

	vc<ll> exp(V, 0);
	for (ll i = 0; i < E; i++)
		exp[a[i][0]]++, exp[a[i][1]]++;
	
	vc<ll> cur(V, 0);
	for (ll i = 0; i < E; i++)
		cur[a[i][0]]++;

	vc<bool> vis1;
	function<bool(ll)> dfs1 = [&](ll v) {
		vis1[v] = true;
		if (cur[v] < exp[v] / 2) {
			cur[v]++;
			return true;
		}
		for (auto &e : g[v]) {
			if (vis1[e.v] or a[e.i][0] != v)
				continue;
			if (dfs1(e.v)) {
				swap(a[e.i][0], a[e.i][1]);
				return true;
			}
		}
		return false;
	};
	
	vc<bool> vis2;
	function<bool(ll)> dfs2 = [&](ll v) {
		vis2[v] = true;
		if (cur[v] < (exp[v] + 1) / 2) {
			cur[v]++;
			return true;
		}
		for (auto &e : g[v]) {
			if (vis2[e.v] or a[e.i][0] != v)
				continue;
			if (dfs2(e.v)) {
				swap(a[e.i][0], a[e.i][1]);
				return true;
			}
		}
		return false;
	};
	
	while (true) {
		vis1.assign(V, false);
		vis2.assign(V, false);
		bool git = false;
		for (ll v = 0; v < V; v++) {
			if (not vis1[v] and cur[v] > exp[v] / 2 and dfs1(v)) {
				git = true;
				cur[v]--;
			}
			if (not vis2[v] and cur[v] > (exp[v] + 1) / 2 and dfs2(v)) {
				git = true;
				cur[v]--;
			}
		}
		if (not git)
			break;
	}
	for (auto &ai : a)
		cout << ai[0] + 1 << " " << ai[1] + 1 << "\n";	
}

void program() {
	input();
	solve();
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	program();
	return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...