Submission #1198120

#TimeUsernameProblemLanguageResultExecution timeMemory
1198120tkm_algorithmsSplit the Attractions (IOI19_split)C++20
40 / 100
61 ms24136 KiB
/**
*    In the name of Allah
*    We are nothing and you're everything
**/
#include <bits/stdc++.h>
#include "split.h"
using namespace std;
using ll = long long;

#define all(x) begin(x), end(x)
//#define sz(x) (int)(x).size()

const char nl = '\n';

const int N = 1e5+10;
vector<int> g[N], taze[N];
int t = 0, sz[N];
int tin[N], tout[N], cut[N], vis[N], pa[N];
vector<pair<int, int>> arr;

void dfs(int a) {
	sz[a] = 1;
	tin[a] = tout[a] = ++t;
	for (auto i: g[a]) {
		if (!tin[i]) {
			dfs(i);
			tout[a] = min(tout[a], tout[i]);
			sz[a] += sz[i];
			taze[a].push_back(i);
			continue;
		}
		if (tin[i] == tin[a]-1)continue; // parent
		tout[a] = min(tout[a], tin[i]); // back edge.
	}
}

int centroid(int a, int id) {
	for (auto i: taze[a]) {
		if (sz[i] >= arr[id].first)return centroid(i, id);
	}
	return a;
}

vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
	int m = (int)p.size();
	arr.push_back({a, 1});
	arr.push_back({b, 2});
	arr.push_back({c, 3});
	for (int i = 0; i < m; ++i) {
		int u = p[i], v = q[i];
		g[u].push_back(v);
		g[v].push_back(u);
	}
	sort(all(arr));
	vector<int> ans(n, arr[2].second);
	dfs(0);
	int v = centroid(0, 0);
	
	function<void(int, int)> one = [&](int u, int par) {
		if (!arr[1].first)return;
		arr[1].first -= 1;
		ans[u] = arr[1].second;
		vis[u] = 1;
		for (auto i: g[u]) {
			if (vis[i] == 1)continue;
			one(i, u);
		}
 	};
 	
 	function<void(int, int)> zero = [&](int u, int par) {
		if (arr[0].first) {
			arr[0].first -= 1;
			ans[u] = arr[0].second;
		}
		vis[u] = 1;
		for (auto i: taze[u]) {
			//if (vis[i] == 1 || cut[i] == 1)continue;
			zero(i, u);
		}
 	};
 	
 	int sum = 0;
 	for (auto i: taze[v]) {
		if (tout[i] < tin[v]) {
			if (sz[v]-sum-sz[i] >= arr[0].first) {
				sum += sz[i];
				cut[i] = 1;
			}
		}
	}
	
	if (n-sz[v] >= arr[1].first) {
		arr[0].first -= 1;
		ans[v] = arr[0].second;
		vis[v] = 1;
		for (auto i: taze[v]) {
			if (!cut[i])zero(i, i);
		}
		bool ok = true;
		for (int i = 0; i < n; ++i) {
			if (vis[i] == 0) {
				one(i, i);
				ok = false;
				break;
			}
		}
		if (arr[0].first != 0 || arr[1].first != 0)ok = true;
		if (!ok)
		return ans;
	}
	
	for (int i = 0; i < n; ++i) {
		cut[i] = vis[i] = 0;
		ans[i] = arr[2].second;
	}
	
	v = centroid(0, 1);
	sum = 0;
	for (auto i: taze[v]) {
		if (tout[i] < tin[v]) {
			if (sz[v]-sum-sz[i] >= arr[1].first) {
				sum += sz[i];
				cut[i] = 1;
			}
		}
	}
	
	swap(arr[0], arr[1]);
	if (n-sz[v] >= arr[1].first) {
		arr[0].first -= 1;
		ans[v] = arr[0].second;
		vis[v] = 1;
		for (auto i: taze[v]) {
			if (!cut[i])zero(i, i);
		}
		bool ok = true;
		for (int i = 0; i < n; ++i) {
			if (vis[i] == 0) {
				one(i, i);
				break;
			}
		}
		if (arr[0].first != 0 || arr[1].first != 0)ok = true;
		return ans;
	}
	return vector<int>(n, 0);
}

//void solve() {
	//vector<int> ans = find_split(9, 4, 2, 3, {0, 0, 0, 0, 0, 0, 1, 3, 4, 5}, {1, 2, 3, 4, 6, 8, 7, 7, 5, 6});
	//for (auto i: ans)cout << i << " ";
//}

//int32_t main() {
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //solve();
    //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...