zoukankan      html  css  js  c++  java
  • Split The Tree(dfs序+树状数组)

    Split The Tree

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 46  解决: 11
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    You are given a tree with n vertices, numbered from 1 to n. ith vertex has a value wi
    We define the weight of a tree as the number of different vertex value in the tree.
    If we delete one edge in the tree, the tree will split into two trees. The score is the sum of these two trees’ weights.
    We want the know the maximal score we can get if we delete the edge optimally.

    输入

    Input is given from Standard Input in the following format:
    n
    p2 p3  . . . pn
    w1 w2  . . . wn
    Constraints
    2 ≤ n ≤ 100000 ,1 ≤ pi < i
    1 ≤ wi ≤ 100000(1 ≤ i ≤ n), and they are integers
    pi means there is a edge between pi and i

    输出

    Print one number denotes the maximal score.

    样例输入

    3
    1 1
    1 2 2
    

    样例输出

    3
    

     思路:

    把序列延长一倍,每段子树的两边的区间连起来,将问题转化成维护区间中不同数字个数的最大值。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+10;
    vector<pair<int,int> >vi[maxn];
    int tree[maxn];
    inline int lowbit(int x){return x&-x;}
    void update(int x,int val) {for (int i=x; i<maxn; i+=lowbit(i)) tree[i]+=val;}
    int Query(int x){int res=0;for (int i=x; i; i-=lowbit(i)) res+=tree[i]; return res;}
    int st[maxn],dfn[maxn],ed[maxn],tot=0;
    struct Edge{int u,v,next;}e[maxn];
    int head[maxn],cnt=0;
    inline void add(int u,int v){e[cnt]=Edge{u,v,head[u]};head[u]=cnt++;}
    int n,vis[maxn],w[maxn];
    void dfs(int x,int fa=0){
        st[x]=++tot;
        dfn[tot]=x;
        dfn[n+tot]=x;
        for (int v,i=head[x]; ~i; i=e[i].next){
            v=e[i].v;
            if(v==fa) continue;
            dfs(v,x);
        }
        ed[x]=tot;
    }
    int ans[maxn],res=0;
    int main(){
        memset(head,-1,sizeof(head));
        scanf("%d",&n);
        for (int p,i=2; i<=n; i++) scanf("%d",&p),add(p,i),add(i,p);
        for (int i=1; i<=n; i++) scanf("%d",&w[i]);
        dfs(1);
        vi[ed[1]].push_back({st[1],1});
        for (int i=2; i<=n; i++){
            vi[ed[i]].push_back({st[i],i});
            vi[st[i]-1+n].push_back({ed[i]+1,i});
        }
        for (int i=1; i<=n+n; i++){
            if(vis[w[dfn[i]]]) update(vis[w[dfn[i]]],-1);
            update(i,1);
            vis[w[dfn[i]]]=i;
            for (auto u:vi[i])
                ans[u.second]+=Query(i)-Query(u.first-1);
        }
        for (int i=1; i<=n; i++) res=max(res,ans[i]);
        printf("%d
    ",res);
        return 0;
    }
    View Code
  • 相关阅读:
    [USACO17FEB]Why Did the Cow Cross the Road III S
    [POI2015]PIE
    GSS3
    Bzoj3203: [Sdoi2013]保护出题人 凸包 + 三分
    斜率优化
    fread优化读入
    [USACO09JAN]安全出行Safe Travel 最短路,并查集
    zookeeper单机模式实现分布式,开发部署测试模式机器有限情况
    解决 jmeter An error occurred: Error while processing samples:Mismatch
    python 垃圾回收解析
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9593896.html
Copyright © 2011-2022 走看看