• 牛客练习赛47 E DongDong数颜色 (树上启发式合并)


    链接:https://ac.nowcoder.com/acm/contest/904/E
    来源:牛客网

    DongDong数颜色
    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 524288K,其他语言1048576K
    64bit IO Format: %lld
    题目描述
    DongDong是个喜欢数颜色的女孩子,她已经熟练地掌握了在序列上数颜色的操作,现在她开始学习如何在树上数颜色,现在给定一个n个点,n-1条边的树形图(视1号店为根),每个点有一个颜色,每次询问以x为根的子树中有多少种不同的颜色,DongDong轻松地解决了这个问题,但她想考考会编程的你。

    输入描述:
    第一行两个整数n,m

    第二行n个整数,表示每个点的颜色

    接下来n-1行每行u,v,表示存在一条从u到v的双向边(保证最终图形是树形图)

    2<=n<=100000,1<=m,color<=n,
    输出描述:
    共m行:每行输出相应询问的答案
    示例1
    输入
    复制
    4 3
    1 1 2 3
    1 2
    2 3
    1 4
    1
    2
    4
    输出
    复制
    3
    2
    1

    思路:

    询问的都是子树的问题,很经典的dsu on tree,

    和这题比较像:https://www.cnblogs.com/qieqiemin/p/11309973.html

    把add 里维护答案的部分改一下即可。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
    inline void getInt(int* p);
    const int maxn = 100010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    
    int n;
    std::vector<int> son[maxn];
    int wson[maxn];
    int SZ[maxn];
    int a[maxn];
    void dfs1(int x,int pre)
    {
        SZ[x]=1;
        int maxson=-1;
        for(auto y:son[x])
        {
            if(y!=pre)
            {
                dfs1(y,x);
                SZ[x]+=SZ[y];
                if(maxson<SZ[y])
                {
                    maxson=SZ[y];
                    wson[x]=y;
                }
            }
        }
    }
    ll ans[maxn];
    ll sum;
    int isson;
    int m;
    ll cnt[maxn];
    void add(int x,int pre,int val)
    {
        if(cnt[a[x]])
        {
            if(val==-1)
            {
                cnt[a[x]]=0;
                sum--;
            }
        }
        if(cnt[a[x]]==0)
        {
            if(val==1)
            {
                cnt[a[x]]=1;
                sum++;
            }
        }
        for(auto y:son[x])
        {
            if(y==pre||y==isson)
                continue;
            add(y,x,val);
        }
    }
    void dfs2(int x,int pre,int op)
    {
        for(auto y:son[x])
        {
            if(y==pre||y==wson[x])
            {
                continue;
            }
            dfs2(y,x,0);
        }
        if(wson[x])
        {
            dfs2(wson[x],x,1);
            isson=wson[x];
        }
        add(x,pre,1);
        isson=0;
        ans[x]=sum;
        if(op==0)
        {
            add(x,pre,-1);
            sum=0;
            m=0;
        }
    }
    int main()
    {
        //freopen("D:\code\text\input.txt","r",stdin);
        //freopen("D:\code\text\output.txt","w",stdout);
        gg(n);
        int q;
        gg(q);
        repd(i,1,n)
        {
            gg(a[i]);
        }
        int u,v;
        repd(i,2,n)
        {
            gg(u);gg(v);
            son[u].pb(v);
            son[v].pb(u);
        }
        dfs1(1,0);
        dfs2(1,0,0);
        int x;
        repd(i,1,q)
        {
            gg(x);
            printf("%lld
    ",ans[x] );
        }
    //    printf("
    ");
    
    
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    vue2.0 移动端,下拉刷新,上拉加载更多 封装组件
    Mac 安装RN android开发环境
    JavaScript 复杂判断的更优雅写法
    JSBridge的原理及使用
    FlatList列表组件的使用
    React Native ScrollView中TextInput焦点问题
    pod update报错(Cocoapods: Failed to connect to GitHub to update the CocoaPods/Specs specs repo)报错解决方案
    如何在pc端通过adb连接手机调试,不用usb数据线
    react学习之搭建后台管理系统
    node+koa2+axios踩坑记
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11310304.html
Copyright © 2020-2023  润新知