#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, i, j, k, a, b, ans[100010], A[100010], res, cent, sz[100010], dep[100010], in[100010], revin[100010], cnt, res2, ans2[100010];
vector<int>adj[100010];
void dfs(int v, int par, int depth)
{
in[v]=++cnt;
dep[v]=depth;
sz[v]=1;
for(int i=0;i<adj[v].size();i++)
{
int p=adj[v][i];
if(p==par)continue;
dfs(p, v, depth+1);
sz[v]+=sz[p];
}
}
int centroid(int v, int par)
{
for(int i=0;i<adj[v].size();i++)
{
int p=adj[v][i];
if(p==par)continue;
if(sz[p]*2>n)
{
return centroid(p, v);
}
}
return v;
}
int f(int x)
{
return ((x+(n/2))%n==0?n:(x+(n/2))%n);
}
void dfs2(int v, int par)
{
vector<int>V;
for(int i=0;i<adj[v].size();i++)
{
int p=adj[v][i];
if(p==par)continue;
dfs2(p, v);
if(!A[p])V.push_back(p);
}
if(v==1&&V.size()==0)
{
ans2[ans2[adj[v][0]]]=1;
ans2[1]=adj[v][0];
res2+=2;
return;
}
if(V.size())
{
if(V.size()==2)
{
ans2[V[0]]=V[1];
ans2[V[1]]=V[0];
res2+=4;
}
else
{
A[v]=1;
ans2[V[0]]=v;
ans2[v]=V[0];
res2+=2;
if(V.size()>1)
{
for(i=1;i<V.size()-1;i++)
{
ans2[V[i]]=V[i+1];
res2+=2;
}
ans2[V.back()]=V[1];
res2+=2;
}
}
}
}
main()
{
for(scanf("%lld", &n);++i<n;)
{
scanf("%lld %lld", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0, 0);
cent=centroid(1, 0);
cnt=0;
dfs(cent, 0, 0);
dfs2(1, 0);
for(i=0;i++<n;)res+=dep[i]*2;
for(i=0;i++<n;)
{
revin[in[i]]=i;
}
for(i=0;i++<n;)
{
ans[revin[i]]=revin[f(i)];
}
printf("%lld %lld\n", res2, res);
for(i=0;i++<n;)printf("%lld ", ans2[i]);
puts("");
for(i=0;i++<n;)
{
printf("%lld ", ans[i]);
}
}
Compilation message
Village.cpp: In function 'void dfs(long long int, long long int, long long int)':
Village.cpp:11:18: 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]
11 | for(int i=0;i<adj[v].size();i++)
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'long long int centroid(long long int, long long int)':
Village.cpp:21:18: 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]
21 | for(int i=0;i<adj[v].size();i++)
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(long long int, long long int)':
Village.cpp:39:18: 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]
39 | for(int i=0;i<adj[v].size();i++)
| ~^~~~~~~~~~~~~~
Village.cpp:69:26: 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]
69 | for(i=1;i<V.size()-1;i++)
| ~^~~~~~~~~~~
Village.cpp: At global scope:
Village.cpp:80:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
80 | main()
| ^
Village.cpp: In function 'int main()':
Village.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
82 | for(scanf("%lld", &n);++i<n;)
| ~~~~~^~~~~~~~~~~~
Village.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
84 | scanf("%lld %lld", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2668 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 7] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
2796 KB |
Partially correct |
2 |
Partially correct |
2 ms |
2796 KB |
Partially correct |
3 |
Partially correct |
3 ms |
2796 KB |
Partially correct |
4 |
Partially correct |
3 ms |
2796 KB |
Partially correct |
5 |
Incorrect |
3 ms |
2796 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 999] |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2668 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 7] |
5 |
Halted |
0 ms |
0 KB |
- |