Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Description
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
Output
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。 问题分析:题目要求判断是否为树,先理清是树的条件,树是一种无向简单连通图
1.无回路和环 也就是说(1,2)(2,1)或者(1,1)或者(1,5)(1,5)这些情况都是不行的。
2.要点与点之间连通,而且注意空树(即无点无边的树)也是树。
#include "cstdio"
struct node
{
int i;
int j;
};
node a[],b[];
int number,time=;
int nbegin()
{
int i=;
time++;
while (scanf ("%d%d",&a[i].i,&a[i].j)&& (a[i].i != - || a[i].j != -))
{
number = i;
if (a[i].i || a[i].j)
i++;
else
return ;
}
return ;
}
void tree()
{
int now,next,q;
node y;
if (a[number].i == && a[number].j == && number == )
{
printf ("Case %d is a tree.\n",time);
return;
}
for (int i=;i<number;i++)
{
for (int k=;k<number;k++)
{
if (a[i].j == a[k].j && i != k)
{
printf("Case %d is not a tree.\n",time);
return;
}
if (a[i].i == a[k].j && a[i].j == a[k].i)
{
printf("Case %d is not a tree.\n",time);
return;
}
}
}
for (int i=;i<number;i++)
{
now=;
next=;
q=;
for (int t=;t<number;t++)
if (a[t].i == a[i].i )
{
b[next++] = a[t];
q++;
}
while (next != now)
{
y = b[now];
now++;
for (int j=;j<number;j++)
if (y.j == a[j].i && y.i != a[j].j)
{
b[next++] = a[j];
q++;
}
}
if (q == number)
{
printf ("Case %d is a tree.\n",time);
return;
}
}
printf("Case %d is not a tree.\n",time);
}
int main()
{
while(nbegin())
tree();
return ;
}