• NYOJ129 树的判定


     

    树的判定

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述

    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.

    The number of test cases will not more than 20,and the number of the node will not exceed 10000.
    The inputs will be ended by a pair of -1.
    输出
    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.
    /* 功能Function Description:     NYOJ-129 树的判定   POJ-1308 
       开发环境Environment:          DEV C++ 4.9.9.1
       技术特点Technique:
       版本Version:
       作者Author:               	 可笑痴狂
       日期Date:                 	 20120730
       备注Notes:                    
    		这道题的题意比较简单,就是判断是不是一棵树,若是树,需满足三个条件:
    		1、无环。由题意可知,如果有环的话,肯定不是一棵树。
    		2、所有节点在一个集合中。
    		3、节点数目减去边数等于1。
    		满足这三个条件的话,就是一棵树。需要注意的是,空树也是一棵树。
    	
    
    */
    /*
    //代码一:(wrong answer)----原因:没有判断是否连通--可能存在这种情况,给出的数据可以生成一棵树和另一个环这样也满足只有一个节点入度为零,其他节点入度都是一。
    							// 本题正确做法应使用---并查集 。  
    #include<cstdio>
    #include<cstring>
    #define MAX 10001
    
    struct Node
    {
    	bool isroot;		//标记该节点是否做过跟节点
    	int indegree;		//节点入度
    }node[MAX];
    
    
    int main()
    {
    	int a,b,flag,i,T=1;
    	while(scanf("%d%d",&a,&b))
    	{
    		flag=0;
    		if(a==-1&&b==-1)
    			break;
    		for(i=0;i<MAX;++i)
    		{
    			node[i].isroot=false;
    			node[i].indegree=0;
    		}
    		node[a].isroot=true;
    		node[b].indegree++;
    		while(scanf("%d%d",&a,&b),a||b)
    		{
    			node[a].isroot=true;
    			node[b].indegree++;
    		}
    		for(i=1;i<MAX;++i)
    		{
    			if(node[i].isroot==true&&node[i].indegree==0)
    				++flag;      //标记可以作为根节点的节点个数
    			if(node[i].indegree>1)
    			{
    				flag+=2;
    				break;
    			}
    		}
    		if(flag==1)
    			printf("Case %d is a tree.\n",T++);
    		else
    			printf("Case %d is not a tree.\n",T++);
    	}
    	return 0;
    }
    
    */
    //代码二:(AC)
     
    #include<stdio.h>
    #include<string.h>
    int father[10005];
    
    int Find(int x)
    {
    	if(father[x]==-1) 
    		return x;
    	else 
    		return Find(father[x]);
    }
    int main()
    {
    	int n,m,i,j,x,y,flag,sum,k,T;
    	bool visit[10001];              //访问标记数组
    	flag=T=0;
    	while(scanf("%d%d",&x,&y),x!=-1||y!=-1)
    	{	
    		memset(father,-1,sizeof(father));  //初始化
    		memset(visit,false,sizeof(visit));
    		k=flag=0;
    		sum=1;
    		if(x==0&&y==0)        //空树
    		{
    			printf("Case %d is a tree.\n",++T);
    			continue;
    		}
    		if(x==y)              //出现自己到自己的环
    			flag=1;
    		else
    		{
    			visit[x]=visit[y]=true;  
    			k+=2;             //k记录节点个数
    		}
    		father[y]=x;          //因为是第一对节点,可以直接赋值
    		while(scanf("%d%d",&x,&y)&&(x||y))   //输入0,0时结束
    		{
    			if(flag) 
    				continue;
    			if(!visit[x])      
    			{
    				visit[x]=true;
    				++k;
    			}
    			if(!visit[y])
    			{
    				visit[y]=true;
    				++k;
    			}
    			if(x==y)	      //出现自己到自己的环
    			{
    				flag=1;
    				continue;
    			}
    			n=Find(x); 
    			m=Find(y);
    			if(n==m||m!=y) 
    		//	if(n==m)  (改成这样是wrong answer)        
    			{
    				flag=1;
    				continue;
    			}
    			father[m]=n; 
    			sum++;           //记录边的数目
    		}
    		if(flag||k!=sum+1)  
    			printf("Case %d is not a tree.\n",++T);
    		else  
    			printf("Case %d is a tree.\n",++T);
    	}
    	return 0;
    }         
    
    //代码三:
    
    #include<iostream>
    using namespace std;
    #define MAXN 100
    int set[MAXN]; //set[]记录每个节点的父节点
    
    int FindSet(int x) //寻找x所在根的根节点
    {
         if(set[x]==x)
             return x;
         if(set[x]==-x)
             return -x;
         else
             set[x]=FindSet(set[x]);
         return set[x];
    }
    
    int main()
    {
         int pointa,pointb,FSa,FSb;
         int cas=1,num=0,I;
         bool judge=0,flag=0;//judge为是否判断完,flag为是否有边输入
         for(I=0;I<MAXN;I++)
             set[I]=-I;
         while(cin>>pointa>>pointb,pointa>=0)
         {
             if(pointa==0)
             {
                      if(num==1||flag==0)
                           cout<<"Case "<<cas<<" is a tree.\n";
                       else
                           cout<<"Case "<<cas<<" is not a tree.\n";
                       cas++;
                       flag=0;
                       num=0;
                       judge=0;
                       for(I=0;I<MAXN;I++)
                           set[I]=-I;
             }
             else
             {
                  flag=1; //有边
                  FSa=FindSet(pointa);
                  if(FSa==-pointa)
                       set[pointa]=pointa;
                  FSb=FindSet(pointb);
                  if(FSb==-pointb)
                       set[pointb]=pointb;
                  if(FSa==FSb) 
                  {
                       num=0;
                       judge=1;
                  }    
                  if(judge==0)
                  {    
                       if(FSb==pointb) //pontb是已出现过的根节点
                       {
                           if(FSa!=-pointa) //当pointa不是还没有出现过的点
                           {
                                num--;
                           }
                           set[pointb]=FindSet(pointa);
                       }
                       else
                       {
                           if(FSb==-pointb) //pointb还没有出现过
                           {
                                if(FSa==-pointa) //pointa还没有出现过
                                {
                                     num++;
                                }
                                set[pointb]=FindSet(pointa);
                           }
                           else
                           {
                                num=0;
                                judge=1;
                           }
                       }
                  }
             }
         }
         return 0;
    }
     
    
           
    
    功不成,身已退
  • 相关阅读:
    java之collection总结
    JAVA集合详解(Collection和Map接口)
    Java集合框架(Collection Framework)学习之 Collection与Map概貌
    (二)用less+gulp+requireJs 搭建项目(gulp)
    (一)用less+gulp+requireJs 搭建项目(了解less)
    数组API汇总
    我的vim配置
    Educational Codeforces Round 47 D
    [AHOI2008]上学路线
    [SDOI2016]硬币游戏
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2618862.html
Copyright © 2020-2023  润新知