Submission #146268

# Submission time Handle Problem Language Result Execution time Memory
146268 2019-08-23T09:04:00 Z jwvg0425 Split the Attractions (IOI19_split) C++17
40 / 100
435 ms 22804 KB
#include "split.h"
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

vector<int> edge[100005];
vector<int> treeEdge[100005];
bool visited[100005];
int cent;
int val[100005];
int csize[100005];
int treeNum[100005];
int parent[100005];
int treeSize[100005];
int compSize[100005];
bool usingTree[100005];
int usingSize;

void dfs(int root)
{
	visited[root] = true;

	for (auto& e : edge[root])
	{
		if (visited[e])
			continue;

		treeEdge[root].push_back(e);
		treeEdge[e].push_back(root);

		dfs(e);
	}
}

int findCent(int root, int n)
{
	csize[root] = 1;

	bool isCent = true;

	for (auto& e : treeEdge[root])
	{
		if (e == parent[root])
			continue;

		parent[e] = root;
		int c = findCent(e, n);

		if (c != -1)
			return c;

		csize[root] += csize[e];

		isCent = isCent && (csize[e] <= (n + 1) / 2);
	}

	if (isCent && n - csize[root] <= (n + 1) / 2)
		return root;

	return -1;
}

int treeDfs(int root, int k)
{
	// cent 방향 안 가면서 크기 계산, 트리 번호도 붙여준다
	int res = 1;
	visited[root] = true;
	treeNum[root] = k;

	for (auto& e : treeEdge[root])
	{
		if (treeNum[e] != 0 || e == cent)
			continue;

		res += treeDfs(e, k);
	}

	return res;
}

int comp(int root)
{
	int res = 0;
	if (!usingTree[treeNum[root]])
	{
		res += treeSize[treeNum[root]];
		usingTree[treeNum[root]] = true;
	}
	visited[root] = true;

	for (auto& e : edge[root])
	{
		if (visited[e] || e == cent)
			continue;

		res += comp(e);
	}

	return res;
}

void findTree(int root, int sz)
{
	if (usingSize >= sz)
		return;

	if (!usingTree[treeNum[root]])
	{
		usingSize += treeSize[treeNum[root]];
		usingTree[treeNum[root]] = true;
	}
	visited[root] = true;

	for (auto& e : edge[root])
	{
		if (visited[e] || e == cent)
			continue;

		findTree(e, sz);
	}
}

void mark(int root, int v, int& sz)
{
	if (sz == 0)
		return;

	val[root] = v;
	sz--;

	for (auto& e : edge[root])
	{
		if (val[e] != 0 || !usingTree[treeNum[e]])
			continue;

		mark(e, v, sz);
	}
}

void mark2(int root, int v, int& sz)
{
	if (sz == 0)
		return;
	
	val[root] = v;
	sz--;

	for (auto& e : edge[root])
	{
		if (val[e] != 0)
			continue;

		mark2(e, v, sz);
	}
}

vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q)
{
	vector<ii> v = { { a, 1 }, { b, 2 }, { c, 3 } };

	for (int i = 0; i < p.size(); i++)
	{
		edge[p[i]].push_back(q[i]);
		edge[q[i]].push_back(p[i]);
	}

	dfs(0);

	sort(all(v));

	cent = findCent(0, n);

	// centroid 제외한 그래프에서 a이상의 크기인 트리가 있는지 확인, 있으면 go
	int tr = 1;
	for (auto& e : edge[cent])
	{
		treeSize[tr] = treeDfs(e, tr);
		if (treeSize[tr] >= v[0].xx)
		{
			//이 경우, 얘 하나만 써도 OK
			usingTree[tr] = true;
			mark(e, v[0].yy, v[0].xx);
			mark2(cent, v[1].yy, v[1].xx);

			for (int i = 0; i < n; i++)
				if (val[i] == 0)
					val[i] = v[2].yy;

			vector<int> result(n);
			for (int i = 0; i < n; i++)
				result[i] = val[i];

			return result;
		}

		tr++;
	}

	memset(visited, false, sizeof(visited));
	for (auto& e : edge[cent])
	{
		int c = comp(e);

		if (c < v[0].xx)
			continue;

		// 합해서 a이상 되는 트리 확인 후, 그 트리에 속하는 원소들만 이용해서 a개수 만든다
		memset(visited, false, sizeof(visited));
		memset(usingTree, false, sizeof(usingTree));
		findTree(e, v[0].xx);

		mark(e, v[0].yy, v[0].xx);
		mark2(cent, v[1].yy, v[1].xx);
		assert(v[1].xx == 0);

		for (int i = 0; i < n; i++)
			if (val[i] == 0)
				val[i] = v[2].yy;

		break;
	}

	vector<int> result(n);
	for (int i = 0; i < n; i++)
		result[i] = val[i];

	return result;
}

Compilation message

split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:183:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < p.size(); i++)
                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 5112 KB ok, correct split
3 Correct 6 ms 5068 KB ok, correct split
4 Correct 6 ms 4984 KB ok, correct split
5 Correct 6 ms 4856 KB ok, correct split
6 Correct 6 ms 5112 KB ok, correct split
7 Correct 162 ms 22392 KB ok, correct split
8 Correct 130 ms 20088 KB ok, correct split
9 Correct 219 ms 19320 KB ok, correct split
10 Correct 130 ms 22776 KB ok, correct split
11 Correct 143 ms 22776 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 4984 KB ok, correct split
3 Correct 6 ms 5112 KB ok, correct split
4 Correct 278 ms 19700 KB ok, correct split
5 Correct 128 ms 15268 KB ok, correct split
6 Correct 157 ms 22804 KB ok, correct split
7 Correct 147 ms 19832 KB ok, correct split
8 Correct 435 ms 16920 KB ok, correct split
9 Correct 135 ms 14968 KB ok, correct split
10 Correct 97 ms 15344 KB ok, correct split
11 Correct 101 ms 15296 KB ok, correct split
12 Correct 108 ms 15304 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4984 KB ok, correct split
2 Correct 181 ms 15168 KB ok, correct split
3 Correct 42 ms 9080 KB ok, correct split
4 Correct 6 ms 5112 KB ok, correct split
5 Correct 152 ms 17528 KB ok, correct split
6 Correct 162 ms 17396 KB ok, correct split
7 Correct 188 ms 17408 KB ok, correct split
8 Correct 147 ms 18424 KB ok, correct split
9 Correct 152 ms 16888 KB ok, correct split
10 Correct 39 ms 8312 KB ok, no valid answer
11 Correct 61 ms 9976 KB ok, no valid answer
12 Correct 112 ms 15348 KB ok, no valid answer
13 Correct 139 ms 14852 KB ok, no valid answer
14 Correct 100 ms 15856 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 5112 KB ok, no valid answer
3 Correct 6 ms 4984 KB ok, correct split
4 Correct 6 ms 4984 KB ok, correct split
5 Correct 6 ms 5112 KB ok, correct split
6 Correct 6 ms 5112 KB ok, correct split
7 Correct 6 ms 5112 KB ok, correct split
8 Correct 6 ms 5112 KB ok, correct split
9 Correct 9 ms 5368 KB ok, correct split
10 Correct 9 ms 5368 KB ok, correct split
11 Correct 6 ms 5112 KB ok, correct split
12 Correct 9 ms 5368 KB ok, correct split
13 Correct 6 ms 5112 KB ok, correct split
14 Correct 6 ms 4984 KB ok, correct split
15 Correct 6 ms 5112 KB ok, correct split
16 Correct 6 ms 4984 KB ok, correct split
17 Correct 6 ms 4984 KB ok, correct split
18 Correct 6 ms 5112 KB ok, correct split
19 Correct 7 ms 5112 KB ok, correct split
20 Correct 7 ms 5240 KB ok, correct split
21 Correct 10 ms 5368 KB ok, correct split
22 Correct 9 ms 5368 KB ok, correct split
23 Correct 9 ms 5368 KB ok, correct split
24 Correct 8 ms 5368 KB ok, correct split
25 Correct 9 ms 5368 KB ok, correct split
26 Incorrect 9 ms 5624 KB invalid split: #1=799, #2=800, #3=801
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 5112 KB ok, correct split
3 Correct 6 ms 5068 KB ok, correct split
4 Correct 6 ms 4984 KB ok, correct split
5 Correct 6 ms 4856 KB ok, correct split
6 Correct 6 ms 5112 KB ok, correct split
7 Correct 162 ms 22392 KB ok, correct split
8 Correct 130 ms 20088 KB ok, correct split
9 Correct 219 ms 19320 KB ok, correct split
10 Correct 130 ms 22776 KB ok, correct split
11 Correct 143 ms 22776 KB ok, correct split
12 Correct 6 ms 4984 KB ok, correct split
13 Correct 6 ms 4984 KB ok, correct split
14 Correct 6 ms 5112 KB ok, correct split
15 Correct 278 ms 19700 KB ok, correct split
16 Correct 128 ms 15268 KB ok, correct split
17 Correct 157 ms 22804 KB ok, correct split
18 Correct 147 ms 19832 KB ok, correct split
19 Correct 435 ms 16920 KB ok, correct split
20 Correct 135 ms 14968 KB ok, correct split
21 Correct 97 ms 15344 KB ok, correct split
22 Correct 101 ms 15296 KB ok, correct split
23 Correct 108 ms 15304 KB ok, correct split
24 Correct 7 ms 4984 KB ok, correct split
25 Correct 181 ms 15168 KB ok, correct split
26 Correct 42 ms 9080 KB ok, correct split
27 Correct 6 ms 5112 KB ok, correct split
28 Correct 152 ms 17528 KB ok, correct split
29 Correct 162 ms 17396 KB ok, correct split
30 Correct 188 ms 17408 KB ok, correct split
31 Correct 147 ms 18424 KB ok, correct split
32 Correct 152 ms 16888 KB ok, correct split
33 Correct 39 ms 8312 KB ok, no valid answer
34 Correct 61 ms 9976 KB ok, no valid answer
35 Correct 112 ms 15348 KB ok, no valid answer
36 Correct 139 ms 14852 KB ok, no valid answer
37 Correct 100 ms 15856 KB ok, no valid answer
38 Correct 6 ms 4984 KB ok, correct split
39 Correct 6 ms 5112 KB ok, no valid answer
40 Correct 6 ms 4984 KB ok, correct split
41 Correct 6 ms 4984 KB ok, correct split
42 Correct 6 ms 5112 KB ok, correct split
43 Correct 6 ms 5112 KB ok, correct split
44 Correct 6 ms 5112 KB ok, correct split
45 Correct 6 ms 5112 KB ok, correct split
46 Correct 9 ms 5368 KB ok, correct split
47 Correct 9 ms 5368 KB ok, correct split
48 Correct 6 ms 5112 KB ok, correct split
49 Correct 9 ms 5368 KB ok, correct split
50 Correct 6 ms 5112 KB ok, correct split
51 Correct 6 ms 4984 KB ok, correct split
52 Correct 6 ms 5112 KB ok, correct split
53 Correct 6 ms 4984 KB ok, correct split
54 Correct 6 ms 4984 KB ok, correct split
55 Correct 6 ms 5112 KB ok, correct split
56 Correct 7 ms 5112 KB ok, correct split
57 Correct 7 ms 5240 KB ok, correct split
58 Correct 10 ms 5368 KB ok, correct split
59 Correct 9 ms 5368 KB ok, correct split
60 Correct 9 ms 5368 KB ok, correct split
61 Correct 8 ms 5368 KB ok, correct split
62 Correct 9 ms 5368 KB ok, correct split
63 Incorrect 9 ms 5624 KB invalid split: #1=799, #2=800, #3=801
64 Halted 0 ms 0 KB -