답안 #802301

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
802301 2023-08-02T11:30:33 Z parsadox2 Village (BOI20_village) C++14
19 / 100
4 ms 5204 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];
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;
		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;
	fsum += (dp[1][child[v][0]] - dp[0][child[v][0]]);
	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;
		}
	}
	//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;
}

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]] = child[v][i + 1];
	if(ty == 1)
	{
		if(par[ty][v] != 0)
			ar[child[v][par[ty][v] - 1]] = child[v][0];		
	}
	else
	{
		ar[v] = 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:53: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]
   53 |  for(int i = 1 ; i < child[v].size() ; i++)
      |                  ~~^~~~~~~~~~~~~~~~~
Village.cpp:68: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]
   68 |  for(int i = 1 ; i < child[v].size() ; i++)
      |                  ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'void Build(long long int, long long int)':
Village.cpp:89: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]
   89 |  for(int i = par[ty][v] ; i < child[v].size() ; i++)
      |                           ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5024 KB Output is correct
2 Incorrect 2 ms 4948 KB Integer parameter [name=vi] equals to 0, violates the range [1, 7]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5032 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5032 KB Output is correct
4 Partially correct 3 ms 5204 KB Partially correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 4 ms 5160 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Partially correct 3 ms 5164 KB Partially correct
12 Correct 3 ms 5156 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5160 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5184 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 4 ms 5164 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5160 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 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 3 ms 5024 KB Output is correct
2 Incorrect 2 ms 4948 KB Integer parameter [name=vi] equals to 0, violates the range [1, 7]
3 Halted 0 ms 0 KB -