답안 #806729

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
806729 2023-08-04T09:16:16 Z pawned Village (BOI20_village) C++17
56 / 100
100 ms 16136 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

int N;

vector<ii> edges;
vi adj[100005];

pair<int, vi> minimumans;

void bruteforce() {

	vector<vi> dist(N, vi(N, 1e9));
	for (int i = 0; i < N; i++) {	// starting node
		queue<int> q;
		dist[i][i] = 0;
		q.push(i);
		while (!q.empty()) {
			int x = q.front();
			q.pop();
			for (int j : adj[x]) {
				if (dist[i][j] == 1e9) {
					dist[i][j] = dist[i][x] + 1;
					q.push(j);
				}
			}
		}
	}
/*
	cout<<"dist: "<<endl;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cout<<dist[i][j]<<" ";
		}
		cout<<endl;
	}
*/
	vi perm(N);
	for (int i = 0; i < N; i++) {
		perm[i] = i;
	}
	int minimum = 1e9;
	vi minans;
	int maximum = 0;
	vi maxans;
	int count = 0;
	do {
		bool can = true;
		for (int i = 0; i < N; i++) {
			if (perm[i] == i)
				can = false;
		}
		if (!can)
			continue;
		count++;
		int total = 0;
		for (int i = 0; i < N; i++) {
			total += dist[i][perm[i]];
		}
		if (total < minimum) {
			minimum = total;
			minans = perm;
		}
		if (total > maximum) {
			maximum = total;
			maxans = perm;
		}
	} while (next_permutation(perm.begin(), perm.begin() + N));
//	cout<<"count: "<<count<<endl;
/*
	cout<<minimum<<" "<<maximum<<endl;
	for (int i = 0; i < N; i++) {
		cout<<(minans[i] + 1)<<" ";
	}
	cout<<endl;
	for (int i = 0; i < N; i++) {
		cout<<(maxans[i] + 1)<<" ";
	}
	cout<<endl;
*/
	minimumans.fi = minimum;
	minimumans.se = minans;
}

int subtree_size[100005];

bool vis[100005];

void dfs(int n) {
	vis[n] = true;
	subtree_size[n] = 1;
	for (int i : adj[n]) {
		if (!vis[i]) {
			dfs(i);
			subtree_size[n] += subtree_size[i];
		}
	}
}

bool vis2[100005];

int dfs2(int n) {
//	cout<<"at "<<n<<endl;
	vis2[n] = true;
	bool works = true;
	for (int i : adj[n]) {
		if (!vis2[i]) {
			if (subtree_size[i] > N / 2) {
				works = false;
				return dfs2(i);
			}
		}
	}
	if (works) {
//		cout<<"yay "<<n<<endl;
		return n;
	}
	return -1;
}

vi order;

bool vis3[100005];

void dfs3(int n) {
	vis3[n] = true;
	order.pb(n);
	for (int i : adj[n]) {
		if (!vis3[i]) {
			dfs3(i);
		}
	}
}

int main() {
	cin>>N;
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin>>u>>v;
		u--; v--;
		edges.pb({u, v});
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(0);
/*
	cout<<"subtree size: ";
	for (int i = 0; i < N; i++) {
		cout<<subtree_size[i]<<" ";
	}
	cout<<endl;
*/
	ll total = 0;
	for (int i = 0; i < N - 1; i++) {
		int u = edges[i].fi;
		int v = edges[i].se;
		if (subtree_size[u] < subtree_size[v])	// u is parent of v
			swap(u, v);
		total += min(subtree_size[v], N - subtree_size[v]);
	}
	total *= 2;
	int centroid = dfs2(0);	// find centroid
//	cout<<"centroid: "<<centroid<<endl;
	dfs3(centroid);	// need order
/*
	cout<<"order: ";
	for (int i : order)
		cout<<i<<" ";
	cout<<endl;
*/
	int ans[N];
	if (N % 2 == 0) {
		for (int i = 0; i < N / 2; i++) {
			ans[order[i]] = order[i + N / 2];
			ans[order[i + N / 2]] = order[i];
		}
	}
	else {
		ans[order[0]] = order[0];
		for (int i = 0; i < N / 2; i++) {
			ans[order[i + 1]] = order[i + N / 2 + 1];
			ans[order[i + N / 2 + 1]] = order[i + 1];
		}
		swap(ans[order[0]], ans[order[1]]);
	}
//	cout<<"ANSWER: "<<endl;
	if (N <= 10) {
		bruteforce();
	}
	else {
		minimumans.fi = 0;
		minimumans.se = vi(N, 1);
	}
	cout<<minimumans.fi<<" "<<total<<endl;
	for (int i = 0; i < N; i++) {
		cout<<(minimumans.se[i] + 1)<<" ";
	}
	cout<<endl;
	for (int i = 0; i < N; i++) {
		cout<<(ans[i] + 1)<<" ";
	}
	cout<<endl;
}

// find centroid, do dfs order, then map i to i + N / 2
// only important is that start and end never belong to same tree
// n = 8: (1, 2, 3, 4) <=> (5, 6, 7, 8)
// n = 9: (2, 3, 4, 5) <=> (6, 7, 8, 9), then swap the "goto" for
// 1 and 2 (then 1 -> 1 and 2 -> 6 becomes 1 -> 6 and 2 -> 1)
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2660 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2656 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 5 ms 2644 KB Output is correct
10 Correct 44 ms 2648 KB Output is correct
11 Correct 42 ms 2644 KB Output is correct
12 Correct 42 ms 2644 KB Output is correct
13 Correct 42 ms 2644 KB Output is correct
14 Correct 41 ms 2644 KB Output is correct
15 Correct 43 ms 2644 KB Output is correct
16 Correct 42 ms 2652 KB Output is correct
17 Correct 44 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 2644 KB Partially correct
2 Partially correct 2 ms 2644 KB Partially correct
3 Partially correct 2 ms 2644 KB Partially correct
4 Partially correct 2 ms 2660 KB Partially correct
5 Partially correct 2 ms 2664 KB Partially correct
6 Partially correct 2 ms 2664 KB Partially correct
7 Partially correct 2 ms 2644 KB Partially correct
8 Partially correct 3 ms 2644 KB Partially correct
9 Partially correct 3 ms 2644 KB Partially correct
10 Partially correct 3 ms 2644 KB Partially correct
11 Partially correct 2 ms 2644 KB Partially correct
12 Partially correct 3 ms 2644 KB Partially correct
13 Partially correct 2 ms 2644 KB Partially correct
14 Partially correct 3 ms 2736 KB Partially correct
15 Partially correct 2 ms 2644 KB Partially correct
16 Partially correct 2 ms 2644 KB Partially correct
17 Partially correct 2 ms 2660 KB Partially correct
18 Partially correct 2 ms 2668 KB Partially correct
19 Partially correct 2 ms 2640 KB Partially correct
20 Partially correct 2 ms 2672 KB Partially correct
21 Partially correct 2 ms 2644 KB Partially correct
22 Partially correct 2 ms 2668 KB Partially correct
23 Partially correct 2 ms 2644 KB Partially correct
24 Partially correct 2 ms 2644 KB Partially correct
25 Partially correct 2 ms 2644 KB Partially correct
26 Partially correct 2 ms 2660 KB Partially correct
27 Partially correct 3 ms 2652 KB Partially correct
28 Partially correct 2 ms 2644 KB Partially correct
29 Partially correct 2 ms 2644 KB Partially correct
30 Partially correct 2 ms 2652 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2660 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2656 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 5 ms 2644 KB Output is correct
10 Correct 44 ms 2648 KB Output is correct
11 Correct 42 ms 2644 KB Output is correct
12 Correct 42 ms 2644 KB Output is correct
13 Correct 42 ms 2644 KB Output is correct
14 Correct 41 ms 2644 KB Output is correct
15 Correct 43 ms 2644 KB Output is correct
16 Correct 42 ms 2652 KB Output is correct
17 Correct 44 ms 2648 KB Output is correct
18 Partially correct 1 ms 2644 KB Partially correct
19 Partially correct 2 ms 2644 KB Partially correct
20 Partially correct 2 ms 2644 KB Partially correct
21 Partially correct 2 ms 2660 KB Partially correct
22 Partially correct 2 ms 2664 KB Partially correct
23 Partially correct 2 ms 2664 KB Partially correct
24 Partially correct 2 ms 2644 KB Partially correct
25 Partially correct 3 ms 2644 KB Partially correct
26 Partially correct 3 ms 2644 KB Partially correct
27 Partially correct 3 ms 2644 KB Partially correct
28 Partially correct 2 ms 2644 KB Partially correct
29 Partially correct 3 ms 2644 KB Partially correct
30 Partially correct 2 ms 2644 KB Partially correct
31 Partially correct 3 ms 2736 KB Partially correct
32 Partially correct 2 ms 2644 KB Partially correct
33 Partially correct 2 ms 2644 KB Partially correct
34 Partially correct 2 ms 2660 KB Partially correct
35 Partially correct 2 ms 2668 KB Partially correct
36 Partially correct 2 ms 2640 KB Partially correct
37 Partially correct 2 ms 2672 KB Partially correct
38 Partially correct 2 ms 2644 KB Partially correct
39 Partially correct 2 ms 2668 KB Partially correct
40 Partially correct 2 ms 2644 KB Partially correct
41 Partially correct 2 ms 2644 KB Partially correct
42 Partially correct 2 ms 2644 KB Partially correct
43 Partially correct 2 ms 2660 KB Partially correct
44 Partially correct 3 ms 2652 KB Partially correct
45 Partially correct 2 ms 2644 KB Partially correct
46 Partially correct 2 ms 2644 KB Partially correct
47 Partially correct 2 ms 2652 KB Partially correct
48 Partially correct 87 ms 9888 KB Partially correct
49 Partially correct 76 ms 10524 KB Partially correct
50 Partially correct 75 ms 10532 KB Partially correct
51 Partially correct 61 ms 8932 KB Partially correct
52 Partially correct 83 ms 10372 KB Partially correct
53 Partially correct 69 ms 9740 KB Partially correct
54 Partially correct 55 ms 9128 KB Partially correct
55 Partially correct 89 ms 16136 KB Partially correct
56 Partially correct 89 ms 12860 KB Partially correct
57 Partially correct 82 ms 11936 KB Partially correct
58 Partially correct 77 ms 11124 KB Partially correct
59 Partially correct 81 ms 10512 KB Partially correct
60 Partially correct 75 ms 10580 KB Partially correct
61 Partially correct 74 ms 10812 KB Partially correct
62 Partially correct 69 ms 10844 KB Partially correct
63 Partially correct 68 ms 10448 KB Partially correct
64 Partially correct 100 ms 10788 KB Partially correct
65 Partially correct 70 ms 10852 KB Partially correct
66 Partially correct 65 ms 10372 KB Partially correct
67 Partially correct 49 ms 8748 KB Partially correct
68 Partially correct 61 ms 9756 KB Partially correct
69 Partially correct 70 ms 10960 KB Partially correct
70 Partially correct 65 ms 10508 KB Partially correct
71 Partially correct 52 ms 8536 KB Partially correct
72 Partially correct 57 ms 9276 KB Partially correct
73 Partially correct 67 ms 10900 KB Partially correct
74 Partially correct 75 ms 10264 KB Partially correct
75 Partially correct 83 ms 10404 KB Partially correct
76 Partially correct 73 ms 10324 KB Partially correct
77 Partially correct 69 ms 10584 KB Partially correct
78 Partially correct 49 ms 8140 KB Partially correct
79 Partially correct 54 ms 8908 KB Partially correct
80 Partially correct 75 ms 10304 KB Partially correct
81 Partially correct 70 ms 10624 KB Partially correct
82 Partially correct 69 ms 10652 KB Partially correct