Submission #291663

# Submission time Handle Problem Language Result Execution time Memory
291663 2020-09-05T15:35:56 Z spdskatr Village (BOI20_village) C++14
75 / 100
202 ms 35436 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], 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 %d\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:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |    for (int j = 0; j < order.size(); j++) {
      |                    ~~^~~~~~~~~~~~~~
Village.cpp:122:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |   for (int i = 0; i + of < nds.size(); i++) {
      |                   ~~~~~~~^~~~~~~~~~~~
Village.cpp:131:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   for (int i = 1; i + of < nds.size(); i++) {
      |                   ~~~~~~~^~~~~~~~~~~~
Village.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
Village.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 8 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 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
# Verdict Execution time Memory 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 9984 KB Output is correct
8 Correct 8 ms 9984 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 8 ms 9856 KB Output is correct
14 Correct 8 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 8 ms 9856 KB Output is correct
17 Correct 8 ms 9856 KB Output is correct
18 Correct 8 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 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 7 ms 9728 KB Output is correct
26 Correct 8 ms 9856 KB Output is correct
27 Correct 7 ms 9728 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
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 8 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 7 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 9984 KB Output is correct
25 Correct 8 ms 9984 KB Output is correct
26 Correct 8 ms 9856 KB Output is correct
27 Correct 8 ms 9856 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
31 Correct 8 ms 9856 KB Output is correct
32 Correct 7 ms 9856 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
34 Correct 8 ms 9856 KB Output is correct
35 Correct 8 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 8 ms 9856 KB Output is correct
40 Correct 8 ms 9856 KB Output is correct
41 Correct 8 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 9728 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 148 ms 22368 KB Output is correct
49 Correct 171 ms 23792 KB Output is correct
50 Correct 161 ms 23664 KB Output is correct
51 Correct 120 ms 20592 KB Output is correct
52 Correct 165 ms 23408 KB Output is correct
53 Correct 139 ms 22128 KB Output is correct
54 Correct 84 ms 22244 KB Output is correct
55 Partially correct 202 ms 35436 KB Partially correct
56 Partially correct 191 ms 29176 KB Partially correct
57 Correct 183 ms 27376 KB Output is correct
58 Correct 178 ms 25964 KB Output is correct
59 Correct 162 ms 23536 KB Output is correct
60 Correct 109 ms 22636 KB Output is correct
61 Correct 119 ms 22512 KB Output is correct
62 Correct 129 ms 22908 KB Output is correct
63 Correct 116 ms 22144 KB Output is correct
64 Correct 134 ms 23156 KB Output is correct
65 Correct 121 ms 23288 KB Output is correct
66 Correct 107 ms 22256 KB Output is correct
67 Correct 87 ms 19312 KB Output is correct
68 Correct 108 ms 21232 KB Output is correct
69 Correct 120 ms 23404 KB Output is correct
70 Correct 111 ms 22512 KB Output is correct
71 Correct 78 ms 19196 KB Output is correct
72 Correct 92 ms 20468 KB Output is correct
73 Correct 120 ms 23280 KB Output is correct
74 Correct 106 ms 22128 KB Output is correct
75 Correct 159 ms 23152 KB Output is correct
76 Correct 153 ms 22640 KB Output is correct
77 Correct 133 ms 22772 KB Output is correct
78 Correct 86 ms 18544 KB Output is correct
79 Correct 100 ms 19824 KB Output is correct
80 Correct 162 ms 23132 KB Output is correct
81 Correct 130 ms 22896 KB Output is correct
82 Correct 118 ms 22776 KB Output is correct