답안 #291665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
291665 2020-09-05T15:37:34 Z spdskatr Village (BOI20_village) C++14
100 / 100
196 ms 34248 KB
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

int N, dp[100005][2], ans[100005], conn[100005], seen[100005]; // 0 = isolated, 1 = attached
vector<int> graph[100005], g2[100005];
vector<int> broken[100005], unbroken[100005];
vector<int> order;

int sz[100005], ans2[100005];
long long tot;
vector<int> nds;

void dfs(int x, int p) {
	dp[x][1] = 0;
	int mindiff = 2069696969;
	for (int v : graph[x]) {
		if (v != p) {
			dfs(v, x);
			int df = max(0, dp[v][0] + 1 - dp[v][1]);
			if (mindiff > df) {
				mindiff = df;
				conn[x] = v;
			}
			if (dp[v][0] + 1 > dp[v][1]) {
				broken[x].push_back(v);
				dp[x][1] += dp[v][0] + 1;
			} else {
				unbroken[x].push_back(v);
				dp[x][1] += dp[v][1];
			}
		}
	}
	dp[x][0] = dp[x][1] - mindiff;
}

void dfs2(int x, int state) {
	int keep = -1;
	if (state == 0) {
		keep = conn[x];
	}
	if (keep != -1) {
		g2[x].push_back(keep);
		g2[keep].push_back(x);
		dfs2(keep, 1);
	}
	for (int v : broken[x]) {
		if (v != keep) {
			dfs2(v, 0);
		}
	}
	for (int v : unbroken[x]) {
		if (v != keep) {
			g2[x].push_back(v);
			g2[v].push_back(x);
			dfs2(v, 1);
		}
	}
}

void dfs3(int x, int p) {
	seen[x] = 1;
	order.push_back(x);
	for (int v : g2[x]) if (v != p) dfs3(v, x);
}

void dfs4(int x, int p) {
	sz[x] = 1;
	for (int v : graph[x]) if (v != p) {
		dfs4(v, x);
		tot += min(sz[v], N - sz[v]);
		sz[x] += sz[v];
	}
}

void dfs5(int x, int p) {
	nds.push_back(x);
	for (int v : graph[x]) if (v != p) dfs5(v, x);
}

int main() {
	scanf("%d", &N);
	for (int i = 0; i < N-1; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs(1, 1);
	dfs2(1, 0); // Create g2
	for (int i = 1; i <= N; i++) {
		if (!seen[i]) {
			order.clear();
			dfs3(i, i);
			for (int j = 0; j < order.size(); j++) {
				ans[order[j]] = order[(j+1)%order.size()];
			}
		}
	}
	// Find centroid
	dfs4(1, 1);
	int cent = 1, rip = 1;
	while (rip) {
		rip = 0;
		for (int v : graph[cent]) {
			if (sz[v] < sz[cent] && sz[v] > (sz[1] / 2)) {
				rip = 1;
				cent = v;
				break;
			}
		}
	}	
	// Cent is centroid
	for (int v : graph[cent]) {
		dfs5(v, cent);
	}
	if (nds.size() & 1) {
		nds.push_back(cent);
		int of = nds.size() / 2;
		for (int i = 0; i + of < nds.size(); i++) {
			ans2[nds[i]] = nds[i + of];
			ans2[nds[i+of]] = nds[i];
		}
	} else {
		int of = nds.size() / 2;
		ans2[nds[0]] = cent;
		ans2[cent] = nds[of];
		ans2[nds[of]] = nds[0];
		for (int i = 1; i + of < nds.size(); i++) {
			ans2[nds[i]] = nds[i+of];
			ans2[nds[i+of]] = nds[i];
		}
	}
	printf("%d %lld\n", 2 * (N-1-dp[1][0]), 2 * tot);
	for (int i = 1; i <= N; i++) {
		if (i == 1) printf("%d", ans[i]);
		else printf(" %d", ans[i]);
	} printf("\n");
	for (int i = 1; i <= N; i++) {
		if (i == 1) printf("%d", ans2[i]);
		else printf(" %d", ans2[i]);
	} printf("\n");
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:98:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |    for (int j = 0; j < order.size(); j++) {
      |                    ~~^~~~~~~~~~~~~~
Village.cpp:123:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |   for (int i = 0; i + of < nds.size(); i++) {
      |                   ~~~~~~~^~~~~~~~~~~~
Village.cpp:132:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |   for (int i = 1; i + of < nds.size(); i++) {
      |                   ~~~~~~~^~~~~~~~~~~~
Village.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
Village.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   88 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 7 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 6 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9856 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 8 ms 9856 KB Output is correct
8 Correct 8 ms 9900 KB Output is correct
9 Correct 8 ms 9888 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 7 ms 9856 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 8 ms 9856 KB Output is correct
15 Correct 8 ms 9856 KB Output is correct
16 Correct 9 ms 9984 KB Output is correct
17 Correct 8 ms 9856 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 8 ms 9856 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 7 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 7 ms 9856 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 8 ms 9856 KB Output is correct
27 Correct 7 ms 9776 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 8 ms 9856 KB Output is correct
30 Correct 8 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 7 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 6 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 8 ms 9856 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 8 ms 9900 KB Output is correct
26 Correct 8 ms 9888 KB Output is correct
27 Correct 8 ms 9856 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 7 ms 9856 KB Output is correct
31 Correct 8 ms 9856 KB Output is correct
32 Correct 8 ms 9856 KB Output is correct
33 Correct 9 ms 9984 KB Output is correct
34 Correct 8 ms 9856 KB Output is correct
35 Correct 7 ms 9856 KB Output is correct
36 Correct 8 ms 9856 KB Output is correct
37 Correct 8 ms 9856 KB Output is correct
38 Correct 8 ms 9856 KB Output is correct
39 Correct 7 ms 9856 KB Output is correct
40 Correct 7 ms 9856 KB Output is correct
41 Correct 7 ms 9856 KB Output is correct
42 Correct 7 ms 9728 KB Output is correct
43 Correct 8 ms 9856 KB Output is correct
44 Correct 7 ms 9776 KB Output is correct
45 Correct 8 ms 9856 KB Output is correct
46 Correct 8 ms 9856 KB Output is correct
47 Correct 8 ms 9856 KB Output is correct
48 Correct 146 ms 21360 KB Output is correct
49 Correct 167 ms 22640 KB Output is correct
50 Correct 170 ms 22512 KB Output is correct
51 Correct 126 ms 19696 KB Output is correct
52 Correct 160 ms 22384 KB Output is correct
53 Correct 146 ms 21104 KB Output is correct
54 Correct 81 ms 21752 KB Output is correct
55 Correct 196 ms 34248 KB Output is correct
56 Correct 192 ms 28144 KB Output is correct
57 Correct 190 ms 26224 KB Output is correct
58 Correct 184 ms 24756 KB Output is correct
59 Correct 162 ms 22384 KB Output is correct
60 Correct 100 ms 21612 KB Output is correct
61 Correct 116 ms 21348 KB Output is correct
62 Correct 133 ms 21752 KB Output is correct
63 Correct 116 ms 21116 KB Output is correct
64 Correct 139 ms 22004 KB Output is correct
65 Correct 124 ms 22136 KB Output is correct
66 Correct 115 ms 21232 KB Output is correct
67 Correct 77 ms 18416 KB Output is correct
68 Correct 108 ms 20336 KB Output is correct
69 Correct 117 ms 22256 KB Output is correct
70 Correct 116 ms 21488 KB Output is correct
71 Correct 81 ms 18428 KB Output is correct
72 Correct 92 ms 19576 KB Output is correct
73 Correct 118 ms 22256 KB Output is correct
74 Correct 105 ms 21104 KB Output is correct
75 Correct 168 ms 22000 KB Output is correct
76 Correct 158 ms 21492 KB Output is correct
77 Correct 129 ms 21612 KB Output is correct
78 Correct 90 ms 17776 KB Output is correct
79 Correct 107 ms 18932 KB Output is correct
80 Correct 164 ms 22000 KB Output is correct
81 Correct 134 ms 21868 KB Output is correct
82 Correct 122 ms 21620 KB Output is correct