zoukankan      html  css  js  c++  java
  • *HDU1325 并查集

    Is It A Tree?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 22403    Accepted Submission(s): 5088


    Problem Description
    A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
    There is exactly one node, called the root, to which no directed edges point.

    Every node except the root has exactly one edge pointing to it.

    There is a unique sequence of directed edges from the root to each node.

    For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



    In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

     
    Input
    The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
     
    Output
    For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
     
    Sample Input
    6 8 5 3 5 2 6 4 5 6 0 0
    8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0
    3 8 6 8 6 4 5 3 5 6 5 2 0 0
    -1 -1
     
    Sample Output
    Case 1 is a tree.
    Case 2 is a tree.
    Case 3 is not a tree.
     
    Source
     
    题意:
    给出若干对单向联通的数,问这些数能否组成一棵树。输入两个负数结束。
    代码:
     1 //判断两点,无环,非森林。
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 int fat[1005];
     7 int nod[1005];
     8 int find(int x)
     9 {
    10     if(fat[x]!=x)
    11     fat[x]=find(fat[x]);
    12     return fat[x];
    13 }
    14 void connect(int x,int y)
    15 {
    16     int xx=find(x),yy=find(y);
    17     if(xx!=yy)
    18     fat[xx]=yy;
    19 }
    20 int main()
    21 {
    22     int a,b;
    23     for(int i=0;i<1000;i++)
    24     fat[i]=i;
    25     int t=0,k=1,flag=0;
    26     memset(nod,0,sizeof(nod));
    27     while(scanf("%d%d",&a,&b))
    28     {
    29         if(a<0&&b<0) break;
    30         if(a==0&&b==0)
    31         {
    32             if(!flag)      //判断是否是森林
    33             {
    34                 int tem=0;
    35                 for(int i=1;i<1000;i++)
    36                 {
    37                     if(!nod[i]) continue;
    38                     if(tem==0) tem=find(i);
    39                     else if(tem!=0&&find(i)!=tem)
    40                     {
    41                         flag=1;
    42                         break;
    43                     }
    44                 }
    45             }
    46             if(flag) printf("Case %d is not a tree.
    ",k);
    47             else printf("Case %d is a tree.
    ",k);
    48             t=0;
    49             flag=0;
    50             k++;
    51             for(int i=0;i<1000;i++)
    52             fat[i]=i;
    53             memset(nod,0,sizeof(nod));
    54             continue;
    55         }
    56         nod[a]=1;nod[b]=1;
    57         if(flag) continue;
    58         if(fat[b]!=b||find(a)==b)   //不能是环:b之前不能有父亲,a,b不能互相连接
    59         {
    60             flag=1;
    61             continue;
    62         }
    63         fat[b]=a;
    64         connect(b,a);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    【新阁教育】爱普生机器人建立工具坐标系教程
    BPF CO-RE 示例代码解析
    gRPC Load Balancing
    Linux Clone函数
    高性能 Nginx HTTPS 调优
    内网渗透测试:内网横向移动基础总结
    Python 运算符
    华为云-容器引擎CCE-部署Nginx应用
    华为云-云容器引擎(CCE)-高危操作及解决方案
    周杰伦新歌《说好不哭》上线,程序员哭了......
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6014033.html
Copyright © 2011-2022 走看看