codeforces dfs序+线段树+bitset

54 篇文章 0 订阅
8 篇文章 0 订阅

题目链接

http://codeforces.com/problemset/problem/620/E

题意

给定一个树,现在有60种颜色,要做如下操作
1 v c:将v及其子树然成c颜色
2 v:查询v及其子树中含有多少不同颜色

思路

用dfs序存储子树,bitset代表子树中含有的颜色。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
#include<stack>
#include<set>
#include<map>
#define ll long long
using namespace std;
const int INF = ( 2e9 ) + 2;
const ll maxn = 4e5+100;
vector<int> g[maxn];
int col[maxn];
int p1[maxn],p2[maxn],con[maxn];
int index;
bitset<61> z,o;
struct node
{
    int l,r,mark;
    bitset<61> p;
}t[maxn*4];
void build(int root,int l,int r)
{
    t[root].l=l;
    t[root].r=r;
    t[root].mark=0;
    if(l==r)
    {
        t[root].p[ col[con[l]] ]=1; // dfs序为l的结点的颜色 
        return;
    }
    int mid=(l+r)>>1;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    t[root].p=t[root*2].p | t[root*2+1].p;
}
void pushdown(int root)
{
    if(t[root].mark==1)
    {
        t[root*2].p.reset();
        t[root*2].p = t[root*2+1].p =t[root].p;
        t[root*2].mark= t[root*2+1].mark = 1;
        t[root*2+1].p.reset();  
        t[root].mark=0;
    }
}
void update(int root,int ul,int ur,int c)
{
    int l=t[root].l,r=t[root].r;
    if(ul>r||ur<l)return;
    if(ul<=l&&ur>=r)
    {
        t[root].p.reset();
        t[root].p[c]=1;
        t[root].mark=1;
        return;
    }
    int mid = (l+r)>>1;
    pushdown(root);
    if(ur<=mid) 
    update(root*2,ul,ur,c);
    else if(ul>mid)
    update(root*2+1,ul,ur,c);
    else
    {
        update(root*2,ul,ur,c);
        update(root*2+1,ul,ur,c);
    }
    t[root].p = t[root*2].p | t[root*2+1].p;
}
bitset<61> query(int root,int ql,int qr)
{
    int l=t[root].l,r=t[root].r;
    if(ql>r||qr<l)return o;
    if(ql<=l&&qr>=r)
    {
        return t[root].p;
    }
    int mid = (l+r)>>1;
    pushdown(root);
    if(qr<=mid)
    return query(root*2,ql,qr);
    else if(ql>mid)
    return query(root*2+1,ql,qr);
    else
    {
        return query(root*2,ql,qr) | query(root*2+1,ql,qr);
    }
}
void dfs(int u,int fa)
{
    p1[u]=++index;
    con[index]=u;
    for(int i=0,L=g[u].size();i<L;i++)
    {
        int v=g[u][i];
        if(v==fa)continue;
        dfs(v,u);
    }
    p2[u]=index;
}
int main()
{
    int n,m,u,v;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&col[i]);
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1,-1);
    build(1,1,n);
    for(int i=1;i<=60;i++)o[i]=1;
    int op;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d%d",&v,&u);
            update(1,p1[v],p2[v],u);
        }
        else
        {
            scanf("%d",&v);
            int cnt=0;
            bitset<61> get=query(1,p1[v],p2[v]);
            printf("%d\n",get.count());
        }
    }
}
  • 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、付费专栏及课程。

余额充值