답안 #298659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298659 2020-09-13T17:58:16 Z keko37 Split the Attractions (IOI19_split) C++14
40 / 100
145 ms 18552 KB
#include<bits/stdc++.h>
#include "split.h"

using namespace std;

typedef vector <int> vi;
typedef pair <int, int> pi;

const int MAXN = 200005;

int n, m, root, cnt;
pi col[3];
int par[MAXN], siz[MAXN], sol[MAXN];
vector <int> v[MAXN];
vector <pi> e;
set <pi> st;

int nadi (int x) {
	if (x == par[x]) return x;
	return par[x] = nadi(par[x]);
}

bool spoji (int a, int b) {
	a = nadi(a); b = nadi(b);
	if (a == b) return 0;
	par[a] = b;
	siz[b] += siz[a];
	return 1;
}

void dfs (int x, int rod) {
	siz[x] = 1;
	for (auto sus : v[x]) {
		if (sus == rod) continue;
		dfs(sus, x);
		siz[x] += siz[sus];
	}
}

int nadi_centroid (int x, int rod) {
	int mx = 0, ind = 0;
	for (auto sus : v[x]) {
		if (sus == rod) continue;
		if (siz[sus] > mx) {
			mx = siz[sus];
			ind = sus;
		}
	}
	if (mx * 2 <= n) return x;
	return nadi_centroid(ind, x);
}

bool check () {
	if (st.empty()) return 0;
	auto it = st.end(); it--;
	return (it -> first) >= col[0].first;
}

void oboji (int x) {
	if (cnt == col[1].first) return;
	sol[x] = col[1].second;
	cnt++;
	for (auto sus : v[x]) {
		if (cnt == col[1].first) return;
		if (sol[sus] == 0) oboji(sus);
	}
}

int p;

void small (int x) {
	if (cnt == col[0].first) return;
	sol[x] = col[0].second;
	cnt++;
	for (auto sus : v[x]) {
		if (cnt == col[0].first) return;
		if (sol[sus] == 0 && nadi(sus) == p) small(sus);
	}
}

vi solve () {
	auto it = st.end(); it--;
	p = it -> second;
	cnt = 0; small(p);
	cnt = 0; oboji(root);
	vi res(n);
	for (int i = 1; i <= n; i++) {
		if (sol[i] == 0) sol[i] = col[2].second;
		res[i - 1] = sol[i];
	}
	return res;
}

vi find_split (int N, int A, int B, int C, vi P, vi Q) {
	n = N; m = P.size();
	col[0] = {A, 1}; col[1] = {B, 2}; col[2] = {C, 3};
	sort(col, col + 3);
	for (int i = 1; i <= n; i++) {
		par[i] = i;
	}
	for (int i = 0; i < m; i++) {
		P[i]++; Q[i]++;
		if (spoji(P[i], Q[i])) {
			v[P[i]].push_back(Q[i]);
			v[Q[i]].push_back(P[i]);
		} else {
			e.push_back({P[i], Q[i]});
		}
	}
	dfs(1, 0);
	root = nadi_centroid(1, 0);
	for (int i = 1; i <= n; i++) {
		par[i] = i;
		siz[i] = 1;
	}
	for (int i = 1; i <= n; i++) {
		for (auto sus : v[i]) {
			if (i != root && sus != root && i < sus) spoji(i, sus);
		}
	}
	for (auto sus : v[root]) {
		st.insert({siz[nadi(sus)], nadi(sus)});
	}
	if (check()) return solve();
	for (auto edge : e) {
		int a = edge.first, b = edge.second;
		a = nadi(a); b = nadi(b);
		if (a == b) continue;
		st.erase({siz[a], a});
		st.erase({siz[b], b});
		par[a] = b;
		siz[b] += siz[a];
		st.insert({siz[b], b});
		if (check()) return solve();
	}
	vi res(n);
	return res;
}




# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 4 ms 4992 KB ok, correct split
3 Correct 4 ms 4992 KB ok, correct split
4 Correct 3 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 123 ms 18424 KB ok, correct split
8 Correct 108 ms 16632 KB ok, correct split
9 Correct 113 ms 15992 KB ok, correct split
10 Correct 110 ms 18552 KB ok, correct split
11 Correct 117 ms 17528 KB ok, correct split
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB ok, correct split
2 Correct 3 ms 4992 KB ok, correct split
3 Correct 4 ms 5120 KB ok, correct split
4 Correct 120 ms 12528 KB ok, correct split
5 Correct 98 ms 11384 KB ok, correct split
6 Correct 110 ms 17272 KB ok, correct split
7 Correct 117 ms 15224 KB ok, correct split
8 Correct 145 ms 13548 KB ok, correct split
9 Correct 110 ms 11384 KB ok, correct split
10 Correct 107 ms 16368 KB ok, correct split
11 Correct 117 ms 16368 KB ok, correct split
12 Correct 113 ms 16368 KB ok, correct split
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB ok, correct split
2 Correct 104 ms 11384 KB ok, correct split
3 Correct 36 ms 8192 KB ok, correct split
4 Correct 3 ms 4992 KB ok, correct split
5 Correct 117 ms 14616 KB ok, correct split
6 Correct 113 ms 14332 KB ok, correct split
7 Correct 140 ms 14200 KB ok, correct split
8 Correct 123 ms 15224 KB ok, correct split
9 Correct 130 ms 14072 KB ok, correct split
10 Correct 29 ms 7416 KB ok, no valid answer
11 Correct 45 ms 8696 KB ok, no valid answer
12 Correct 122 ms 15220 KB ok, no valid answer
13 Correct 96 ms 12664 KB ok, no valid answer
14 Correct 122 ms 17136 KB ok, no valid answer
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4992 KB ok, correct split
2 Correct 4 ms 4992 KB ok, no valid answer
3 Correct 3 ms 4992 KB ok, correct split
4 Correct 4 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 4 ms 4992 KB ok, correct split
8 Correct 3 ms 4992 KB ok, correct split
9 Correct 7 ms 5248 KB ok, correct split
10 Correct 6 ms 5248 KB ok, correct split
11 Correct 4 ms 4992 KB ok, correct split
12 Incorrect 7 ms 5376 KB invalid split: #1=799, #2=802, #3=800
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 4 ms 4992 KB ok, correct split
3 Correct 4 ms 4992 KB ok, correct split
4 Correct 3 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 123 ms 18424 KB ok, correct split
8 Correct 108 ms 16632 KB ok, correct split
9 Correct 113 ms 15992 KB ok, correct split
10 Correct 110 ms 18552 KB ok, correct split
11 Correct 117 ms 17528 KB ok, correct split
12 Correct 4 ms 5120 KB ok, correct split
13 Correct 3 ms 4992 KB ok, correct split
14 Correct 4 ms 5120 KB ok, correct split
15 Correct 120 ms 12528 KB ok, correct split
16 Correct 98 ms 11384 KB ok, correct split
17 Correct 110 ms 17272 KB ok, correct split
18 Correct 117 ms 15224 KB ok, correct split
19 Correct 145 ms 13548 KB ok, correct split
20 Correct 110 ms 11384 KB ok, correct split
21 Correct 107 ms 16368 KB ok, correct split
22 Correct 117 ms 16368 KB ok, correct split
23 Correct 113 ms 16368 KB ok, correct split
24 Correct 3 ms 4992 KB ok, correct split
25 Correct 104 ms 11384 KB ok, correct split
26 Correct 36 ms 8192 KB ok, correct split
27 Correct 3 ms 4992 KB ok, correct split
28 Correct 117 ms 14616 KB ok, correct split
29 Correct 113 ms 14332 KB ok, correct split
30 Correct 140 ms 14200 KB ok, correct split
31 Correct 123 ms 15224 KB ok, correct split
32 Correct 130 ms 14072 KB ok, correct split
33 Correct 29 ms 7416 KB ok, no valid answer
34 Correct 45 ms 8696 KB ok, no valid answer
35 Correct 122 ms 15220 KB ok, no valid answer
36 Correct 96 ms 12664 KB ok, no valid answer
37 Correct 122 ms 17136 KB ok, no valid answer
38 Correct 3 ms 4992 KB ok, correct split
39 Correct 4 ms 4992 KB ok, no valid answer
40 Correct 3 ms 4992 KB ok, correct split
41 Correct 4 ms 4992 KB ok, correct split
42 Correct 4 ms 4992 KB ok, correct split
43 Correct 4 ms 4992 KB ok, correct split
44 Correct 4 ms 4992 KB ok, correct split
45 Correct 3 ms 4992 KB ok, correct split
46 Correct 7 ms 5248 KB ok, correct split
47 Correct 6 ms 5248 KB ok, correct split
48 Correct 4 ms 4992 KB ok, correct split
49 Incorrect 7 ms 5376 KB invalid split: #1=799, #2=802, #3=800
50 Halted 0 ms 0 KB -