Submission #1093130

# Submission time Handle Problem Language Result Execution time Memory
1093130 2024-09-26T02:15:09 Z Jawad_Akbar_JJ Village (BOI20_village) C++17
25 / 100
84 ms 20652 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)
			continue;
		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 : nei[rt])
		vec.push_back({ch[i], 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++];
	get(vec[0].second, rt, 1);
	for (int i : lst)
		Num[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();

	cout<<Mn + 1<<" "<<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:65: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]
   65 |  for (int i=1;i<vec.size();i++){
      |               ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:87:11: warning: division by zero [-Wdiv-by-zero]
   87 |   cout<<1 / 0;
      |         ~~^~~
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 2652 KB Partially correct
2 Partially correct 1 ms 2652 KB Partially correct
3 Partially correct 1 ms 2652 KB Partially correct
4 Partially correct 1 ms 2652 KB Partially correct
5 Partially correct 1 ms 2652 KB Partially correct
6 Partially correct 1 ms 2652 KB Partially correct
7 Partially correct 1 ms 2648 KB Partially correct
8 Partially correct 1 ms 2652 KB Partially correct
9 Partially correct 2 ms 2652 KB Partially correct
10 Partially correct 1 ms 2652 KB Partially correct
11 Partially correct 1 ms 2652 KB Partially correct
12 Partially correct 1 ms 2652 KB Partially correct
13 Partially correct 1 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 2 ms 2652 KB Partially correct
17 Partially correct 1 ms 2652 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 2652 KB Partially correct
2 Partially correct 2 ms 2652 KB Partially correct
3 Partially correct 2 ms 2652 KB Partially correct
4 Partially correct 2 ms 2652 KB Partially correct
5 Partially correct 2 ms 2652 KB Partially correct
6 Partially correct 2 ms 2648 KB Partially correct
7 Partially correct 3 ms 2908 KB Partially correct
8 Partially correct 2 ms 2648 KB Partially correct
9 Partially correct 2 ms 2652 KB Partially correct
10 Partially correct 2 ms 2652 KB Partially correct
11 Partially correct 2 ms 2652 KB Partially correct
12 Partially correct 2 ms 2652 KB Partially correct
13 Partially correct 2 ms 2652 KB Partially correct
14 Partially correct 2 ms 2652 KB Partially correct
15 Partially correct 2 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 3 ms 2652 KB Partially correct
23 Partially correct 3 ms 2652 KB Partially correct
24 Partially correct 2 ms 2652 KB Partially correct
25 Partially correct 2 ms 2652 KB Partially correct
26 Partially correct 2 ms 2652 KB Partially correct
27 Partially correct 2 ms 2652 KB Partially correct
28 Partially correct 2 ms 2652 KB Partially correct
29 Partially correct 1 ms 2652 KB Partially correct
30 Partially correct 2 ms 2652 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 2652 KB Partially correct
2 Partially correct 1 ms 2652 KB Partially correct
3 Partially correct 1 ms 2652 KB Partially correct
4 Partially correct 1 ms 2652 KB Partially correct
5 Partially correct 1 ms 2652 KB Partially correct
6 Partially correct 1 ms 2652 KB Partially correct
7 Partially correct 1 ms 2648 KB Partially correct
8 Partially correct 1 ms 2652 KB Partially correct
9 Partially correct 2 ms 2652 KB Partially correct
10 Partially correct 1 ms 2652 KB Partially correct
11 Partially correct 1 ms 2652 KB Partially correct
12 Partially correct 1 ms 2652 KB Partially correct
13 Partially correct 1 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 2 ms 2652 KB Partially correct
17 Partially correct 1 ms 2652 KB Partially correct
18 Partially correct 1 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 2 ms 2652 KB Partially correct
23 Partially correct 2 ms 2648 KB Partially correct
24 Partially correct 3 ms 2908 KB Partially correct
25 Partially correct 2 ms 2648 KB Partially correct
26 Partially correct 2 ms 2652 KB Partially correct
27 Partially correct 2 ms 2652 KB Partially correct
28 Partially correct 2 ms 2652 KB Partially correct
29 Partially correct 2 ms 2652 KB Partially correct
30 Partially correct 2 ms 2652 KB Partially correct
31 Partially correct 2 ms 2652 KB Partially correct
32 Partially correct 2 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 3 ms 2652 KB Partially correct
40 Partially correct 3 ms 2652 KB Partially correct
41 Partially correct 2 ms 2652 KB Partially correct
42 Partially correct 2 ms 2652 KB Partially correct
43 Partially correct 2 ms 2652 KB Partially correct
44 Partially correct 2 ms 2652 KB Partially correct
45 Partially correct 2 ms 2652 KB Partially correct
46 Partially correct 1 ms 2652 KB Partially correct
47 Partially correct 2 ms 2652 KB Partially correct
48 Partially correct 62 ms 8136 KB Partially correct
49 Partially correct 79 ms 8588 KB Partially correct
50 Partially correct 76 ms 8592 KB Partially correct
51 Partially correct 53 ms 7404 KB Partially correct
52 Partially correct 73 ms 8456 KB Partially correct
53 Partially correct 60 ms 7848 KB Partially correct
54 Partially correct 42 ms 11408 KB Partially correct
55 Incorrect 84 ms 20652 KB Output isn't correct
56 Halted 0 ms 0 KB -