题意
给定一个森林,要求将森林连成树,使得合成的树中端点互不相同的边的数量最大,输出连边方案
题解
- 自叶子向上贪心可以求出一棵树的最优情况,同时保证了根部不连与根部连的最优情况即为上述贪心所求的情况。
- 通过1便可将问题转化为已知多个可匹配或不可匹配的点,将他们连成树,每次连边都会给大树带来k个可匹配的点。(id为1的树固定)
- 上述贪心容易想到首先肯定是将不可匹配点先连到大树(为了使得大树中可匹配的点越多)之后贪心地选取带有可匹配点多的树并入大树
- 合并的时候记录方案即可
代码
/**
* author: TelmaZzzz
* create: 2019-09-04-19.15.15
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=1e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//ÄæÔª
int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
int ans;
int cnt;
vector<int> que;
int pos[N];
bool mark[N];
vector<int>black[N],white[N];
struct node{
int col;
int num;
int id;
}res[N];
queue<int>q;
void dfs(int x,int pre){
for(int i=head[x];i;i=NEXT[i]){
int y=ver[i];
if(y==pre) continue;
dfs(y,x);
}
if(!mark[pre]&&!mark[x]){
mark[pre]=true;
mark[x]=true;
ans++;
}
if(mark[x]){
black[cnt].PB(x);
}
else {
if(pre!=0)
white[cnt].PB(x);
}
}
bool cmp(node a,node b){
if(a.col==b.col){
return a.num>b.num;
}
return a.col>b.col;
}
int main(){
freopen("hidden.in","r",stdin);
freopen("hidden.out","w",stdout);
//ios::sync_with_stdio(false);
mark[0]=true;
int n;
R(n);
cnt=0;
ans=0;
tot=0;
que.PB(1);
int inp;
rep(i,2,n){
R(inp);
if(inp==0) {que.PB(i);continue;}
link(inp,i);
link(i,inp);
pos[i]=inp;
}
rep(i,0,(int)que.size()-1){
cnt++;
dfs(que[i],0);
if(mark[que[i]]){
res[i].num=white[cnt].size();
res[i].id=i;
res[i].col=1;
}
else {
res[i].num=white[cnt].size();
res[i].id=i;
res[i].col=1;
}
}
sort(res+1,res+que.size(),cmp);
// rep(i,0,(int)que.size()-1) cout<<res[i].id<<' ';
// cout<<endl;
rep(i,0,(int)white[1].size()-1){
q.push(white[1][i]);
}
if(!mark[1]) q.push(1);
rep(i,1,(int)que.size()-1){
int id=res[i].id;
if(mark[que[id]]){
pos[que[id]]=1;
}
else {
if(q.size()){
int x=q.front();
q.pop();
pos[que[id]]=x;
ans++;
}
else {
q.push(que[id]);
pos[que[id]]=1;
}
}
rep(j,0,(int)white[id+1].size()-1){
q.push(white[id+1][j]);
}
}
W(ans);
rep(i,2,n) {
//if(pos[i]==0) {printf("1 ");continue;}
printf("%d ",pos[i]);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
//1 9 10 11 26 27 30 38 42 50 62 63 66 67 85 90 91 97