답안 #802397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
802397 2023-08-02T12:07:17 Z parsadox2 Village (BOI20_village) C++14
100 / 100
122 ms 30120 KB
#include <bits/stdc++.h>
#define int long long 
using namespace std;

const int maxn = 1e5 + 10;
int n , st[maxn] , fn[maxn] , dp[2][maxn] , par[2][maxn] , sz[maxn] , ar[maxn] , br[maxn] , pos[maxn];
vector <int> trav , adj[maxn] , child[maxn];

void dfs(int v , int p = -1)
{
	st[v] = trav.size();
	sz[v] = 1;
	trav.push_back(v);
	for(auto u : adj[v])  if(u != p)
	{
		dfs(u , v);
		sz[v] += sz[u];
	}
	fn[v] = trav.size();
}

bool cmp(int a , int b)
{
	return dp[1][a] - dp[0][a] < dp[1][b] - dp[0][b];
}

void solve(int v , int p = -1)
{
	bool leaf = true;
	int sum = 0;
	for(auto u : adj[v])  if(u != p)
	{
		leaf = false;
		solve(u , v);
		child[v].push_back(u);
		sum += dp[0][u];
	}
	if(leaf)
	{
		dp[1][v] = 0;
		dp[0][v] = maxn << 2;
		pos[v] = v;
		return;
	}
	sort(child[v].begin() , child[v].end() , cmp);
	//cout << v << " " << p << " : " << endl;
	//for(auto u : child[v])
	//	cout << u << " " << dp[0][u] << " " << dp[1][u] << endl;
	//cout << endl;
	int fsum = sum;
	dp[1][v] = sum;
	par[1][v] = 0;
	pos[v] = v;
	fsum += (dp[1][child[v][0]] - dp[0][child[v][0]]);
	//cout << fsum << endl;
	if(fsum + 2 < dp[1][v])
	{
		par[1][v] = 1;
		dp[1][v] = fsum + 2;
		pos[v] = pos[child[v][0]];
	}
	//cout << dp[1][v] << "!" << endl;
	for(int i = 1 ; i < child[v].size() ; i++)
	{
		int now = child[v][i];
		fsum += (dp[1][now] - dp[0][now]);
		if(fsum + 2 * (i + 1) < dp[1][v])
		{
			dp[1][v] = fsum + 2 * (i + 1);
			par[1][v] = i + 1;
			pos[v] = v;
		}
	}
	//cout << "SALAM" << endl;
	//cout << v << " " << p << endl;
	fsum = sum + dp[1][child[v][0]] - dp[0][child[v][0]];
	dp[0][v] = fsum + 2;
	par[0][v] = 1;
	for(int i = 1 ; i < child[v].size() ; i++)
	{
		//cout << i << endl;
		int now = child[v][i];
		fsum += (dp[1][now] - dp[0][now]);
		if(fsum + 2 * (i + 1) < dp[0][v])
		{
			dp[0][v] = fsum + 2 * (i + 1);
			par[0][v] = i + 1;
		}
	}
	//cout << v << " " << dp[0][v] << " " << dp[1][v] << endl;
	//if(v == 5)
	//	dp[1][v] = 2;
}

void Build(int v , int ty)
{
	//cout << v << " " << ty << " " << child[v].size() << endl;
	if(child[v].size() == 0)
		return;
	for(int i = 0 ; i < par[ty][v] ; i++)
		Build(child[v][i] , 1);
	for(int i = par[ty][v] ; i < child[v].size() ; i++)
		Build(child[v][i] , 0);
	//cout << v << " " << ty << endl;
	for(int i = 0 ; i + 1 < par[ty][v] ; i++)
		ar[child[v][i]] = pos[child[v][i + 1]];
	if(ty == 1)
	{
		if(par[ty][v] == 1)
		{
			ar[child[v][0]] = v;
		}
		else if(par[ty][v] != 0)
			ar[child[v][par[ty][v] - 1]] = pos[child[v][0]];		
	}
	else
	{
		ar[v] = pos[child[v][0]];
		ar[child[v][par[ty][v] - 1]] = v;
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 0 ; i < n - 1 ; i++)
	{
		int v , u;  cin >> v >> u;
		adj[v].push_back(u);
		adj[u].push_back(v);
	}
	dfs(1);
	//cout << "DFSED" << endl;
	solve(1);
	//cout << "SOLVED" << endl;
	Build(1 , 0);
	//cout << "BUILt" << endl;
	int lar = 0 , sma = dp[0][1];
	for(int i = 2 ; i <= n ; i++)
		lar += min(sz[i] , n - sz[i]) * 2;
	for(int i = 1 ; i <= n ; i++)
		br[i] = trav[(st[i] + (n / 2)) % n];
	cout << sma << " " << lar << endl;
	for(int i = 1 ; i <= n ; i++)
		cout << ar[i] << " ";
	cout << endl;
	for(int i = 1 ; i <= n ; i++)
		cout << br[i] << " ";
	cout << endl;
	return 0;
}

Compilation message

Village.cpp: In function 'void solve(long long int, long long int)':
Village.cpp:63:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  for(int i = 1 ; i < child[v].size() ; i++)
      |                  ~~^~~~~~~~~~~~~~~~~
Village.cpp:79:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for(int i = 1 ; i < child[v].size() ; i++)
      |                  ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'void Build(long long int, long long int)':
Village.cpp:102:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |  for(int i = par[ty][v] ; i < child[v].size() ; i++)
      |                           ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 2 ms 5204 KB Output is correct
7 Correct 2 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 2 ms 5204 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 2 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 2 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 2 ms 5076 KB Output is correct
19 Correct 2 ms 5076 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 2 ms 5204 KB Output is correct
24 Correct 2 ms 5204 KB Output is correct
25 Correct 3 ms 5204 KB Output is correct
26 Correct 3 ms 5204 KB Output is correct
27 Correct 3 ms 5204 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 2 ms 5204 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 3 ms 5204 KB Output is correct
35 Correct 3 ms 5204 KB Output is correct
36 Correct 3 ms 5204 KB Output is correct
37 Correct 2 ms 5204 KB Output is correct
38 Correct 3 ms 5204 KB Output is correct
39 Correct 3 ms 5204 KB Output is correct
40 Correct 3 ms 5204 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 2 ms 5076 KB Output is correct
45 Correct 4 ms 5204 KB Output is correct
46 Correct 3 ms 5204 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 59 ms 18944 KB Output is correct
49 Correct 64 ms 20280 KB Output is correct
50 Correct 96 ms 20312 KB Output is correct
51 Correct 45 ms 16908 KB Output is correct
52 Correct 63 ms 20200 KB Output is correct
53 Correct 56 ms 18712 KB Output is correct
54 Correct 43 ms 17232 KB Output is correct
55 Correct 75 ms 30120 KB Output is correct
56 Correct 97 ms 24816 KB Output is correct
57 Correct 122 ms 23240 KB Output is correct
58 Correct 71 ms 22008 KB Output is correct
59 Correct 73 ms 20400 KB Output is correct
60 Correct 49 ms 17996 KB Output is correct
61 Correct 62 ms 18348 KB Output is correct
62 Correct 68 ms 18752 KB Output is correct
63 Correct 43 ms 17984 KB Output is correct
64 Correct 53 ms 20200 KB Output is correct
65 Correct 56 ms 20192 KB Output is correct
66 Correct 44 ms 18296 KB Output is correct
67 Correct 33 ms 15048 KB Output is correct
68 Correct 52 ms 17872 KB Output is correct
69 Correct 71 ms 19968 KB Output is correct
70 Correct 45 ms 19000 KB Output is correct
71 Correct 44 ms 15424 KB Output is correct
72 Correct 51 ms 16756 KB Output is correct
73 Correct 49 ms 20040 KB Output is correct
74 Correct 56 ms 18684 KB Output is correct
75 Correct 83 ms 20280 KB Output is correct
76 Correct 79 ms 19912 KB Output is correct
77 Correct 77 ms 19964 KB Output is correct
78 Correct 35 ms 15160 KB Output is correct
79 Correct 52 ms 16528 KB Output is correct
80 Correct 63 ms 20200 KB Output is correct
81 Correct 51 ms 20076 KB Output is correct
82 Correct 45 ms 19464 KB Output is correct