CodeForces - 1521D Nastia Plays with a Tree(树上最小路径覆盖)

Posted Frozen_Guardian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1521D Nastia Plays with a Tree(树上最小路径覆盖)相关的知识,希望对你有一定的参考价值。

题目链接:点击查看

题目大意:给出一棵树,可以删除 x x x 条边并增加 x x x 条边使得树变为竹子,竹子就是一条链,问 x x x 最小可以为多少,输出一种方案数

题目分析:树上最小路径覆盖,按照子节点个数分两种情况讨论即可:

  1. s o n [ u ] = = 2 son[u]==2 son[u]==2:删除掉 u u u f a [ u ] fa[u] fa[u] 这条边即可
  2. s o n [ u ] > 2 son[u]>2 son[u]>2:删除掉 u u u f a [ u ] fa[u] fa[u] ,并且删除掉 s o n [ u ] − 2 son[u]-2 son[u]2 条连向子节点的边

然后剩下的图一定是一个 “竹子森林”,将叶子节点两两相连即可

代码:

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
	T f=1;x=0;
	char ch=getchar();
	while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	x*=f;
}
template<typename T>
inline void write(T x)
{
	if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
vector<pair<int,int>>node[N];
bool del_fa[N],vis[N],ban[N];
vector<pair<int,int>>del,add;
vector<pair<int,int>>leaf;
vector<int>temp_leaf;
int answer=0;
void dfs1(int u,int fa) {
	int son=0;
	for(auto it:node[u]) {
		int v=it.first;
		int id=it.second;
		if(v==fa) {
			continue;
		}
		dfs1(v,u);
		if(del_fa[v]) {
			ban[id]=true;
		} else {
			son++;
		}
	}
	if(son>=2) {
		del_fa[u]=true;
		for(auto it:node[u]) {
			int v=it.first;
			int id=it.second;
			if(v==fa) {
				continue;
			}
			if(son<=2) {
				break;
			}
			if(!del_fa[v]) {
				son--;
				ban[id]=true;
			}
		}
	}
}
void dfs2(int u,int fa) {
	int son=0;
	vis[u]=true;
	for(auto it:node[u]) {
		int v=it.first;
		int id=it.second;
		if(v==fa) {
			continue;
		}
		if(ban[id]) {
			continue;
		}
		dfs2(v,u);
		son++;
	}
	if(fa==-1&&son==1) {
		temp_leaf.push_back(u);
	} else if(son==0) {
		temp_leaf.push_back(u);
	}
}
void init(int n) {
	memset(vis,false,(n+5));
	memset(ban,false,(n+5));
	memset(del_fa,false,(n+5));
	leaf.clear();
	add.clear();
	del.clear();
	for(int i=1;i<=n;i++) {
		node[i].clear();
	}
}
int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--) {
		int n;
		read(n);
		init(n);
		for(int i=1;i<n;i++) {
			int u,v;
			read(u),read(v);
			node[u].push_back({v,i});
			node[v].push_back({u,i});
		}
		dfs1(1,-1);
		for(int i=1;i<=n;i++) {
			for(auto it:node[i]) {
				if(ban[it.second]&&it.first<i) {
					del.push_back({it.first,i});
				}
			}
		}
		for(int i=1;i<=n;i++) {
			if(!vis[i]) {
				temp_leaf.clear();
				dfs2(i,-1);
				if(temp_leaf.size()==2) {
					leaf.push_back({temp_leaf[0],temp_leaf[1]});
				} else {
					leaf.push_back({temp_leaf[0],temp_leaf[0]});
				}
			}
		}
		for(int i=1;i<(int)leaf.size();i++) {
			add.push_back({leaf[i-1].second,leaf[i].first});
		}
		cout<<del.size()<<endl;
		for(int i=0;i<(int)del.size();i++) {
			cout<<del[i].first<<' '<<del[i].second<<' '<<add[i].first<<' '<<add[i].second<<endl;
		}
	}
    return 0;
}

以上是关于CodeForces - 1521D Nastia Plays with a Tree(树上最小路径覆盖)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 1521A Nastia and Nearly Good Numbers

CodeForces - 1521B Nastia and a Good Array

Codeforces Round #720 (Div. 2), B. Nastia and a Good Array

Codeforces Round #720 (Div. 2) A. Nastia and Nearly Good Num

C. Nastia and a Hidden Permutation(Codeforces Round #720 (Div. 2)题解)

Codeforces Round #720 (Div. 2) 1521 C. Nastia and a Hidden Permutation(交互,技巧)