Submission #86592

# Submission time Handle Problem Language Result Execution time Memory
86592 2018-11-26T20:23:45 Z KCSC Amusement Park (JOI17_amusement_park) C++14
92 / 100
68 ms 20976 KB
#include "Joi.h"

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

static const int NMAX = 10000;
static const int BITS = 60;

namespace {
vector<int> tree[NMAX];
int N;

int union_find[NMAX];
int root(int p)
{
	return union_find[p] < 0 ? p : (union_find[p] = root(union_find[p]));
}
bool join(int p, int q)
{
	p = root(p);
	q = root(q);
	if (p == q) return false;
	union_find[p] += union_find[q];
	union_find[q] = p;
	return true;
}
void SpanningTree(int M, int A[], int B[]) {
	fill(union_find, union_find + N, -1);
	for (int i = 0; i < M; ++i) {
		if (join(A[i], B[i])) {
			tree[A[i]].push_back(B[i]);
			tree[B[i]].push_back(A[i]);
		}
	}
	for (int i = 0; i < N; ++i) sort(tree[i].begin(), tree[i].end());
}

bool HasEdge(int u, int v) {
	return binary_search(tree[u].begin(), tree[u].end(), v);
}

vector<int> subtree[NMAX];
int idx[NMAX];

void InitialSubtree(int p, int rt, vector<int> &sto)
{
	if (sto.size() >= BITS) return;
	idx[p] = sto.size();
	sto.push_back(p);
	for (int q : tree[p]) if (q != rt) {
		InitialSubtree(q, p, sto);
	}
}
void ComputeSubtrees(int p, int rt, vector<pair<int, int> > sub)
{
	bool has = false;
	for (auto v : sub) if (v.first == p) has = true;

	if (!has) {
		int purge = -1;
		for (int i = 0; i < sub.size(); ++i) {
			if (sub[i].second == 1 && sub[i].first != rt) {
				purge = i;
				break;
			}
		}
		for (int i = 0; i < sub.size(); ++i) {
			if (HasEdge(sub[i].first, sub[purge].first)) {
				--sub[i].second;
			}
		}
		idx[p] = idx[sub[purge].first];
		sub[purge] = make_pair(p, 1);
		for (int i = 0; i < sub.size(); ++i) {
			if (sub[i].first == rt) {
				++sub[i].second;
			}
		}
	}

	for (auto v : sub) subtree[p].push_back(v.first);
	for (int q : tree[p]) if (q != rt) {
		ComputeSubtrees(q, p, sub);
	}
}
void CommonProc(int N_, int M, int A[], int B[]) {
	N = N_;
	SpanningTree(M, A, B);

	vector<int> tree;
	InitialSubtree(0, -1, tree);

	vector<pair<int, int> > tree_deg;
	for (int i = 0; i < tree.size(); ++i) {
		int deg = 0;
		for (int j = 0; j < tree.size(); ++j) {
			if (HasEdge(tree[i], tree[j])) ++deg;
		}
		tree_deg.push_back({ tree[i], deg });
	}
	ComputeSubtrees(0, -1, tree_deg);
}
}
void Joi(int N_, int M, int A[], int B[], long long X, int T) {
	CommonProc(N_, M, A, B);

	for (int i = 0; i < N; ++i) {
		MessageBoard(i, (int)((X >> idx[i]) & 1));
	}
}
#include "Ioi.h"

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

static const int NMAX = 10000;
static const int BITS = 60;

namespace {
vector<int> tree[NMAX];
int N;

int union_find[NMAX];
int root(int p)
{
	return union_find[p] < 0 ? p : (union_find[p] = root(union_find[p]));
}
bool join(int p, int q)
{
	p = root(p);
	q = root(q);
	if (p == q) return false;
	union_find[p] += union_find[q];
	union_find[q] = p;
	return true;
}
void SpanningTree(int M, int A[], int B[]) {
	fill(union_find, union_find + N, -1);
	for (int i = 0; i < M; ++i) {
		if (join(A[i], B[i])) {
			tree[A[i]].push_back(B[i]);
			tree[B[i]].push_back(A[i]);
		}
	}
	for (int i = 0; i < N; ++i) sort(tree[i].begin(), tree[i].end());
}

bool HasEdge(int u, int v) {
	return binary_search(tree[u].begin(), tree[u].end(), v);
}

vector<int> subtree[NMAX];
int idx[NMAX];

void InitialSubtree(int p, int rt, vector<int> &sto)
{
	if (sto.size() >= BITS) return;
	idx[p] = sto.size();
	sto.push_back(p);
	for (int q : tree[p]) if (q != rt) {
		InitialSubtree(q, p, sto);
	}
}
void ComputeSubtrees(int p, int rt, vector<pair<int, int> > sub)
{
	bool has = false;
	for (auto v : sub) if (v.first == p) has = true;

	if (!has) {
		int purge = -1;
		for (int i = 0; i < sub.size(); ++i) {
			if (sub[i].second == 1 && sub[i].first != rt) {
				purge = i;
				break;
			}
		}
		for (int i = 0; i < sub.size(); ++i) {
			if (HasEdge(sub[i].first, sub[purge].first)) {
				--sub[i].second;
			}
		}
		idx[p] = idx[sub[purge].first];
		sub[purge] = make_pair(p, 1);
		for (int i = 0; i < sub.size(); ++i) {
			if (sub[i].first == rt) {
				++sub[i].second;
			}
		}
	}

	for (auto v : sub) subtree[p].push_back(v.first);
	for (int q : tree[p]) if (q != rt) {
		ComputeSubtrees(q, p, sub);
	}
}
void CommonProc(int N_, int M, int A[], int B[]) {
	N = N_;
	SpanningTree(M, A, B);

	vector<int> tree;
	InitialSubtree(0, -1, tree);

	vector<pair<int, int> > tree_deg;
	for (int i = 0; i < tree.size(); ++i) {
		int deg = 0;
		for (int j = 0; j < tree.size(); ++j) {
			if (HasEdge(tree[i], tree[j])) ++deg;
		}
		tree_deg.push_back({ tree[i], deg });
	}
	ComputeSubtrees(0, -1, tree_deg);
}

long long ans;
bool target[NMAX];

void Solve(int p, int rt, int v)
{
	ans |= (long long)v << idx[p];
	for (int q : tree[p]) if (q != rt && target[q]) {
		int v2 = Move(q);
		Solve(q, p, v2);
		Move(p);
	}
}
}
long long Ioi(int N_, int M, int A[], int B[], int P, int V, int T) {
	CommonProc(N_, M, A, B);

	ans = 0;
	for (int i = 0; i < N; ++i) target[i] = false;
	for (auto p : subtree[P]) target[p] = true;

	Solve(P, -1, V);
	return ans;
}

Compilation message

Joi.cpp: In function 'void {anonymous}::ComputeSubtrees(int, int, std::vector<std::pair<int, int> >)':
Joi.cpp:63:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Joi.cpp:69:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Joi.cpp:76:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Joi.cpp: In function 'void {anonymous}::CommonProc(int, int, int*, int*)':
Joi.cpp:96:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < tree.size(); ++i) {
                  ~~^~~~~~~~~~~~~
Joi.cpp:98:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < tree.size(); ++j) {
                   ~~^~~~~~~~~~~~~

Ioi.cpp: In function 'void {anonymous}::ComputeSubtrees(int, int, std::vector<std::pair<int, int> >)':
Ioi.cpp:63:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Ioi.cpp:69:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Ioi.cpp:76:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < sub.size(); ++i) {
                   ~~^~~~~~~~~~~~
Ioi.cpp: In function 'void {anonymous}::CommonProc(int, int, int*, int*)':
Ioi.cpp:96:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < tree.size(); ++i) {
                  ~~^~~~~~~~~~~~~
Ioi.cpp:98:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < tree.size(); ++j) {
                   ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1756 KB Output is correct
2 Correct 4 ms 1808 KB Output is correct
3 Correct 6 ms 1816 KB Output is correct
4 Correct 4 ms 1908 KB Output is correct
5 Correct 4 ms 1880 KB Output is correct
6 Correct 4 ms 1908 KB Output is correct
7 Correct 6 ms 2168 KB Output is correct
8 Runtime error 4 ms 1912 KB Execution killed with signal 2 (could be triggered by violating memory limits)
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 10976 KB Output is correct
2 Correct 65 ms 11356 KB Output is correct
3 Correct 63 ms 11268 KB Output is correct
4 Correct 51 ms 10272 KB Output is correct
5 Correct 66 ms 15392 KB Output is correct
6 Correct 57 ms 11552 KB Output is correct
7 Correct 63 ms 12160 KB Output is correct
8 Correct 61 ms 13116 KB Output is correct
9 Correct 59 ms 12576 KB Output is correct
10 Correct 41 ms 9252 KB Output is correct
11 Correct 41 ms 9172 KB Output is correct
12 Correct 41 ms 9608 KB Output is correct
13 Correct 43 ms 9480 KB Output is correct
14 Correct 48 ms 9752 KB Output is correct
15 Correct 53 ms 10784 KB Output is correct
16 Correct 45 ms 10784 KB Output is correct
17 Correct 48 ms 10532 KB Output is correct
18 Correct 47 ms 10528 KB Output is correct
19 Correct 51 ms 10512 KB Output is correct
20 Correct 43 ms 14880 KB Output is correct
21 Correct 41 ms 14700 KB Output is correct
22 Correct 58 ms 12064 KB Output is correct
23 Correct 58 ms 11812 KB Output is correct
24 Correct 58 ms 12016 KB Output is correct
25 Correct 61 ms 12560 KB Output is correct
26 Correct 59 ms 12824 KB Output is correct
27 Correct 59 ms 11964 KB Output is correct
28 Correct 55 ms 12064 KB Output is correct
29 Correct 52 ms 11056 KB Output is correct
30 Correct 59 ms 11284 KB Output is correct
31 Correct 4 ms 1908 KB Output is correct
32 Correct 4 ms 1912 KB Output is correct
33 Correct 4 ms 1784 KB Output is correct
34 Correct 4 ms 1908 KB Output is correct
35 Correct 4 ms 1792 KB Output is correct
36 Correct 4 ms 1764 KB Output is correct
37 Correct 4 ms 1788 KB Output is correct
38 Correct 4 ms 1652 KB Output is correct
39 Correct 4 ms 1652 KB Output is correct
40 Correct 4 ms 1920 KB Output is correct
41 Correct 4 ms 1780 KB Output is correct
42 Correct 4 ms 1764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1776 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 4 ms 1776 KB Output is correct
4 Correct 10 ms 4652 KB Output is correct
5 Correct 11 ms 4660 KB Output is correct
6 Correct 10 ms 4652 KB Output is correct
7 Correct 11 ms 4780 KB Output is correct
8 Correct 11 ms 4652 KB Output is correct
9 Correct 47 ms 20860 KB Output is correct
10 Correct 47 ms 20976 KB Output is correct
11 Correct 46 ms 20768 KB Output is correct
12 Correct 4 ms 1908 KB Output is correct
13 Correct 4 ms 1784 KB Output is correct
14 Correct 4 ms 1908 KB Output is correct
15 Correct 4 ms 1780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 11076 KB Output is correct
2 Correct 65 ms 10820 KB Output is correct
3 Correct 61 ms 10820 KB Output is correct
4 Correct 52 ms 10388 KB Output is correct
5 Correct 59 ms 15916 KB Output is correct
6 Correct 59 ms 12416 KB Output is correct
7 Correct 55 ms 11680 KB Output is correct
8 Correct 58 ms 11652 KB Output is correct
9 Correct 59 ms 13036 KB Output is correct
10 Correct 41 ms 9388 KB Output is correct
11 Correct 41 ms 9280 KB Output is correct
12 Correct 40 ms 9520 KB Output is correct
13 Correct 43 ms 9560 KB Output is correct
14 Correct 46 ms 9756 KB Output is correct
15 Correct 52 ms 10764 KB Output is correct
16 Correct 48 ms 10788 KB Output is correct
17 Correct 53 ms 10580 KB Output is correct
18 Correct 50 ms 10380 KB Output is correct
19 Correct 51 ms 10528 KB Output is correct
20 Correct 41 ms 15008 KB Output is correct
21 Correct 40 ms 14712 KB Output is correct
22 Correct 56 ms 12696 KB Output is correct
23 Correct 54 ms 11808 KB Output is correct
24 Correct 54 ms 12528 KB Output is correct
25 Correct 55 ms 11788 KB Output is correct
26 Correct 51 ms 11812 KB Output is correct
27 Correct 56 ms 13260 KB Output is correct
28 Correct 54 ms 12220 KB Output is correct
29 Correct 51 ms 11624 KB Output is correct
30 Correct 54 ms 11284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 10816 KB Output is correct
2 Correct 61 ms 10992 KB Output is correct
3 Correct 60 ms 10964 KB Output is correct
4 Correct 53 ms 10288 KB Output is correct
5 Correct 59 ms 17920 KB Output is correct
6 Correct 53 ms 13088 KB Output is correct
7 Correct 54 ms 12960 KB Output is correct
8 Correct 55 ms 12320 KB Output is correct
9 Correct 57 ms 11552 KB Output is correct
10 Correct 59 ms 9312 KB Output is correct
11 Correct 40 ms 9364 KB Output is correct
12 Correct 40 ms 9504 KB Output is correct
13 Correct 40 ms 9608 KB Output is correct
14 Correct 41 ms 9960 KB Output is correct
15 Correct 45 ms 10792 KB Output is correct
16 Correct 45 ms 10776 KB Output is correct
17 Correct 48 ms 10468 KB Output is correct
18 Correct 48 ms 10528 KB Output is correct
19 Correct 44 ms 10548 KB Output is correct
20 Correct 39 ms 14880 KB Output is correct
21 Correct 40 ms 14684 KB Output is correct
22 Correct 56 ms 12104 KB Output is correct
23 Correct 54 ms 11936 KB Output is correct
24 Correct 59 ms 12360 KB Output is correct
25 Correct 55 ms 12632 KB Output is correct
26 Correct 54 ms 12040 KB Output is correct
27 Correct 57 ms 12816 KB Output is correct
28 Correct 63 ms 13284 KB Output is correct
29 Correct 55 ms 12332 KB Output is correct
30 Correct 53 ms 11284 KB Output is correct
31 Correct 4 ms 1908 KB Output is correct
32 Correct 4 ms 1780 KB Output is correct
33 Correct 5 ms 1792 KB Output is correct
34 Correct 5 ms 1780 KB Output is correct
35 Correct 4 ms 1908 KB Output is correct
36 Correct 4 ms 1908 KB Output is correct
37 Correct 4 ms 1916 KB Output is correct
38 Correct 4 ms 1656 KB Output is correct
39 Correct 4 ms 1780 KB Output is correct
40 Correct 4 ms 1780 KB Output is correct
41 Correct 4 ms 1916 KB Output is correct
42 Correct 4 ms 1652 KB Output is correct