답안 #26583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26583 2017-07-03T09:54:52 Z model_code Amusement Park (JOI17_amusement_park) C++14
100 / 100
89 ms 23480 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) {
                     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5684 KB Output is correct
2 Correct 0 ms 5948 KB Output is correct
3 Correct 0 ms 5948 KB Output is correct
4 Correct 0 ms 5948 KB Output is correct
5 Correct 0 ms 5948 KB Output is correct
6 Correct 0 ms 5948 KB Output is correct
7 Correct 0 ms 5948 KB Output is correct
8 Correct 0 ms 5948 KB Output is correct
9 Correct 0 ms 5948 KB Output is correct
10 Correct 0 ms 5948 KB Output is correct
11 Correct 3 ms 5948 KB Output is correct
12 Correct 0 ms 5684 KB Output is correct
13 Correct 0 ms 5948 KB Output is correct
14 Correct 0 ms 5948 KB Output is correct
15 Correct 0 ms 5948 KB Output is correct
16 Correct 0 ms 5948 KB Output is correct
17 Correct 0 ms 5948 KB Output is correct
18 Correct 0 ms 5948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 13604 KB Output is correct
2 Correct 62 ms 13340 KB Output is correct
3 Correct 89 ms 13596 KB Output is correct
4 Correct 56 ms 13340 KB Output is correct
5 Correct 55 ms 17932 KB Output is correct
6 Correct 58 ms 14128 KB Output is correct
7 Correct 69 ms 14696 KB Output is correct
8 Correct 62 ms 15644 KB Output is correct
9 Correct 55 ms 15148 KB Output is correct
10 Correct 46 ms 12148 KB Output is correct
11 Correct 45 ms 12148 KB Output is correct
12 Correct 46 ms 12548 KB Output is correct
13 Correct 55 ms 12548 KB Output is correct
14 Correct 49 ms 12812 KB Output is correct
15 Correct 49 ms 13604 KB Output is correct
16 Correct 52 ms 13604 KB Output is correct
17 Correct 58 ms 13340 KB Output is correct
18 Correct 62 ms 13340 KB Output is correct
19 Correct 58 ms 13332 KB Output is correct
20 Correct 49 ms 17512 KB Output is correct
21 Correct 36 ms 17224 KB Output is correct
22 Correct 62 ms 14780 KB Output is correct
23 Correct 72 ms 14596 KB Output is correct
24 Correct 78 ms 14620 KB Output is correct
25 Correct 76 ms 15284 KB Output is correct
26 Correct 69 ms 15676 KB Output is correct
27 Correct 72 ms 14604 KB Output is correct
28 Correct 59 ms 14680 KB Output is correct
29 Correct 52 ms 13676 KB Output is correct
30 Correct 62 ms 14196 KB Output is correct
31 Correct 0 ms 5948 KB Output is correct
32 Correct 0 ms 5948 KB Output is correct
33 Correct 0 ms 5948 KB Output is correct
34 Correct 0 ms 5948 KB Output is correct
35 Correct 0 ms 5948 KB Output is correct
36 Correct 0 ms 5684 KB Output is correct
37 Correct 0 ms 5684 KB Output is correct
38 Correct 0 ms 5684 KB Output is correct
39 Correct 0 ms 5684 KB Output is correct
40 Correct 0 ms 5684 KB Output is correct
41 Correct 0 ms 5948 KB Output is correct
42 Correct 0 ms 5684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5948 KB Output is correct
2 Correct 0 ms 5948 KB Output is correct
3 Correct 0 ms 5948 KB Output is correct
4 Correct 3 ms 8480 KB Output is correct
5 Correct 6 ms 8476 KB Output is correct
6 Correct 3 ms 8476 KB Output is correct
7 Correct 6 ms 8468 KB Output is correct
8 Correct 6 ms 8472 KB Output is correct
9 Correct 35 ms 23480 KB Output is correct
10 Correct 39 ms 23480 KB Output is correct
11 Correct 39 ms 23476 KB Output is correct
12 Correct 0 ms 5948 KB Output is correct
13 Correct 0 ms 5948 KB Output is correct
14 Correct 0 ms 5684 KB Output is correct
15 Correct 0 ms 5684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 13604 KB Output is correct
2 Correct 72 ms 13604 KB Output is correct
3 Correct 65 ms 13340 KB Output is correct
4 Correct 46 ms 13332 KB Output is correct
5 Correct 58 ms 18828 KB Output is correct
6 Correct 65 ms 15152 KB Output is correct
7 Correct 65 ms 14304 KB Output is correct
8 Correct 72 ms 14396 KB Output is correct
9 Correct 69 ms 15632 KB Output is correct
10 Correct 38 ms 12140 KB Output is correct
11 Correct 41 ms 12148 KB Output is correct
12 Correct 52 ms 12540 KB Output is correct
13 Correct 49 ms 12540 KB Output is correct
14 Correct 41 ms 12812 KB Output is correct
15 Correct 58 ms 13604 KB Output is correct
16 Correct 49 ms 13604 KB Output is correct
17 Correct 48 ms 13340 KB Output is correct
18 Correct 55 ms 13340 KB Output is correct
19 Correct 58 ms 13340 KB Output is correct
20 Correct 49 ms 17516 KB Output is correct
21 Correct 35 ms 17224 KB Output is correct
22 Correct 86 ms 15344 KB Output is correct
23 Correct 69 ms 14440 KB Output is correct
24 Correct 52 ms 15196 KB Output is correct
25 Correct 78 ms 14608 KB Output is correct
26 Correct 72 ms 14560 KB Output is correct
27 Correct 72 ms 15824 KB Output is correct
28 Correct 62 ms 14828 KB Output is correct
29 Correct 65 ms 14416 KB Output is correct
30 Correct 52 ms 14000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 13340 KB Output is correct
2 Correct 62 ms 13340 KB Output is correct
3 Correct 72 ms 13596 KB Output is correct
4 Correct 59 ms 13340 KB Output is correct
5 Correct 62 ms 20696 KB Output is correct
6 Correct 59 ms 15664 KB Output is correct
7 Correct 62 ms 15572 KB Output is correct
8 Correct 62 ms 14872 KB Output is correct
9 Correct 69 ms 14184 KB Output is correct
10 Correct 39 ms 12148 KB Output is correct
11 Correct 49 ms 12148 KB Output is correct
12 Correct 38 ms 12548 KB Output is correct
13 Correct 52 ms 12540 KB Output is correct
14 Correct 55 ms 12812 KB Output is correct
15 Correct 46 ms 13596 KB Output is correct
16 Correct 49 ms 13604 KB Output is correct
17 Correct 52 ms 13332 KB Output is correct
18 Correct 55 ms 13340 KB Output is correct
19 Correct 52 ms 13332 KB Output is correct
20 Correct 41 ms 17508 KB Output is correct
21 Correct 28 ms 17224 KB Output is correct
22 Correct 76 ms 14504 KB Output is correct
23 Correct 72 ms 14644 KB Output is correct
24 Correct 72 ms 15192 KB Output is correct
25 Correct 68 ms 15244 KB Output is correct
26 Correct 72 ms 14792 KB Output is correct
27 Correct 76 ms 15572 KB Output is correct
28 Correct 65 ms 15872 KB Output is correct
29 Correct 56 ms 14960 KB Output is correct
30 Correct 58 ms 14072 KB Output is correct
31 Correct 0 ms 5948 KB Output is correct
32 Correct 0 ms 5948 KB Output is correct
33 Correct 0 ms 5948 KB Output is correct
34 Correct 0 ms 5948 KB Output is correct
35 Correct 0 ms 5948 KB Output is correct
36 Correct 0 ms 5684 KB Output is correct
37 Correct 0 ms 5684 KB Output is correct
38 Correct 0 ms 5684 KB Output is correct
39 Correct 0 ms 5684 KB Output is correct
40 Correct 0 ms 5684 KB Output is correct
41 Correct 0 ms 5948 KB Output is correct
42 Correct 0 ms 5684 KB Output is correct