Submission #802136

# Submission time Handle Problem Language Result Execution time Memory
802136 2023-08-02T10:14:33 Z amirhoseinfar1385 Village (BOI20_village) C++17
50 / 100
61 ms 18940 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<int>adj[maxn];
int n,m,sz[maxn],linkmax[maxn];
vector<vector<int>>allk;
long long res=0;

void dfs(int u,int par=0)
{
	sz[u]=1;
	for(auto x:adj[u]){
		if(x!=par){
			dfs(x,u);
			sz[u]+=sz[x];
		}
	}
}

int findcen(int u,int ted,int par=0){
	//cout<<u<<" "<<ted<<" "<<par<<" "<<sz[u]<<endl;
	for(auto x:adj[u]){
		if(x!=par&&sz[x]*2>=ted){
			return findcen(x,ted,u);
		}
	}
	//cout<<u<<endl;
	return u;
}

int finds(){
	dfs(1);
	//cout<<"salam"<<endl;
	return findcen(1,sz[1]);
}

int solve(int u,int rishe,int par=0){
	if(u!=rishe){
		allk.back().push_back(u);
	}
	int ret=1;
	for(auto x:adj[u]){
		if(x!=par){
			if(u==rishe){
				allk.push_back({});
			}
			int z=solve(x,rishe,u);
			res+=2*z;
			ret+=z;
		}
	}
	return ret;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	int r=finds();
	//cout<<"salam"<<endl;
	solve(r,r);
	cout<<0<<" "<<res<<"\n";
	for(int i=0;i<n;i++){
		cout<<1<<" ";
	}
	cout<<"\n";
	set<pair<int,int>>st;
	for(int i=0;i<(int)allk.size();i++){
		st.insert(make_pair((int)allk[i].size(),i));
	}
	//cout<<"salam"<<endl;
	while((int)st.size()>1){
		auto x=*st.rbegin();
		st.erase(x);
		auto y=*st.rbegin();
		st.erase(y);
		linkmax[allk[y.second].back()]=allk[x.second].back();
		linkmax[allk[x.second].back()]=allk[y.second].back();
		allk[x.second].pop_back();
		allk[y.second].pop_back();
		x.first--;
		y.first--;
		if(x.first>0){
			st.insert(x);
		}
		if(y.first>0){
			st.insert(y);
		}
	}
	if((int)st.size()>0){
		auto x=*st.rbegin();
		int z=allk[x.second].back();
		linkmax[z]=r;
		linkmax[r]=z;
	}
	else{
		if(r!=1){
			linkmax[r]=1;
			linkmax[linkmax[1]]=r;
		}
		else{
			linkmax[r]=2;
			linkmax[linkmax[2]]=r;
		}
	}
	for(int i=1;i<=n;i++){
		cout<<linkmax[i]<<" ";
	}
	cout<<"\n";
}	
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 2644 KB Partially correct
2 Partially correct 2 ms 2676 KB Partially correct
3 Partially correct 1 ms 2644 KB Partially correct
4 Partially correct 2 ms 2644 KB Partially correct
5 Partially correct 2 ms 2684 KB Partially correct
6 Partially correct 2 ms 2676 KB Partially correct
7 Partially correct 1 ms 2676 KB Partially correct
8 Partially correct 2 ms 2644 KB Partially correct
9 Partially correct 1 ms 2644 KB Partially correct
10 Partially correct 2 ms 2680 KB Partially correct
11 Partially correct 2 ms 2644 KB Partially correct
12 Partially correct 1 ms 2676 KB Partially correct
13 Partially correct 1 ms 2680 KB Partially correct
14 Partially correct 1 ms 2644 KB Partially correct
15 Partially correct 2 ms 2644 KB Partially correct
16 Partially correct 1 ms 2644 KB Partially correct
17 Partially correct 2 ms 2644 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 2644 KB Partially correct
2 Partially correct 2 ms 2676 KB Partially correct
3 Partially correct 2 ms 2644 KB Partially correct
4 Partially correct 2 ms 2644 KB Partially correct
5 Partially correct 2 ms 2684 KB Partially correct
6 Partially correct 2 ms 2684 KB Partially correct
7 Partially correct 2 ms 2772 KB Partially correct
8 Partially correct 2 ms 2684 KB Partially correct
9 Partially correct 2 ms 2644 KB Partially correct
10 Partially correct 2 ms 2644 KB Partially correct
11 Partially correct 2 ms 2684 KB Partially correct
12 Partially correct 2 ms 2772 KB Partially correct
13 Partially correct 2 ms 2684 KB Partially correct
14 Partially correct 2 ms 2644 KB Partially correct
15 Partially correct 2 ms 2644 KB Partially correct
16 Partially correct 2 ms 2644 KB Partially correct
17 Partially correct 2 ms 2644 KB Partially correct
18 Partially correct 2 ms 2644 KB Partially correct
19 Partially correct 2 ms 2772 KB Partially correct
20 Partially correct 2 ms 2644 KB Partially correct
21 Partially correct 2 ms 2644 KB Partially correct
22 Partially correct 2 ms 2720 KB Partially correct
23 Partially correct 2 ms 2644 KB Partially correct
24 Partially correct 2 ms 2644 KB Partially correct
25 Partially correct 2 ms 2680 KB Partially correct
26 Partially correct 2 ms 2644 KB Partially correct
27 Partially correct 1 ms 2644 KB Partially correct
28 Partially correct 2 ms 2684 KB Partially correct
29 Partially correct 2 ms 2644 KB Partially correct
30 Partially correct 2 ms 2644 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 2644 KB Partially correct
2 Partially correct 2 ms 2676 KB Partially correct
3 Partially correct 1 ms 2644 KB Partially correct
4 Partially correct 2 ms 2644 KB Partially correct
5 Partially correct 2 ms 2684 KB Partially correct
6 Partially correct 2 ms 2676 KB Partially correct
7 Partially correct 1 ms 2676 KB Partially correct
8 Partially correct 2 ms 2644 KB Partially correct
9 Partially correct 1 ms 2644 KB Partially correct
10 Partially correct 2 ms 2680 KB Partially correct
11 Partially correct 2 ms 2644 KB Partially correct
12 Partially correct 1 ms 2676 KB Partially correct
13 Partially correct 1 ms 2680 KB Partially correct
14 Partially correct 1 ms 2644 KB Partially correct
15 Partially correct 2 ms 2644 KB Partially correct
16 Partially correct 1 ms 2644 KB Partially correct
17 Partially correct 2 ms 2644 KB Partially correct
18 Partially correct 1 ms 2644 KB Partially correct
19 Partially correct 2 ms 2676 KB Partially correct
20 Partially correct 2 ms 2644 KB Partially correct
21 Partially correct 2 ms 2644 KB Partially correct
22 Partially correct 2 ms 2684 KB Partially correct
23 Partially correct 2 ms 2684 KB Partially correct
24 Partially correct 2 ms 2772 KB Partially correct
25 Partially correct 2 ms 2684 KB Partially correct
26 Partially correct 2 ms 2644 KB Partially correct
27 Partially correct 2 ms 2644 KB Partially correct
28 Partially correct 2 ms 2684 KB Partially correct
29 Partially correct 2 ms 2772 KB Partially correct
30 Partially correct 2 ms 2684 KB Partially correct
31 Partially correct 2 ms 2644 KB Partially correct
32 Partially correct 2 ms 2644 KB Partially correct
33 Partially correct 2 ms 2644 KB Partially correct
34 Partially correct 2 ms 2644 KB Partially correct
35 Partially correct 2 ms 2644 KB Partially correct
36 Partially correct 2 ms 2772 KB Partially correct
37 Partially correct 2 ms 2644 KB Partially correct
38 Partially correct 2 ms 2644 KB Partially correct
39 Partially correct 2 ms 2720 KB Partially correct
40 Partially correct 2 ms 2644 KB Partially correct
41 Partially correct 2 ms 2644 KB Partially correct
42 Partially correct 2 ms 2680 KB Partially correct
43 Partially correct 2 ms 2644 KB Partially correct
44 Partially correct 1 ms 2644 KB Partially correct
45 Partially correct 2 ms 2684 KB Partially correct
46 Partially correct 2 ms 2644 KB Partially correct
47 Partially correct 2 ms 2644 KB Partially correct
48 Partially correct 39 ms 8572 KB Partially correct
49 Partially correct 45 ms 9140 KB Partially correct
50 Partially correct 46 ms 9160 KB Partially correct
51 Partially correct 34 ms 7776 KB Partially correct
52 Partially correct 61 ms 9184 KB Partially correct
53 Partially correct 43 ms 8640 KB Partially correct
54 Partially correct 29 ms 9420 KB Partially correct
55 Partially correct 59 ms 16640 KB Partially correct
56 Partially correct 48 ms 12524 KB Partially correct
57 Partially correct 50 ms 11340 KB Partially correct
58 Partially correct 51 ms 10272 KB Partially correct
59 Partially correct 50 ms 9264 KB Partially correct
60 Partially correct 58 ms 18940 KB Partially correct
61 Partially correct 53 ms 11292 KB Partially correct
62 Partially correct 43 ms 9544 KB Partially correct
63 Partially correct 45 ms 9308 KB Partially correct
64 Partially correct 42 ms 9428 KB Partially correct
65 Partially correct 53 ms 9604 KB Partially correct
66 Partially correct 40 ms 9368 KB Partially correct
67 Partially correct 35 ms 8968 KB Partially correct
68 Partially correct 44 ms 8456 KB Partially correct
69 Partially correct 40 ms 9584 KB Partially correct
70 Partially correct 42 ms 9180 KB Partially correct
71 Partially correct 30 ms 7500 KB Partially correct
72 Partially correct 36 ms 8116 KB Partially correct
73 Partially correct 41 ms 9672 KB Partially correct
74 Partially correct 38 ms 9000 KB Partially correct
75 Partially correct 43 ms 9048 KB Partially correct
76 Partially correct 56 ms 9080 KB Partially correct
77 Partially correct 47 ms 9352 KB Partially correct
78 Partially correct 30 ms 7116 KB Partially correct
79 Partially correct 32 ms 7852 KB Partially correct
80 Partially correct 42 ms 9000 KB Partially correct
81 Partially correct 46 ms 9296 KB Partially correct
82 Partially correct 41 ms 9332 KB Partially correct