This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
vi adjList[100000];
int parent[100000],size[100000];
LLI dp[100000][2];
int doDFS(int u,int p) {
int i;
LLI m = 1e9;
parent[u] = p,size[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
size[u] += doDFS(v,u);
dp[u][0] += dp[v][1];
dp[u][1] += min(dp[v][0]+1,dp[v][1]);
m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
}
}
dp[u][1] += m;
dp[u][0] = min(dp[u][0],(LLI) 1e9),dp[u][1] = min(dp[u][1],(LLI) 1e9);
return size[u];
}
vi adjList2[100000];
int getAns(int u,int p,int t) {
int i;
if (t == 0) {
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) getAns(v,u,1);
}
}
else {
LLI m = 1e9;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
}
m = max(m,0LL);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
if (min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]) <= m) {
adjList2[u].pb(v);
adjList2[v].pb(u);
getAns(v,u,dp[v][1] < dp[v][0]);
m = 0;
}
else getAns(v,u,dp[v][1] < dp[v][0]+1);
}
}
}
return 0;
}
vi order;
int visited[100000];
int doDFS2(int u,int p) {
int i;
order.pb(u),visited[u] = 1;
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (v != p) doDFS2(v,u);
}
return 0;
}
int doDFS3(int u,int p) {
int i;
order.pb(u);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) doDFS3(v,u);
}
return 0;
}
int ans[100000];
int main() {
int i;
int N,a,b;
scanf("%d",&N);
for (i = 0; i < N-1; i++) {
scanf("%d %d",&a,&b);
a--,b--;
adjList[a].pb(b);
adjList[b].pb(a);
}
int j;
LLI sum = 0;
doDFS(0,-1);
for (i = 1; i < N; i++) sum += 2*min(size[i],N-size[i]);
printf("%lld %lld\n",2*dp[0][1],sum);
getAns(0,-1,1);
for (i = 0; i < N; i++) {
if (!visited[i]) {
doDFS2(i,-1);
for (j = 0; j < order.size(); j++) ans[order[j]] = order[(j+1) % order.size()];
order.clear();
}
}
for (i = 0; i < N; i++) printf("%d ",ans[i]+1);
printf("\n");
for (i = 0; i < N; i++) {
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if ((v == parent[i]) && (N-size[i] > N/2)) break;
else if ((v != parent[i]) && (size[v] > N/2)) break;
}
if (j == adjList[i].size()) break;
}
int c = i;
for (i = 0; i < adjList[c].size(); i++) doDFS3(adjList[c][i],c);
if (!(N & 1)) order.pb(c);
for (i = 0; i < order.size(); i++) ans[order[i]] = order[(i+(order.size()/2)) % order.size()];
if (N & 1) ans[c] = ans[order[0]],ans[order[0]] = c;
for (i = 0; i < N; i++) printf("%d ",ans[i]+1);
printf("\n");
return 0;
}
Compilation message (stderr)
Village.cpp: In function 'int doDFS(int, int)':
Village.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int getAns(int, int, int)':
Village.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int doDFS2(int, int)':
Village.cpp:114:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (i = 0; i < adjList2[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int doDFS3(int, int)':
Village.cpp:123:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:150:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (j = 0; j < order.size(); j++) ans[order[j]] = order[(j+1) % order.size()];
| ~~^~~~~~~~~~~~~~
Village.cpp:158:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | for (j = 0; j < adjList[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:163:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | if (j == adjList[i].size()) break;
| ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:166:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for (i = 0; i < adjList[c].size(); i++) doDFS3(adjList[c][i],c);
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:168:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for (i = 0; i < order.size(); i++) ans[order[i]] = order[(i+(order.size()/2)) % order.size()];
| ~~^~~~~~~~~~~~~~
Village.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
133 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
Village.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
135 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |