hdu 1325 Is it a tree

本文介绍了一种简单的方法来判断一组节点连接是否构成树结构。通过寻找根节点并进行线性遍历来实现,若遍历中发现重复节点或遍历结束后未覆盖所有节点,则判断为非树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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.


   这题是题水题,很多人说用并查集来判断连通和无环,但我觉得完全没有必要,只需要找出一个根节点,然后线性遍历就行了,如果遍历过程中出现了之前出现过的节点,这不是树,如果遍历完成后不够输入时那么多节点,则也不是树(此时很有可能是森林!),否则便是树:下面就是我的代码:


/*******************************************************************************/
/* OS           : Linux fc20.x86_64 #1 SMP Tue Dec  UTC 2013 x86_64  GNU/Linux
 * Compiler     : 4.8.2 20131212 (Red Hat 4.8.2-7) (GCC)
 * Encoding     : UTF8
 * Date         : 2014-04-02
 * All Rights Reserved by alop.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/

#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
struct node
{
 bool in;
 vector<int>v;
 bool flag;
 node(bool in=0,bool flag=0):in(in),flag(flag){}
};
int ca=0;
vector<node>v(30);
set<int>s;
bool solve()
{
 int i=0,n=s.size();
 for(;i<30;i++)
  if(v[i].v.size()!=0&&v[i].in==0)
   break;
int sum=0;
 if(i!=30)
 {
    queue<int>q;
    q.push(i);
    v[i].flag=1;
    sum++;
  while(!q.empty())
    {
              int m=q.front();q.pop();
              int ss=v[m].v.size();
              for(int j=0;j<ss;j++)
              {
                  int tmp=v[m].v[j];
                  if(v[tmp].flag)
                    return 0;
                  v[tmp].flag=1;
                  q.push(tmp);
                  sum++;
              }

    }

 }
    if(sum==n)
        return 1;
    return 0;
}
int main()
{
    int a,b;

 while(cin>>a>>b&&a>=0)
 {

  if(a==0&&b==0)
  {
   ca++;
   if(solve())
    cout<<"Case "<<ca<<" is a tree."<<endl;
   else
    cout<<"Case "<<ca<<" is not a tree."<<endl;
   s.clear();
   for(int i=0;i<30;i++)
   {
    v[i].v.clear();
    v[i].in=0;
    v[i].flag=0;
    }

  }
  else
  {
   v[a].v.push_back(b);
   v[b].in=1;
   s.insert(a);
   s.insert(b);

  }
}
return 0;
}

只要就行了,要注意此题节点很少,还有就是输入结束不是两个-1,而是<0。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值