zoukankan      html  css  js  c++  java
  • POJ 1308 Is It A Tree?【裸的并查集】

    Language:

    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.

    思路:题意很简单,但是要注意的很多,代码中有注释;

    代码如下:

    View Code
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    int f[1005], visit[1005], map[1005][1005]; 
    int find(int x)
    {
        return x==f[x]?x:f[x]=find(f[x]);
    }
    int main()
    {
        int x, y, xx, yy, maxnum, i;
        int cas=0; 
        while(1)
        {    
            memset(visit, 0, sizeof(visit));
            memset(map, 0, sizeof(map)); 
            for(i=0; i<=1000; i++)
                f[i]=i;
            int flag=1;
            maxnum=0;
            while(1)
            {
                scanf("%d%d", &x, &y);
                if(x==-1&&y==-1)    return 0;
                if(x==0&&y==0)    break;
                 maxnum=max(maxnum, x); maxnum=max(maxnum, y); 
                xx=find(x), yy=find(y);
                visit[x]=1, visit[y]=1;
                if(yy!=y&&xx!=yy||x==y||map[x][y]==1||map[y][x]==1)//没有重复的 1 2 1 2;没有1 2 2 1; 没有多个父节点的  
                    flag=0;
                f[yy]=xx;
                map[x][y]=1;
            }
            int num=0; 
            for(i=0; i<=maxnum; i++)
                if(f[i]==i&&visit[i])
                    num++;
             if(num>1)      flag=0;
             cas++; 
             if(flag)
                 printf("Case %d is a tree.\n", cas);
             else
                 printf("Case %d is not a tree.\n", cas);
        }
    }
  • 相关阅读:
    MySQL之架构与历史(二)
    MySQL之架构与历史(一)
    MySQL之体系结构与存储实例
    Redis实现之复制(二)
    Redis实现之复制(一)
    选项卡
    滑动效果
    选择器
    下拉列表
    1.__tostring()这个方法在类里可以直接输出对象。2.克隆对象的运用
  • 原文地址:https://www.cnblogs.com/Hilda/p/2626575.html
Copyright © 2011-2022 走看看