ACM求解1

来源:百度知道 编辑:UC知道 时间:2024/06/16 11:04:41
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.

http://acm.pku.edu.cn/JudgeOnline/images/1308_1.jpg

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 sa

这个是我从网上考来的答案,哈哈。

#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";