Codeforces 682C: Alyona and the Tree

  

Alyona and the Tree

题目链接:

http://codeforces.com/contest/682/problem/C

题意:

有一棵树,以1为根,每条边和每个点都有相应的权值,删除一些叶子节点使得所有子树(设V为根节点)不存在一个点U,V到U的边权合大于U点的权值

 

题解:

从1开始dfs,搜到不满足条件的点把以它为根的子树删掉。

             

代码

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
long long a[100005];
bool mark[100005];
int res=0;
struct node
{
  int to;
  long long x;
  node(int xx,long long yy)
  {
    to=xx;x=yy;
  }
};
vector<node>q[100005];
void dfs(int xt,long long u)
{
  res++;
  mark[xt]=true;
  for(int i=0;i<q[xt].size();++i)
  {
    node mm=q[xt][i];
    int v=mm.to;
    long long w=mm.x;
    if(!mark[v])
    {
      if(w+u>a[v]||w>a[v])
      {
        mark[v]=true;
      }
      else
      {
        if(u>=0)dfs(v,u+w);
        else dfs(v,w);
      }
    }
  }
}
int main()
{
  int n,v;
  long long w;
  scanf("%d",&n);
  for(int i=1;i<=n;++i)
    scanf("%lld",&a[i]),mark[i]=false,q[i].clear();
  for(int i=2;i<=n;++i)
  {
    scanf("%d%lld",&v,&w);
    q[i].push_back(node(v,w));
    q[v].push_back(node(i,w));
  }
  res=0;
  dfs(1,0);
  printf("%d\n",n-res);
}

  

posted @ 2016-06-18 19:46  kiuhghcsc  阅读(225)  评论(0编辑  收藏  举报