您当前的位置: 首页 > 

minato_yukina

暂无认证

  • 2浏览

    0关注

    138博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

D. Weight the Tree 树形dp好题 Codeforces Round #774 (Div. 2)

minato_yukina 发布时间:2022-03-23 14:17:30 ,浏览量:2

https://codeforces.com/contest/1646/problem/D Codeforces Round #774 (Div. 2) D 定义一个点为good为它的权和等于其他相邻的点的和.计算出一个树最多有多少个好点,并且统计出该条件最少的权和. 思路:观察后发现只有两个点,一条边的情况下才有可能有两个good的点相邻,否则他们总是不相邻的.问题转化为求这个树的最大独立集.用 d p ( u , b ) dp(u,b) dp(u,b)表示u是否在集合中,b=0,代表不是,b=1代表是. 如果u在集合中,那么v一定不在.反之,u不在集合中,v可以再可以不在,两者取max即可. 最后,根据树形dp的思想,用树型结果在把结果还原即可.

#include
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const int INF = 1e9+7;
typedef pair pii;
vector G[maxn];
pii dp[maxn][2];// first = num of good , second = sum of node
pii dfs(int u,int b,int fa){
	pii & res = dp[u][b];
	if(res.first>=0) return res;
	res = {b,b ? (-G[u].size()):-1};
	for(auto v : G[u]){
		if(v==fa) continue;
		pii next = dfs(v,0,u);
		if(b==0) next = max(next,dfs(v,1,u));
		res.first += next.first;
		res.second += next.second;
	}
	return res;
}
vector good;
void build(pii val,int u,int fa){
	if(val==dfs(u,0,fa)){
		good[u] = false;
		for(auto v : G[u]){
			if(v==fa) continue;
			build(max(dfs(v,0,u),dfs(v,1,u)),v,u);
		}
	}
	else {
		good[u] = true;
		for(auto v : G[u]){
			if(v==fa) continue;
			build(dfs(v,0,u),v,u);
		}
	} 
}
int main(){
//    freopen("1.txt","r",stdin);
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;cin>>n;
	for(int i=0;iu>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	if(n==2){
		cout            
关注
打赏
1663570241
查看更多评论
0.0413s