답안 #1093126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1093126 2024-09-26T02:06:07 Z Jawad_Akbar_JJ Village (BOI20_village) C++17
50 / 100
86 ms 23236 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
const int N = 1e5 + 10;
vector<int> nei[N], lst;
int Num[N], Num2[N], ch[N], Mx = 0, Mn = 0, n;

void dfs(int u, int p, int c = 0){
	ch[u] = 1;
	for (int i : nei[u]){
		if (i == p)
			continue;
		dfs(i, u, c);
		ch[u] += ch[i];
	}
	if (c == 0)
		return;
	vector<int> vec = {u};
	for (int i : nei[u]){
		if (i == p)
			continue;
		Mx += min(n - ch[i], ch[i]) * 2;
		if (Num2[i] == i or (u == 1 and vec.size() == 1)){
			int k = Num2[i];
			Num2[i] = vec.back();
			vec.push_back(k);
		}
	}
	Num2[u] = vec.back();
	Mn += (vec.size() - 1) * 2;
}

int find_cent(int u, int p){
	for (int i : nei[u])
		if (i != p and ch[i] * 2 > ch[1])
			return find_cent(i, u);
	return u;
}

void get(int u, int p){
	for (int i : nei[u])
		if (i != p)
			get(i, u);
	lst.push_back(u);
}
vector<int> get(int a, int b, int c){
	lst.clear();
	get(a, b);
	return lst;
}

void solve(){
	dfs(1, 1, 1);
	int rt = find_cent(1, 1), ind = 0;
	dfs(rt, rt);
	vector<pair<int,int>> vec;
	for (int i=0;i<nei[rt].size();i++)
		vec.push_back({ch[nei[rt][i]], nei[rt][i]});
	sort(rbegin(vec), rend(vec));

	vector<int> Vec = get(vec[0].second, rt, 1);
	for (int i=1;i<vec.size();i++){
		vector<int> v = get(vec[i].second, rt, 1);
		for (int j : v)
			Num[j] = Vec[ind++], Vec.push_back(j);
	}

	Vec.push_back(rt);
	Num[rt] = Vec[ind++];
	for (int i=0;i<vec[0].first;i++)
		Num[Vec[i]] = Vec[ind++];
}

int main(){
	cin>>n;

	for (int i=1, a, b;i<n;i++){
		cin>>a>>b;
		nei[a].push_back(b);
		nei[b].push_back(a);
	}
	if (n == 1){
		cout<<1 / 0;
		return 0;
	}

	solve();

	if (Num2[1] == 1)
		swap(Num2[nei[1][0]], Num2[1]), Mn += 2;
	cout<<Mn<<" "<<Mx<<'\n';
	for (int i=1;i<=n;i++)
		cout<<Num2[i]<<' ';
	cout<<'\n';
	for (int i=1;i<=n;i++)
		cout<<Num[i]<<' ';
	cout<<'\n';
}

Compilation message

Village.cpp: In function 'void solve()':
Village.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i=0;i<nei[rt].size();i++)
      |               ~^~~~~~~~~~~~~~~
Village.cpp:64:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |  for (int i=1;i<vec.size();i++){
      |               ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:85:11: warning: division by zero [-Wdiv-by-zero]
   85 |   cout<<1 / 0;
      |         ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Partially correct 1 ms 2652 KB Partially correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Partially correct 1 ms 2652 KB Partially correct
14 Correct 1 ms 2652 KB Output is correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Partially correct 2 ms 2652 KB Partially correct
7 Partially correct 2 ms 2748 KB Partially correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 2 ms 2904 KB Output is correct
11 Correct 2 ms 2896 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2876 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Partially correct 2 ms 2652 KB Partially correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2832 KB Output is correct
19 Correct 2 ms 2792 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 3 ms 2652 KB Output is correct
23 Correct 2 ms 2796 KB Output is correct
24 Correct 1 ms 2796 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 2 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Partially correct 2 ms 2652 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Partially correct 1 ms 2652 KB Partially correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Partially correct 1 ms 2652 KB Partially correct
14 Correct 1 ms 2652 KB Output is correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Partially correct 2 ms 2652 KB Partially correct
24 Partially correct 2 ms 2748 KB Partially correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 2 ms 2904 KB Output is correct
28 Correct 2 ms 2896 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2876 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Partially correct 2 ms 2652 KB Partially correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 2 ms 2832 KB Output is correct
36 Correct 2 ms 2792 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 3 ms 2652 KB Output is correct
40 Correct 2 ms 2796 KB Output is correct
41 Correct 1 ms 2796 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 2 ms 2652 KB Output is correct
45 Correct 2 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Partially correct 2 ms 2652 KB Partially correct
48 Partially correct 63 ms 9180 KB Partially correct
49 Correct 68 ms 9840 KB Output is correct
50 Correct 72 ms 9828 KB Output is correct
51 Correct 52 ms 8176 KB Output is correct
52 Partially correct 70 ms 9732 KB Partially correct
53 Correct 61 ms 8864 KB Output is correct
54 Partially correct 42 ms 12688 KB Partially correct
55 Partially correct 85 ms 23236 KB Partially correct
56 Partially correct 86 ms 15968 KB Partially correct
57 Correct 76 ms 13888 KB Output is correct
58 Correct 73 ms 11836 KB Output is correct
59 Correct 74 ms 9816 KB Output is correct
60 Correct 67 ms 10128 KB Output is correct
61 Correct 61 ms 10060 KB Output is correct
62 Correct 62 ms 10088 KB Output is correct
63 Correct 57 ms 9612 KB Output is correct
64 Correct 63 ms 9964 KB Output is correct
65 Correct 58 ms 10276 KB Output is correct
66 Correct 58 ms 9884 KB Output is correct
67 Correct 45 ms 8188 KB Output is correct
68 Correct 66 ms 8912 KB Output is correct
69 Correct 58 ms 10352 KB Output is correct
70 Correct 55 ms 9748 KB Output is correct
71 Correct 41 ms 8048 KB Output is correct
72 Correct 49 ms 8508 KB Output is correct
73 Correct 60 ms 10148 KB Output is correct
74 Correct 55 ms 9716 KB Output is correct
75 Correct 65 ms 9664 KB Output is correct
76 Correct 67 ms 9584 KB Output is correct
77 Correct 58 ms 9664 KB Output is correct
78 Correct 42 ms 7560 KB Output is correct
79 Correct 55 ms 8224 KB Output is correct
80 Correct 69 ms 9676 KB Output is correct
81 Correct 61 ms 9932 KB Output is correct
82 Correct 63 ms 9896 KB Output is correct