题目
思路
让人想起 S o r t I t O u t \mathrm{Sort\; It\; Out} SortItOut 的方法:如果有一些边留下来,会怎样?——显然会留下很多条链。而两个链之间有且仅有一条边,就把这条边改一改即可。
所以问题转化为链剖分,使得链的总长度最长。这就是大家都会的。只是输出方案比较麻烦。我就直接把每个点对应的那条链存了下来,然后直接拼接。复杂度 O ( n ) \mathcal O(n) O(n) 。只是我实现的时候用了 s o r t \mathrm{sort} sort 图方便……
官方解法
有一种贪心的构造法。如果一个点,有至少两个儿子节点,何必让它与父节点连边?——它的父节点就可以去连接别的儿子,而对于这一步来说,丢失的边数量都是 d e g − 2 deg-2 deg−2 嘛。
于是做法就很简单了:如果只有一个子节点,接上去;否则断掉它与父节点的连边,并且任选两个子节点接上去。
代码
#include <bits/stdc++.h>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 100005;
vector<int> G[MaxN];
int dp[MaxN][2]; // 1:have a endpoint 0:whatever
int pre[MaxN][2], endpos[MaxN][2];
int id[MaxN];
bool cmp(const int &a,const int &b){
return dp[a][1]-dp[a][0] > dp[b][1]-dp[b][0];
}
void dfs(int x,int fa){
for(int y : G[x])
if(y != fa)
dfs(y,x);
int tot = dp[x][1] = 0;
for(int y : G[x])
if(y != fa){
dp[x][1] += dp[y][0];
id[++ tot] = y;
}
if(tot == 0){ // leaf
endpos[x][0] = x; // itself
endpos[x][1] = x; // single
dp[x][0] = dp[x][1] = 0;
pre[x][0] = pre[x][1] = 0;
return ; // nothing to do
}
sort(id+1,id+tot+1,cmp);
id[tot+1] = 0; // if tot == 1
memcpy(pre[x],id+1,2<<2);
endpos[x][0] = endpos[id[1]][0];
endpos[x][1] = endpos[id[2]][0];
dp[x][1] += dp[id[1]][1]+1-dp[id[1]][0];
dp[x][0] = dp[x][1]+dp[id[2]][1]+1-dp[id[2]][0];
if(tot == 1) dp[x][0] = -1; // invalid
if(dp[x][0] < dp[x][1]){
dp[x][0] = dp[x][1];
endpos[x][1] = x; // my self
}
}
/** L for father, R for son */
void output(int x,int d,int fa,int &L,int &R){
if(d == 1) pre[x][1] = 0; // useless
for(int y : G[x])
if(y != fa && y != pre[x][0] && y != pre[x][1]){
output(y,0,x,endpos[y][0],endpos[y][1]);
printf("%d %d %d %d\n",x,y,R,endpos[y][0]);
R = endpos[y][1]; // joined
}
if(pre[x][0]) output(pre[x][0],1,x,L,R);
if(pre[x][1]) output(pre[x][1],1,x,L,R);
}
int main(){
for(int T=readint(); T; --T){
int n = readint();
rep(i,1,n) G[i].clear();
rep(i,1,n-1){
int a = readint();
int b = readint();
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1,0);
printf("%d\n",n-1-dp[1][0]);
output(1,0,0,endpos[1][0],endpos[1][1]);
}
return 0;
}
官方题解
#include <bits/stdc++.h>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 100005;
vector<int> G[MaxN];
int dp[MaxN]; // number of cut
int pre[MaxN][3];
void dfs(int x,int fa){
dp[x] = 0; // clear
rep(j,0,1) pre[x][j] = 0;
for(int y : G[x]) if(y != fa){
dfs(y,x); dp[x] += dp[y];
if(pre[y][1]) continue;
rep(j,0,2) if(!pre[x][j] || j == 2){
dp[x] += (j&2)>>1;
pre[x][j] = y; break;
}
}
if(fa && pre[x][1]) ++ dp[x];
}
/** 1 for father, 0 for son */
int ep[MaxN][2]; // endpoint
void output(int x,int fa){
ep[x][0] = ep[x][1] = x;
rep(j,0,1) if(pre[x][j]){
output(pre[x][j],x);
ep[x][j] = ep[pre[x][j]][0];
}
for(int y : G[x]) if(y != fa)
if(y != pre[x][0] && y != pre[x][1]){
output(y,x); printf("%d %d ",x,y);
printf("%d %d\n",ep[x][0],ep[y][1]);
ep[x][0] = ep[y][0]; // joined
}
}
int main(){
for(int T=readint(); T; --T){
int n = readint();
rep(i,1,n) G[i].clear();
rep(i,1,n-1){
int a = readint();
int b = readint();
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1,0);
printf("%d\n",dp[1]);
output(1,0);
}
return 0;
}