/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2012 panyanyany All rights reserved.
URL : http://acm.hdu.edu.cn/showproblem.php?pid=1325
Name : 1325 Is It A Tree?
Date : Tuesday, April 10, 2012
Time Stage : one hour
Result:
5744703 2012-04-10 16:19:24 Accepted 1325
0MS 612K 1133 B
C++ pyy
Test Data :
Review :
hdu 的数据比poj弱,没有以下两种情况:
0 0
1 1 0 0
答案都是 not a tree。
除此之外要注意的是循环的情况:
1 2 1 3 2 1 0 0 // not a tree
和两棵树的情况:
1 2 3 4 0 0 //not a tree
//----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std ;
#define MAXN (1002)
int set[MAXN];
int flag;
int tcase;
void init()
{
int i;
memset(set, -1, sizeof(set));
flag = 0;
}
int find(int x)
{
if (set[x] == -1)
set[x] = x;
if (set[x] == x)
return x;
return set[x] = find(set[x]);
}
int main()
{
int i, j;
int x, y;
tcase = 1;
init();
while (scanf("%d %d", &x, &y) != EOF)
{
if (x == -1 && y == -1)
break;
if (x == 0 && y == 0)
{
printf ("Case %d", tcase++);
if (!flag)
{
j = 0;
for (i = 0; i < MAXN; ++i)
if (set[i] == i)
++j;
if (j > 1) // not a tree: 1 2 3 4 0 0, a tree: 0 0
flag = 1;
}
if (!flag)
{
printf (" is a tree.\n");
}
else
printf (" is not a tree.\n");
init();
continue;
}
int px = find(x);
int py = find(y);
if (py != y && px != py || x == y || (x != y && px == py))// not a tree: 1 2 1 3 2 1 0 0
{// not a tree: 1 1 0 0
flag = 1;
}
else
set[py] = set[px];
}
return 0;
}