zoukankan      html  css  js  c++  java
  • HDU1308 Is It A Tree?(简单并查集)

    描述

    传送门:我是传送门

    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.

    输入

    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.

    输出

    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).

    样例

    输入

    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

    输出

    Case 1 is a tree.
    Case 2 is a tree.
    Case 3 is not a tree.

    思路

    HDU1272类似

    写错find()坑了自己半个小时

    注意坑点

    • 0 0 也算一棵树
    • 不要忽略每一行输出的最后那个’.’

    代码

      1 /*
      2  * ==============================================
      3  *
      4  *       Filename:  N.cpp
      5  *
      6  *           Link:  http://poj.org/problem?id=1308
      7  *
      8  *        Version:  1.0
      9  *        Created:  2018/09/24 02时59分21秒
     10  *       Revision:  none
     11  *       Compiler:  g++
     12  *
     13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
     14  *   Organization:  QLU_浪在ACM
     15  *
     16  * ==============================================
     17  */
     18 #include<cstdio>
     19 #include<iostream>
     20 #include<cstring>
     21 #include<algorithm>
     22 using namespace std;
     23 #define clr(a, x) memset(a, x, sizeof(a))
     24 #define rep(i,a,n) for(int i=a;i<=n;i++)
     25 #define pre(i,a,n) for(int i=n;i>=a;i--)
     26 #define ll long long
     27 #define max3(a,b,c) fmax(a,fmax(b,c))
     28 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     29 const double eps = 1e-6;
     30 const int INF = 0x3f3f3f3f;
     31 const int mod = 1e9 + 7;
     32 const int N = 110;
     33 int f[N];
     34 bool vis[N];
     35 int find(int x)
     36 {
     37     if(f[x] != x)
     38     {
     39         f[x] = find(f[x]);
     40     }
     41     return f[x];
     42 }
     43 void init()
     44 {
     45     for(int i = 0;i < N;i++)
     46     {
     47         f[i] = i;
     48         vis[i] = 0;
     49     }
     50 }
     51 
     52 int x,y;
     53 int main()
     54 {
     55     ios
     56 #ifdef ONLINE_JUDGE 
     57 #else 
     58         freopen("in.txt","r",stdin);
     59     // freopen("out.txt","w",stdout); 
     60 #endif
     61     int ca = 1;
     62     int flag = 0;
     63     int tmp = -1;
     64     init();
     65     while(scanf("%d %d",&x,&y) && (x != -1 && y != -1))
     66     {
     67         if(x == 0 && y == 0)
     68         {
     69             // printf("tmp = %d !!!
    ",tmp);
     70             if(tmp != -1)
     71                 tmp = find(tmp);
     72             // printf("tmp = %d !!!
    ",tmp);
     73             for(int i = 0;i < N;i++)
     74             {
     75                 if(vis[i])
     76                 {
     77                     if(tmp != find(i))
     78                     {
     79                         // printf("%d
    ",i);
     80                         flag = 1;
     81                         break;
     82                     }
     83                 }
     84             }
     85             printf("Case %d ",ca++);
     86             if(flag)
     87             {
     88                 printf("is not a tree.
    ");
     89             }
     90             else 
     91             {
     92                 printf("is a tree.
    ");
     93             }
     94             flag = 0;
     95             tmp = -1;
     96             init();
     97             continue;
     98         }
     99         // printf("x = %d y = %d
    ",x,y);
    100         if(tmp == -1)
    101             tmp = x;
    102         // printf("tmp = %d
    ",tmp);
    103         int fx = find(x);
    104         int fy = find(y);
    105         // printf("fx = %d fy = %d
    ",fx,fy);
    106         vis[x] = 1;
    107         vis[y] = 1;
    108         // printf("vis[%d] = %d vis[%d] = %d
    ",x,vis[x],y,vis[y]);
    109         if(fx == fy)
    110         {
    111             flag = 1;
    112         }
    113         else 
    114         {
    115             // printf("f[%d] = %d
    ",fy,fx);
    116             f[fy] = fx;
    117         }
    118     }
    119 
    120     fclose(stdin);
    121     // fclose(stdout);
    122     return 0;
    123 }
  • 相关阅读:
    git技能
    iOS 命名规则
    iOS crash 报错类型
    iOS 面试相关
    【转】app后端如何选择合适的数据库产品
    App的token机制
    【转】Spring注解详解
    spring mvc ModelAndView 404的原因
    ibatis 环境搭建(1)
    Android中的Selector的用法
  • 原文地址:https://www.cnblogs.com/duny31030/p/14305053.html
Copyright © 2011-2022 走看看