Submission #1092988

# Submission time Handle Problem Language Result Execution time Memory
1092988 2024-09-25T15:19:06 Z Jawad_Akbar_JJ Village (BOI20_village) C++17
25 / 100
80 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];
		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;
		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:57:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for (int i=0;i<nei[rt].size();i++)
      |               ~^~~~~~~~~~~~~~~
Village.cpp:62: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]
   62 |  for (int i=1;i<vec.size();i++){
      |               ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:83:11: warning: division by zero [-Wdiv-by-zero]
   83 |   cout<<1 / 0;
      |         ~~^~~
# Verdict Execution time Memory 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 1 ms 2792 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2648 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 2648 KB Output is correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Partially correct 2 ms 2792 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 2648 KB Partially correct
2 Partially correct 2 ms 2652 KB Partially correct
3 Partially correct 2 ms 2652 KB Partially correct
4 Partially correct 3 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 2908 KB Output is correct
8 Partially correct 2 ms 2652 KB Partially correct
9 Partially correct 2 ms 2860 KB Partially correct
10 Partially correct 1 ms 2908 KB Partially correct
11 Partially correct 1 ms 2652 KB Partially correct
12 Correct 2 ms 2648 KB Output is correct
13 Correct 2 ms 2852 KB Output is correct
14 Partially correct 3 ms 2904 KB Partially correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 2 ms 2652 KB Partially correct
17 Partially correct 2 ms 2652 KB Partially correct
18 Partially correct 2 ms 2652 KB Partially correct
19 Partially correct 2 ms 2652 KB Partially correct
20 Partially correct 2 ms 2652 KB Partially correct
21 Partially correct 2 ms 2652 KB Partially correct
22 Partially correct 1 ms 2652 KB Partially correct
23 Partially correct 2 ms 2652 KB Partially correct
24 Partially correct 2 ms 2652 KB Partially correct
25 Partially correct 1 ms 2652 KB Partially correct
26 Partially correct 2 ms 2648 KB Partially correct
27 Partially correct 2 ms 2652 KB Partially correct
28 Partially correct 2 ms 2792 KB Partially correct
29 Partially correct 2 ms 2652 KB Partially correct
30 Partially correct 1 ms 2652 KB Partially correct
# Verdict Execution time Memory 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 1 ms 2792 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2648 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 2648 KB Output is correct
15 Partially correct 1 ms 2652 KB Partially correct
16 Partially correct 1 ms 2652 KB Partially correct
17 Partially correct 2 ms 2792 KB Partially correct
18 Partially correct 1 ms 2648 KB Partially correct
19 Partially correct 2 ms 2652 KB Partially correct
20 Partially correct 2 ms 2652 KB Partially correct
21 Partially correct 3 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 2908 KB Output is correct
25 Partially correct 2 ms 2652 KB Partially correct
26 Partially correct 2 ms 2860 KB Partially correct
27 Partially correct 1 ms 2908 KB Partially correct
28 Partially correct 1 ms 2652 KB Partially correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2852 KB Output is correct
31 Partially correct 3 ms 2904 KB Partially correct
32 Partially correct 1 ms 2652 KB Partially correct
33 Partially correct 2 ms 2652 KB Partially correct
34 Partially correct 2 ms 2652 KB Partially correct
35 Partially correct 2 ms 2652 KB Partially correct
36 Partially correct 2 ms 2652 KB Partially correct
37 Partially correct 2 ms 2652 KB Partially correct
38 Partially correct 2 ms 2652 KB Partially correct
39 Partially correct 1 ms 2652 KB Partially correct
40 Partially correct 2 ms 2652 KB Partially correct
41 Partially correct 2 ms 2652 KB Partially correct
42 Partially correct 1 ms 2652 KB Partially correct
43 Partially correct 2 ms 2648 KB Partially correct
44 Partially correct 2 ms 2652 KB Partially correct
45 Partially correct 2 ms 2792 KB Partially correct
46 Partially correct 2 ms 2652 KB Partially correct
47 Partially correct 1 ms 2652 KB Partially correct
48 Partially correct 60 ms 9180 KB Partially correct
49 Partially correct 67 ms 9840 KB Partially correct
50 Partially correct 64 ms 9616 KB Partially correct
51 Partially correct 51 ms 8176 KB Partially correct
52 Partially correct 66 ms 9736 KB Partially correct
53 Partially correct 58 ms 8876 KB Partially correct
54 Correct 39 ms 12488 KB Output is correct
55 Partially correct 80 ms 23236 KB Partially correct
56 Incorrect 74 ms 16188 KB Output isn't correct
57 Halted 0 ms 0 KB -