zoukankan      html  css  js  c++  java
  • 统计节点个数

    Description
    给出一棵有向树,一共有N (1<N≤1000 )个节点,如果一个节点的度(入度+出度)不小于它所有儿子以及它父亲的度(如果存在父亲或儿子),那么我们称这个节点为p节点,现在你的任务是统计p节点的个数。
    InputFormat
    第一行为数据组数T (1≤T≤100 )。
    每组数据第一行为N 表示树的节点数。后面为N−1 行,每行两个数x ,y (0≤x,y<N ),代表y 是x 的儿子节点。
    OutputFormat
    每组数据输出一行,为一个整数,代表这棵树上p节点的个数。
    SampleInput
    2
    5
    0 1
    1 2
    2 3
    3 4
    3
    0 2
    0 1
    SampleOutput
    3
    1

    #include <iostream>
    using namespace std;
    struct node
    {
           int count;
           int child[1010];
           int father;
    }a[1010];
    int c[1010]={0};
    int d[1010]={-1};
    int main()
    {
       
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int t;
        cin>>t;
        for(int i=0;i<t;i++)
        {
                for(int s=0;s<1010;s++)
                {
                        for(int q=0;q<1010;q++)
                        {
                                a[s].child[q]=0;
                        }
                        c[s]=0;
                        a[s].father=0;
                        a[s].count=0;
                        d[s]=-1;
                }
                int n;
                cin>>n;
                for(int j=0;j<n-1;j++)
                {
                      int x,y;
                      cin>>x>>y;
                      a[x].count++;
                      a[y].count++;
                      a[y].father=x;
                      a[x].child[c[x]]=y; 
                      d[y]++; 
                      c[x]++;
                }
                int ans=0;
                for(int j=0;j<n;j++)
                {
                       int flag=1; 
                       if(d[j]==-1)
                        {
                             
                             for(int k=0;k<c[j];k++)
                             {
                                     if(a[j].count<a[a[j].child[k]].count)
                                     {
                                          flag=0;
                                          break;
                                     }
                             }   
                        }
                        else if(c[j]!=0)
                        {
                            for(int k=0;k<c[j];k++)
                             {
                                     if(a[j].count<a[a[j].child[k]].count)
                                     {
                                          flag=0;
                                          break;
                                     }
                             } 
                             if(a[j].count<a[a[j].father].count) 
                              {   flag=0;}
                        }
                        
                        else if(a[j].count<a[a[j].father].count)
                            {flag=0;}
                        if(flag==1)
                          ans++;
                }
                cout<<ans<<endl;
        }
       
        return 0;
    }
  • 相关阅读:
    OPENWRT 支持git
    LUCI MVC框架
    luci范例
    Hive | Hive可以避免进行MapReduce
    ORACLE日常-oracle中给date型字段insert into值
    ORACLE日常-ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法
    ORACLE日常-oracle字符串操作:拼接、替换、截取、查找
    HDFS中的shell操作
    java日常-map转成json
    java学习-reflection反射机制
  • 原文地址:https://www.cnblogs.com/xym4869/p/8658164.html
Copyright © 2011-2022 走看看