Submission #940539

# Submission time Handle Problem Language Result Execution time Memory
940539 2024-03-07T10:21:39 Z pcc Village (BOI20_village) C++17
100 / 100
77 ms 23240 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>


const int mxn = 1e5+10;
vector<int> tree[mxn];
int N;

namespace S1{
	ll ans;
	ll dp[mxn];
	int mx[mxn];
	void dfs(int now,int par){
		vector<int> need;
		dp[now] = 1;
		for(auto nxt:tree[now]){
			if(nxt == par)continue;
			dfs(nxt,now);
			if(dp[nxt])need.push_back(nxt);
		}
		if(need.size()>=2){
			dp[now] = 0;
			ans += need.size()*2;
			for(int i = 1;i<need.size();i++){
				mx[need[i-1]] = need[i];
			}
			mx[need.back()] = now;
			mx[now] = need[0];
			need.clear();
		}
		else if(need.size() == 1){
			dp[now] = 0;
			ans += 2;
			int a = need[0];
			mx[now] = a;
			mx[a] = now;
		}
		if(now==par&&dp[now]){
			ans += 2;
			int a = tree[now][0],b = mx[a];
			while(mx[b] != a)b = mx[b];
			mx[now] = a;
			mx[b] = now;
		}
		return;
	}
	void solve(){
		dfs(1,1);
		return;
	}
}

namespace S2{
	int mx[mxn],sz[mxn],dep[mxn];
	int arr[mxn];
	ll ans = 0;
	void szdfs(int now,int par){
		sz[now] = 1;
		for(auto nxt:tree[now]){
			if(nxt == par)continue;
			szdfs(nxt,now);
			sz[now] += sz[nxt];
		}
		return;
	}
	int get_centroid(int now,int par,int tar = sz[1]){
		for(auto nxt:tree[now]){
			if(nxt == par)continue;
			if(sz[nxt]>(tar-1)/2)return get_centroid(nxt,now,tar);
		}
		return now;
	}
	vector<vector<int>> v;
	vector<pii> vp;
	void dfs(int now,int par){
		dep[now] = dep[par]+1;
		ans += dep[now]*2;
		v.back().push_back(now);
		for(auto nxt:tree[now]){
			if(nxt == par)continue;
			dfs(nxt,now);
		}
		return;
	}
	void solve(){
		szdfs(1,1);
		int cen = get_centroid(1,1);
		for(auto nxt:tree[cen]){
			vp.push_back({0,v.size()});
			v.push_back({});
			dfs(nxt,cen);
			vp.back().fs = v.back().size();
		}
		sort(vp.begin(),vp.end());
		v[0].push_back(cen);
		vp[0].sc++;
		int pt = 0;
		while(!v.empty()){
			arr[pt] = v.back().back();
			v.back().pop_back();
			if(v.back().empty())v.pop_back();
			pt+=2;
			if(pt>=N)pt = 1;
		}
		for(int i = 0;i<N;i++)assert(arr[i]);
		for(int i = 1;i<N;i++){
			mx[arr[i]] = arr[i-1];
		}
		mx[arr[0]] = arr[N-1];
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N;
	for(int i = 1;i<N;i++){
		int a,b;
		cin>>a>>b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	S1::solve();
	S2::solve();
	cout<<S1::ans<<' '<<S2::ans<<'\n';
	for(int i = 1;i<=N;i++)cout<<S1::mx[i]<<' ';cout<<'\n';
	for(int i = 1;i<=N;i++)cout<<S2::mx[i]<<' ';cout<<'\n';
	return 0;
}

Compilation message

Village.cpp: In function 'void S1::dfs(int, int)':
Village.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |    for(int i = 1;i<need.size();i++){
      |                  ~^~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:133:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  133 |  for(int i = 1;i<=N;i++)cout<<S1::mx[i]<<' ';cout<<'\n';
      |  ^~~
Village.cpp:133:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  133 |  for(int i = 1;i<=N;i++)cout<<S1::mx[i]<<' ';cout<<'\n';
      |                                              ^~~~
Village.cpp:134:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  134 |  for(int i = 1;i<=N;i++)cout<<S2::mx[i]<<' ';cout<<'\n';
      |  ^~~
Village.cpp:134:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  134 |  for(int i = 1;i<=N;i++)cout<<S2::mx[i]<<' ';cout<<'\n';
      |                                              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5464 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5212 KB Output is correct
6 Correct 1 ms 5208 KB Output is correct
7 Correct 1 ms 5216 KB Output is correct
8 Correct 1 ms 5212 KB Output is correct
9 Correct 2 ms 5216 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 1 ms 5216 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5212 KB Output is correct
15 Correct 2 ms 5464 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 1 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5352 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5464 KB Output is correct
14 Correct 2 ms 5388 KB Output is correct
15 Correct 1 ms 5388 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 2 ms 5376 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5468 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 1 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5464 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5212 KB Output is correct
6 Correct 1 ms 5208 KB Output is correct
7 Correct 1 ms 5216 KB Output is correct
8 Correct 1 ms 5212 KB Output is correct
9 Correct 2 ms 5216 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 1 ms 5216 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5212 KB Output is correct
15 Correct 2 ms 5464 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 1 ms 5212 KB Output is correct
18 Correct 1 ms 5212 KB Output is correct
19 Correct 1 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 2 ms 5352 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5208 KB Output is correct
29 Correct 2 ms 5468 KB Output is correct
30 Correct 2 ms 5464 KB Output is correct
31 Correct 2 ms 5388 KB Output is correct
32 Correct 1 ms 5388 KB Output is correct
33 Correct 1 ms 5212 KB Output is correct
34 Correct 2 ms 5376 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5468 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5212 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 2 ms 5212 KB Output is correct
44 Correct 1 ms 5212 KB Output is correct
45 Correct 2 ms 5212 KB Output is correct
46 Correct 2 ms 5212 KB Output is correct
47 Correct 1 ms 5212 KB Output is correct
48 Correct 44 ms 10632 KB Output is correct
49 Correct 44 ms 11464 KB Output is correct
50 Correct 67 ms 11324 KB Output is correct
51 Correct 34 ms 9876 KB Output is correct
52 Correct 58 ms 11308 KB Output is correct
53 Correct 40 ms 10876 KB Output is correct
54 Correct 36 ms 13896 KB Output is correct
55 Correct 77 ms 23240 KB Output is correct
56 Correct 58 ms 16900 KB Output is correct
57 Correct 52 ms 14920 KB Output is correct
58 Correct 49 ms 13072 KB Output is correct
59 Correct 45 ms 11224 KB Output is correct
60 Correct 44 ms 17340 KB Output is correct
61 Correct 41 ms 13012 KB Output is correct
62 Correct 40 ms 11660 KB Output is correct
63 Correct 35 ms 11324 KB Output is correct
64 Correct 45 ms 11508 KB Output is correct
65 Correct 51 ms 11796 KB Output is correct
66 Correct 34 ms 11552 KB Output is correct
67 Correct 40 ms 10968 KB Output is correct
68 Correct 42 ms 10652 KB Output is correct
69 Correct 35 ms 11760 KB Output is correct
70 Correct 51 ms 11452 KB Output is correct
71 Correct 25 ms 9960 KB Output is correct
72 Correct 28 ms 10584 KB Output is correct
73 Correct 39 ms 11956 KB Output is correct
74 Correct 31 ms 11036 KB Output is correct
75 Correct 42 ms 11068 KB Output is correct
76 Correct 43 ms 11012 KB Output is correct
77 Correct 39 ms 11424 KB Output is correct
78 Correct 26 ms 9368 KB Output is correct
79 Correct 49 ms 10084 KB Output is correct
80 Correct 43 ms 11080 KB Output is correct
81 Correct 39 ms 11484 KB Output is correct
82 Correct 39 ms 11556 KB Output is correct