Submission #366745

# Submission time Handle Problem Language Result Execution time Memory
366745 2021-02-15T08:36:56 Z arnold518 Village (BOI20_village) C++14
100 / 100
177 ms 29864 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N;
vector<int> adj[MAXN+10];

ll ans1, ans2;
int memo[MAXN+10];
int P1[MAXN+10], P2[MAXN+10];

int dp1[MAXN+10], dp2[MAXN+10];
int L[MAXN+10], cnt;
void dfs(int now, int bef)
{
	L[now]=++cnt;
	int chd=0;

	bool flag=false;
	pii p={987654321, -1};
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		chd++;
		dfs(nxt, now);
		dp1[now]+=min(dp1[nxt]+1, dp2[nxt]);
		if(dp1[nxt]+1<=dp2[nxt])
		{
			dp2[now]+=dp1[nxt]+1;
			flag=true;
		}
		else
		{
			dp2[now]+=dp2[nxt];
			p=min(p, {dp1[nxt]+1-dp2[nxt], nxt});
		}
	}
	if(!flag)
	{
		dp2[now]+=p.first;
		memo[now]=p.second;
	}
	if(chd==0) dp2[now]=987654321;
}

struct UF
{
	int par[MAXN+10];
	int Find(int x)
	{
		if(par[x]==x) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x); y=Find(y);
		par[x]=y;
	}
}uf;

void dfs2(int now, int bef, int col)
{
	if(col==1)
	{
		for(auto nxt : adj[now])
		{
			if(nxt==bef) continue;
			if(dp1[nxt]+1<=dp2[nxt])
			{
				uf.Union(now, nxt);
				dfs2(nxt, now, 1);
			}
			else
			{
				dfs2(nxt, now, 2);
			}
		}
	}
	else
	{
		for(auto nxt : adj[now])
		{
			if(nxt==bef) continue;
			if(dp1[nxt]+1<=dp2[nxt] || memo[now]==nxt)
			{
				uf.Union(now, nxt);
				dfs2(nxt, now, 1);
			}
			else
			{	
				dfs2(nxt, now, 2);	
			}
		}
	}
}

vector<int> V[MAXN+10];

int sz[MAXN+10];
void getsz(int now, int bef)
{
	sz[now]=1;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		getsz(nxt, now);
		sz[now]+=sz[nxt];
	}
}

int getcen(int now, int bef)
{
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(sz[nxt]>N/2) return getcen(nxt, now);
	}
	return now;
}

void dfs3(int now, int bef, int d, vector<int> &V)
{
	ans2+=d*2;
	V.push_back(now);
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs3(nxt, now, d+1, V);
	}
}

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) uf.par[i]=i;
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	dfs(1, 1);
	dfs2(1, 1, 2);
	ans1=2*dp2[1];

	for(int i=1; i<=N; i++) V[uf.Find(i)].push_back(i);
	for(int i=1; i<=N; i++) sort(V[i].begin(), V[i].end(), [&](const int &p, const int &q)
	{
		return L[p]<L[q];
	});

	for(int i=1; i<=N; i++)
	{
		for(int j=0; j<V[i].size(); j++)
		{
			P1[V[i][j]]=V[i][(j+1)%V[i].size()];
		}
	}

	getsz(1, 1);
	int cen=getcen(1, 1);

	vector<vector<int>> VV, VV2;
	VV.push_back({cen});
	for(auto nxt : adj[cen])
	{
		vector<int> V;
		dfs3(nxt, cen, 1, V);
		VV.push_back(V);
	}
	VV2.resize(VV.size());

	sort(VV.begin(), VV.end(), [&](const vector<int> &p, vector<int> &q)
	{
		return p.size()<q.size();
	});

	int p;
	for(int i=VV.size()-2, j=p=VV.size()-1; i>=0; i--)
	{
		for(auto it : VV[i])
		{
			if(VV2[j].size()==VV[j].size()) j--, p--;
			VV2[j].push_back(it);
		}
	}
	for(auto it : VV[VV.size()-1])
	{
		if(VV2[p].size()==VV[p].size()) p--;
		VV2[p].push_back(it);
	}

	for(int i=0; i<VV.size(); i++)
	{
		for(int j=0; j<VV[i].size(); j++)
		{
			P2[VV[i][j]]=VV2[i][j];
		}
	}
	printf("%lld %lld\n", ans1, ans2);
	for(int i=1; i<=N; i++) printf("%d ", P1[i]); printf("\n");
	for(int i=1; i<=N; i++) printf("%d ", P2[i]); printf("\n");
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:161:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |   for(int j=0; j<V[i].size(); j++)
      |                ~^~~~~~~~~~~~
Village.cpp:200:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |  for(int i=0; i<VV.size(); i++)
      |               ~^~~~~~~~~~
Village.cpp:202:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  202 |   for(int j=0; j<VV[i].size(); j++)
      |                ~^~~~~~~~~~~~~
Village.cpp:208:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  208 |  for(int i=1; i<=N; i++) printf("%d ", P1[i]); printf("\n");
      |  ^~~
Village.cpp:208:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  208 |  for(int i=1; i<=N; i++) printf("%d ", P1[i]); printf("\n");
      |                                                ^~~~~~
Village.cpp:209:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  209 |  for(int i=1; i<=N; i++) printf("%d ", P2[i]); printf("\n");
      |  ^~~
Village.cpp:209:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  209 |  for(int i=1; i<=N; i++) printf("%d ", P2[i]); printf("\n");
      |                                                ^~~~~~
Village.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  139 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
Village.cpp:144:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  144 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 5 ms 5228 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5248 KB Output is correct
7 Correct 4 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 5 ms 5248 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 4 ms 5248 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 5 ms 5228 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 4 ms 5228 KB Output is correct
19 Correct 4 ms 5228 KB Output is correct
20 Correct 4 ms 5228 KB Output is correct
21 Correct 4 ms 5228 KB Output is correct
22 Correct 4 ms 5228 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 4 ms 5228 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 4 ms 5228 KB Output is correct
29 Correct 4 ms 5228 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 5 ms 5228 KB Output is correct
22 Correct 4 ms 5228 KB Output is correct
23 Correct 5 ms 5248 KB Output is correct
24 Correct 4 ms 5228 KB Output is correct
25 Correct 5 ms 5228 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 5 ms 5248 KB Output is correct
28 Correct 6 ms 5356 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 5 ms 5228 KB Output is correct
32 Correct 4 ms 5100 KB Output is correct
33 Correct 4 ms 5228 KB Output is correct
34 Correct 5 ms 5228 KB Output is correct
35 Correct 4 ms 5228 KB Output is correct
36 Correct 4 ms 5228 KB Output is correct
37 Correct 4 ms 5228 KB Output is correct
38 Correct 4 ms 5228 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 4 ms 5100 KB Output is correct
41 Correct 4 ms 5100 KB Output is correct
42 Correct 4 ms 5100 KB Output is correct
43 Correct 4 ms 5228 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 4 ms 5228 KB Output is correct
46 Correct 4 ms 5228 KB Output is correct
47 Correct 4 ms 5228 KB Output is correct
48 Correct 117 ms 15008 KB Output is correct
49 Correct 135 ms 15868 KB Output is correct
50 Correct 136 ms 15888 KB Output is correct
51 Correct 101 ms 13412 KB Output is correct
52 Correct 132 ms 15892 KB Output is correct
53 Correct 116 ms 14784 KB Output is correct
54 Correct 71 ms 17028 KB Output is correct
55 Correct 177 ms 29864 KB Output is correct
56 Correct 159 ms 22564 KB Output is correct
57 Correct 156 ms 20392 KB Output is correct
58 Correct 149 ms 18340 KB Output is correct
59 Correct 136 ms 16164 KB Output is correct
60 Correct 115 ms 25820 KB Output is correct
61 Correct 102 ms 17236 KB Output is correct
62 Correct 110 ms 15908 KB Output is correct
63 Correct 100 ms 15096 KB Output is correct
64 Correct 115 ms 15644 KB Output is correct
65 Correct 109 ms 15660 KB Output is correct
66 Correct 98 ms 15208 KB Output is correct
67 Correct 73 ms 13864 KB Output is correct
68 Correct 90 ms 14060 KB Output is correct
69 Correct 108 ms 15840 KB Output is correct
70 Correct 99 ms 15104 KB Output is correct
71 Correct 70 ms 12420 KB Output is correct
72 Correct 79 ms 13420 KB Output is correct
73 Correct 107 ms 15844 KB Output is correct
74 Correct 99 ms 14880 KB Output is correct
75 Correct 131 ms 15584 KB Output is correct
76 Correct 130 ms 15308 KB Output is correct
77 Correct 116 ms 15460 KB Output is correct
78 Correct 77 ms 12136 KB Output is correct
79 Correct 84 ms 13208 KB Output is correct
80 Correct 134 ms 15712 KB Output is correct
81 Correct 110 ms 15596 KB Output is correct
82 Correct 112 ms 15212 KB Output is correct