• Is It A Tree? 挂着并查集的帽子招摇撞骗


    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.



    记录一下刚A了这道题的我此时的心情。
    重要的事情说三遍,,emmm 这道题巨坑!这道题巨坑!这道题巨坑!!!

    平静一下

    该题需要注意的是,空的图也是一棵树。

    解题思路 :1、判断是否有环。 2、判断入度为零的点只有一个。

    判断是否有环:若边数大于等于顶点数,则有环

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<string>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 #include<stack>
      9 #include<deque>
     10 #include<map>
     11 #include<set>
     12 #include<iostream>
     13 using namespace std;
     14 typedef long long  LL;
     15 const double pi=acos(-1.0);
     16 const double e=exp(1);
     17 const int N = 100000;
     18 
     19 int captain[N],check[N];
     20 
     21 struct con{
     22     int x;
     23     int y;
     24 }con[N];
     25 
     26 int seek(int x)
     27 {
     28 //    while(x!=captain[x])
     29 //        x=captain[x];
     30 //    return x;
     31     return captain[x];
     32 }
     33 
     34 void combine(int a,int b)
     35 {
     36     captain[b]=a;
     37 //    int boss1=seek(a);
     38 //    int boss2=seek(b);
     39 //    if(boss1!=boss2)
     40 //        captain[boss2]=boss1;
     41 
     42 }
     43 
     44 void init()
     45 {
     46     int i;
     47     for(i=0;i<=N-1;i++)
     48         captain[i]=i;
     49 }
     50 
     51 int main()
     52 {
     53     int i,p,j,n,a,b;
     54     int cnt=1,flag=0,edge=0;
     55     set<int > qq;
     56     while(1)
     57     {
     58         qq.clear();
     59         flag=edge=0;
     60         memset(con,0,sizeof(con));
     61         memset(captain,0,sizeof(captain));
     62         memset(check,0,sizeof(check));
     63         init();
     64 
     65         scanf("%d%d",&a,&b);
     66         if(a==0&&b==0)
     67         {
     68             printf("Case %d is a tree.
    ",cnt);
     69             cnt++;
     70             continue;
     71         }
     72         if(a<0&&b<0)
     73             break;
     74         qq.insert(a);
     75         qq.insert(b);
     76         edge++;
     77         con[0].x=a;
     78         con[0].y=b;
     79         combine(a,b);
     80         p=1;
     81         while(1)
     82         {
     83             scanf("%d%d",&a,&b);
     84             if(a==0&&b==0)
     85                 break;
     86 
     87             if(captain[a]==b||captain[b]==a)
     88                 flag=1;
     89 
     90             qq.insert(a);
     91             qq.insert(b);
     92             edge++;
     93             con[p].x=a;
     94             con[p++].y=b;
     95 
     96             combine(a,b);
     97         }
     98         int head=0;
     99         for(i=0;i<p;i++)
    100         {
    101             if(captain[con[i].x]==con[i].x&&check[con[i].x]==0)
    102             {
    103                 head++;
    104                 check[con[i].x]=1;
    105             }
    106             if(captain[con[i].y]==con[i].y&&check[con[i].y]==0)
    107             {
    108                 head++;
    109                 check[con[i].y]=1;
    110             }
    111             if(con[i].x==con[i].y)
    112                 flag=1;
    113         }
    114 
    115         if(head!=1)
    116             flag=1;
    117 
    118         int tail=qq.size();
    119         if(edge>=tail)
    120             flag=1;
    121 
    122         if(flag==1)
    123             printf("Case %d is not a tree.
    ",cnt);
    124         else
    125             printf("Case %d is a tree.
    ",cnt);
    126         cnt++;
    127     }
    128     return 0;
    129 }
    View Code
  • 相关阅读:
    postman 的基础使用篇(一)
    C# struct
    细说javascript typeof操作符
    javascript-void keyword
    深圳求生记
    博客园开篇--对程序员的一点看法
    京东2018校园招聘 数据开发
    数据结构之哈希、哈希函数、哈希表
    scrapy入门教程
    linux学习笔记1
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9709091.html
Copyright © 2020-2023  润新知