zoukankan      html  css  js  c++  java
  • POJ 1308 Is It A Tree?

    换个马甲又是一题
    Is It A Tree?
    Time Limit: 1000MSMemory Limit: 10000K
    Total Submissions: 17610Accepted: 6029

    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. 
    POJ 1308 Is It A Tree? - qhn999 - 码代码的猿猿

    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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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


    #include <cstdio>
    #include <cstring>

    #define N 101000

    using namespace std;

    int fa[N];
    int num[N];
    int OK=1;

    void inti()
    {
        for(int i=0;i<N;i++)
            fa=i;
    }

    int Find(int x)
    {
        if(x==fa[x])  return x;
        else
        Find(fa[x]);
    }

    void Union(int a,int b)
    {
        num[a]=1;num=1;
        int ra=Find(a);
        int rb=Find(b);
        if(ra==rb) return ;
        else
        {
            fa[rb]=ra;
        }
        return;
    }
    int tot=1;
    int main()
    {
        int a,b;
        inti();
        OK=1;
        memset(num,0,sizeof(num));
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            if(a==-1&&b==-1)  break;
            if(a==0&&b==0)
            {
                if(OK==1)
                {
                    int sum=0;
                    for(int i=1;i<N;i++)
                    {
                        if(fa==i&&num==1)
                            sum++;
                    }
                    if(sum<=1)
                       printf("Case %d is a tree.\n",tot++);
                    else
                       printf("Case %d is not a tree.\n",tot++);
                    inti();
                    OK=1;
                    memset(num,0,sizeof(num));
                }
                else printf("Case %d is not a tree.\n",tot++);
                inti();
                OK=1;
                memset(num,0,sizeof(num));
            }
            else
            {
                int ra=Find(a);
                int rb=Find(b);
                if(ra==rb) OK=0;
                else Union(a,b);
            }
        }

        return 0;
    }

  • 相关阅读:
    大二下每周总结
    大二下学期阅读笔记(人月神话)
    大二下学期第一次结对作业(第二阶段)
    大二下学期第一次结对作业(第二阶段)
    大二下学期项目练习(滑屏数据展示)
    elasticSearch中使用聚合查询后只显示10个bucket的问题
    elasticsearch Terms Query 实现类似于sql in查询
    crontab定时调用shell脚本
    java使用static静态变量
    ElasticSearch 复合数据类型——数组,对象和嵌套
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351071.html
Copyright © 2011-2022 走看看