답안 #1092987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092987 2024-09-25T15:14:50 Z Jawad_Akbar_JJ Village (BOI20_village) C++17
25 / 100
85 ms 23240 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];
		Mx += min(n - ch[i], ch[i]);
	}
	if (c == 0)
		return;
	vector<int> vec = {u};
	for (int i : nei[u]){
		if (i == p or ch[i] % 2 == 0)
			continue;
		Num2[i] = vec.back();
		vec.push_back(i);
	}
	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:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |  for (int i=0;i<nei[rt].size();i++)
      |               ~^~~~~~~~~~~~~~~
Village.cpp:61: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]
   61 |  for (int i=1;i<vec.size();i++){
      |               ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:82:11: warning: division by zero [-Wdiv-by-zero]
   82 |   cout<<1 / 0;
      |         ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 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 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Partially correct 1 ms 2652 KB Partially 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 2 ms 2652 KB Partially correct
14 Partially correct 1 ms 2652 KB Partially correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Partially correct 3 ms 2652 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 2652 KB Partially correct
2 Partially correct 1 ms 2652 KB Partially correct
3 Partially correct 2 ms 2648 KB Partially correct
4 Partially correct 2 ms 2652 KB Partially correct
5 Partially correct 2 ms 2648 KB Partially correct
6 Partially correct 2 ms 2652 KB Partially correct
7 Correct 2 ms 2904 KB Output is correct
8 Partially correct 2 ms 2652 KB Partially correct
9 Partially correct 2 ms 2652 KB Partially correct
10 Partially correct 2 ms 2824 KB Partially correct
11 Partially correct 2 ms 2652 KB Partially correct
12 Partially correct 2 ms 2652 KB Partially correct
13 Correct 2 ms 2652 KB Output is correct
14 Partially correct 2 ms 2652 KB Partially correct
15 Partially correct 2 ms 2652 KB Partially correct
16 Partially correct 2 ms 2664 KB Partially correct
17 Partially correct 2 ms 2796 KB Partially correct
18 Partially correct 2 ms 2652 KB Partially correct
19 Partially correct 2 ms 2648 KB Partially correct
20 Partially correct 2 ms 2652 KB Partially correct
21 Partially correct 2 ms 2648 KB Partially correct
22 Partially correct 2 ms 2652 KB Partially correct
23 Partially correct 2 ms 2648 KB Partially correct
24 Partially correct 2 ms 2664 KB Partially correct
25 Partially correct 2 ms 2652 KB Partially correct
26 Partially correct 2 ms 2792 KB Partially correct
27 Partially correct 2 ms 2652 KB Partially correct
28 Partially correct 2 ms 2796 KB Partially correct
29 Partially correct 2 ms 2652 KB Partially correct
30 Partially correct 2 ms 2652 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 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 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Partially correct 1 ms 2652 KB Partially 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 2 ms 2652 KB Partially correct
14 Partially correct 1 ms 2652 KB Partially correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Partially correct 3 ms 2652 KB Partially correct
18 Partially correct 1 ms 2652 KB Partially correct
19 Partially correct 1 ms 2652 KB Partially correct
20 Partially correct 2 ms 2648 KB Partially correct
21 Partially correct 2 ms 2652 KB Partially correct
22 Partially correct 2 ms 2648 KB Partially correct
23 Partially correct 2 ms 2652 KB Partially correct
24 Correct 2 ms 2904 KB Output is correct
25 Partially correct 2 ms 2652 KB Partially correct
26 Partially correct 2 ms 2652 KB Partially correct
27 Partially correct 2 ms 2824 KB Partially correct
28 Partially correct 2 ms 2652 KB Partially correct
29 Partially correct 2 ms 2652 KB Partially correct
30 Correct 2 ms 2652 KB Output is correct
31 Partially correct 2 ms 2652 KB Partially correct
32 Partially correct 2 ms 2652 KB Partially correct
33 Partially correct 2 ms 2664 KB Partially correct
34 Partially correct 2 ms 2796 KB Partially correct
35 Partially correct 2 ms 2652 KB Partially correct
36 Partially correct 2 ms 2648 KB Partially correct
37 Partially correct 2 ms 2652 KB Partially correct
38 Partially correct 2 ms 2648 KB Partially correct
39 Partially correct 2 ms 2652 KB Partially correct
40 Partially correct 2 ms 2648 KB Partially correct
41 Partially correct 2 ms 2664 KB Partially correct
42 Partially correct 2 ms 2652 KB Partially correct
43 Partially correct 2 ms 2792 KB Partially correct
44 Partially correct 2 ms 2652 KB Partially correct
45 Partially correct 2 ms 2796 KB Partially correct
46 Partially correct 2 ms 2652 KB Partially correct
47 Partially correct 2 ms 2652 KB Partially correct
48 Partially correct 68 ms 9172 KB Partially correct
49 Partially correct 67 ms 9796 KB Partially correct
50 Partially correct 69 ms 9844 KB Partially correct
51 Partially correct 54 ms 8172 KB Partially correct
52 Partially correct 79 ms 9736 KB Partially correct
53 Partially correct 61 ms 8884 KB Partially correct
54 Correct 42 ms 12688 KB Output is correct
55 Partially correct 85 ms 23240 KB Partially correct
56 Incorrect 74 ms 16188 KB Output isn't correct
57 Halted 0 ms 0 KB -