Split The Tree

题目描述

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

 

来源/分类

2018东北四省赛 

题意:每颗树的重量定义为这颗树上所有节点权值不同的个数,现在要割掉一条边,求生成的两颗树最大的重量和。

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

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define lowbit(x)  x&(-x)
typedef pair<int,int>pa;
const int INF=2e9+7;
const int maxn=2e5+50;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
vector<pa> vi[maxn];
struct node
{
    int u,v,nextt;
} edge[maxn];
int head[maxn],tot;
int st[maxn],ed[maxn],dfn[maxn],cnt;
int n,vis[maxn],weight[maxn],tree[maxn],ans[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addedge(int u,int v)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].nextt=head[u];
    head[u]=tot++;
}
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;
}
void dfs(int x,int fa=0)
{
    st[x]=++cnt;
    dfn[cnt]=x;
    dfn[n+cnt]=x;
    for(int i=head[x];i!= -1; i=edge[i].nextt)
    {
        int v=edge[i].v;
        if(v!=fa)
        {
            dfs(v,x);
        }
    }
    ed[x]=cnt;
}
int main()
{
    init();
    scanf("%d",&n);
    int p;
    for(int i=2; i<=n; i++)
    {
        scanf("%d",&p);
        addedge(p,i);
        addedge(i,p);
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&weight[i]);
    }
    dfs(1);
    pa tmp;
    tmp.first=st[1];
    tmp.second=1;
    vi[ed[1]].push_back(tmp);
    for(int i=2; i<=n; i++)
    {
        tmp.first=st[i];
        tmp.second=i;
        vi[ed[i]].push_back(tmp);
        tmp.first=ed[i]+1;
        tmp.second=i;
        vi[st[i]-1+n].push_back(tmp);
    }
    for(int i=1;i<=2*n;i++)
    {
        if(vis[weight[dfn[i]]])
        {
            update(vis[weight[dfn[i]]],-1);
        }
        update(i,1);
        vis[weight[dfn[i]]]=i;
        for(auto u:vi[i])
        {
            ans[u.second]+=query(i)-query(u.first-1);
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)
        {
            res=max(res,ans[i]);
        }
        printf("%d\n",res);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值