• ACM1325Is it A tree?


    通过这道简单而又坑人的题目,练习并查集和set 容器的使用;

    Is It A Tree?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    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.
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<set>
     4 using namespace std;
     5 const int N=20000;
     6 int p[N];
     7 bool flag;
     8 int find(int x)
     9 {
    10     if(x==p[x])
    11     return x;
    12     return p[x]=find(p[x]);
    13 }
    14 void link(int a,int b)
    15 {
    16     int fa=find(a);
    17     int fb=find(b);
    18     if(fa==fb)
    19         flag=true;
    20     else
    21     {
    22         if(fb!=b)
    23             flag=true;
    24         p[fb]=fa;
    25     }
    26 }
    27 int main()
    28 {
    29     int n,m;
    30     int cd=1;
    31     while(true)
    32     { 
    33         set<int>q;
    34         flag=false;
    35         for(int i=1;i<N;i++)
    36         p[i]=i;
    37         scanf("%d %d",&n,&m);
    38         if(n==0&&m==0)
    39         {
    40             printf("Case %d is a tree.
    ",cd);
    41             cd++;
    42             continue;
    43         }
    44         if(n<0&&m<0)break;
    45         q.insert(n);
    46         q.insert(m);
    47         link(n,m);
    48         while(scanf("%d%d",&n,&m)==2&&n&&m)
    49         {
    50             q.insert(n);
    51             q.insert(m);
    52             link(n,m);
    53         }
    54         int count=-1;
    55         set<int>::iterator it;
    56         for(it=q.begin();it!=q.end();it++)
    57         {
    58             if(p[*it]==*it)
    59             count++;
    60         }
    61         if(count>0)flag=true;
    62         if(flag==true)
    63         printf("Case %d is not a tree.
    ",cd);
    64         else printf("Case %d is a tree.
    ",cd);
    65         cd++;
    66     }
    67     return 0;
    68 }
    What I don't dare to say is I can't!
  • 相关阅读:
    剑指offer——72圆圈中最后剩下的数字
    剑指offer——71扑克牌中的顺子
    剑指offer——70n个骰子的点数
    剑指offer——69队列的最大值
    剑指offer——68队列的最大值
    剑指offer——67左旋转字符串
    剑指offer——66翻转字符串
    剑指offer——65和为S的连续正数序列
    「日常训练」COMMON 约数研究(HYSBZ-1968)
    「暑期训练」「基础DP」免费馅饼(HDU-1176)
  • 原文地址:https://www.cnblogs.com/sytu/p/3854413.html
Copyright © 2020-2023  润新知