Codeforces 343D - Water Tree 带子树树剖

div1里看到树剖就像看见亲爹(

#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
#define ll long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
const int inf=0x3f3f3f3f;
const int maxn=5e5+9;  
//int arr[maxn];
 
int n;
 
struct EDGE{
    int v;int next;
}G[maxn<<2];
int head[maxn],tot;

void addedge(int u,int v){
    ++tot;G[tot].v=v;G[tot].next=head[u];head[u]=tot;
}
 
int top[maxn];
int pre[maxn];
int dep[maxn];
int num[maxn];
int p[maxn];    //v的对应位置
int out[maxn];  //退出时间戳
int fp[maxn];   //访问序列
int son[maxn];  //重儿子
int pos;

void init(){
    memset(head,-1,sizeof head);tot=0;
	memset(son,-1,sizeof son);
}

void dfs1(int u,int fa,int d){
    dep[u]=d;
    pre[u]=fa;
    num[u]=1;
    for(int i=head[u];~i;i=G[i].next){
        int v=G[i].v;
        if(v==fa)continue;
        dfs1(v,u,d+1);
        num[u]+=num[v];
        if(son[u]==-1||num[v]>num[son[u]])son[u]=v;
    }
}
void getpos(int u,int sp){
    top[u]=sp;
    p[u]=out[u]=++pos;
    fp[p[u]]=u;
    if(son[u]==-1)return;
    getpos(son[u],sp);
    for(int i=head[u];~i;i=G[i].next){
        int v=G[i].v;
        if(v!=son[u]&&v!=pre[u])getpos(v,v);
    }
    out[u]=pos;
}
 
struct NODE{
	int col;	//-1非纯色
}ST[maxn<<2];
void pushup(int rt){
	if(ST[rt<<1].col==ST[rt<<1|1].col)ST[rt].col=ST[rt<<1].col;
	else ST[rt].col=-1;
}
void pushdown(int rt){
	if(ST[rt].col!=-1){
		ST[rt<<1].col=ST[rt<<1|1].col=ST[rt].col;
	}
}
void update(int a,int b,int c,int l,int r,int rt){
	if(a<=l && b>=r){
		ST[rt].col=c;return;
	}
	pushdown(rt);
	int m=l+r>>1;
	if(a<=m)update(a,b,c,l,m,rt<<1);
	if(b>m)update(a,b,c,m+1,r,rt<<1|1);
	pushup(rt);
}
int query(int a,int l,int r,int rt){
	if(ST[rt].col!=-1)return ST[rt].col;
	pushdown(rt);
	int m=l+r>>1;
	if(a<=m)return query(a,l,m,rt<<1);
	else return query(a,m+1,r,rt<<1|1);
}

void solve(int u,int v){

    while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		//cout<<u<<"~~~"<<top[u]<<endl;
        //cout<<p[top[u]]<<" "<<p[u]<<endl;
        update(p[top[u]],p[u],0,1,n,1);
		u=pre[top[u]];
    }

	if(dep[u]<dep[v])swap(u,v);
    update(p[v],p[u],0,1,n,1);
}
 
int main(){
	scanf("%d",&n);
	init();

	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		addedge(x,y);addedge(y,x);
	}

	dfs1(1,1,0);
	getpos(1,1);

	int q;scanf("%d",&q);
	while(q--){
		int knd,u;
		scanf("%d%d",&knd,&u);
		if(knd==1){
			update(p[u],out[u],1,1,n,1);
		}else if(knd==2){
			solve(u,1);
		}else{
			printf("%d\n",query(p[u],1,n,1));
		}
	}
}

还发现一个恐怖的事情就是我的板子接口变化莫测,有空得整理一波


转载于:https://www.cnblogs.com/Drenight/p/8611256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值