• 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-res);
    }

      

  • 相关阅读:
    SOGo 2.0 发布,群组协同工作系统
    微软随.NET 4.5发布新REST API框架
    DynamicReports 3.0.2 发布,Java 报表方案
    使用 Ant 集成 IBM Security AppScan Standard 进行自动测试
    SUSE 用 zypper 工具 安装 rpm
    嵌入式ARM系统中OpenCV的移植
    qtopiax86安装配置及编程方法
    [转]QTCreator的使用
    在Qt Creator中使用OpenCV库
    vim
  • 原文地址:https://www.cnblogs.com/kiuhghcsc/p/5596751.html
Copyright © 2020-2023  润新知