답안 #314420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
314420 2020-10-19T20:23:26 Z sofapuden Village (BOI20_village) C++14
100 / 100
118 ms 20088 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector<vector<ll>> gra;
vector<ll> posmin, posmax, sz, dec1, dec2;

vector<bool> used;

ll ansmin = 0, ansmax = 0, n, cnt = 0;

void dfs(int x, int p){
	sz[x] = 1;
	for(auto i : gra[x]){
		if(i == p)continue;
		dfs(i,x);
		sz[x]+=sz[i];
		if(used[i])continue;
		used[x] = true;
		swap(posmin[x],posmin[i]);
		ansmin += 2;
	}
	if(x)ansmax += min(sz[x], n-sz[x])*2;
	else if(!used[0]){
		swap(posmin[0], posmin[gra[0][0]]);
		ansmin+=2;
	}
}
void findroot(int x, int p){
	for(auto i : gra[x]){
		if(i == p)continue;
		if(sz[i] > n/2){
			dec1[i] = 0;
			dec2[0] = i;
			findroot(i,x);
		}
	}
}
void encode(int x, int p){
	dec1[cnt] = x;
	dec2[x] = cnt++;
	for(auto i : gra[x]){
		if(i == p)continue;
		encode(i,x);
	}
}
	
int main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	cin >> n;
	gra.resize(n);
	posmin.resize(n);
	posmax.resize(n);
	dec1.resize(n);
	dec2.resize(n);
	sz.resize(n);
	used.resize(n,false);
	iota(posmin.begin(), posmin.end(), 0);
	iota(posmax.begin(), posmax.end(), 0);
	for(int i = 0; i < n-1; ++i){
		int a, b; cin >> a >> b; a--; b--;
		gra[a].push_back(b);
		gra[b].push_back(a);
	}
	dfs(0,0);
	findroot(0,0);
	encode(dec1[0],dec1[0]);
	for(int i = 0; i < n; ++i){
		posmax[i] = dec1[(dec2[i]+n/2)%n];
	}
	cout << ansmin << " " << ansmax << "\n";
	for(auto x : posmin)cout << x+1 << " ";
	cout << "\n";
	for(auto x : posmax)cout << x+1 << " ";
	cout << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 640 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 1 ms 512 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 81 ms 10616 KB Output is correct
49 Correct 89 ms 11640 KB Output is correct
50 Correct 90 ms 11640 KB Output is correct
51 Correct 62 ms 9084 KB Output is correct
52 Correct 90 ms 11516 KB Output is correct
53 Correct 78 ms 10360 KB Output is correct
54 Correct 43 ms 9976 KB Output is correct
55 Correct 118 ms 20088 KB Output is correct
56 Correct 107 ms 15864 KB Output is correct
57 Correct 105 ms 14200 KB Output is correct
58 Correct 96 ms 13048 KB Output is correct
59 Correct 90 ms 11768 KB Output is correct
60 Correct 63 ms 11760 KB Output is correct
61 Correct 62 ms 11752 KB Output is correct
62 Correct 74 ms 12024 KB Output is correct
63 Correct 65 ms 11256 KB Output is correct
64 Correct 83 ms 12024 KB Output is correct
65 Correct 90 ms 12152 KB Output is correct
66 Correct 80 ms 11260 KB Output is correct
67 Correct 50 ms 8820 KB Output is correct
68 Correct 66 ms 10360 KB Output is correct
69 Correct 93 ms 12152 KB Output is correct
70 Correct 67 ms 11384 KB Output is correct
71 Correct 50 ms 8568 KB Output is correct
72 Correct 52 ms 9592 KB Output is correct
73 Correct 71 ms 12152 KB Output is correct
74 Correct 62 ms 11128 KB Output is correct
75 Correct 87 ms 12024 KB Output is correct
76 Correct 84 ms 11512 KB Output is correct
77 Correct 78 ms 11640 KB Output is correct
78 Correct 47 ms 8056 KB Output is correct
79 Correct 55 ms 9208 KB Output is correct
80 Correct 97 ms 12024 KB Output is correct
81 Correct 75 ms 11896 KB Output is correct
82 Correct 71 ms 11640 KB Output is correct