#include <iostream>
using namespace std;
#define MAXN 100001
int father[ MAXN ];
int visited[ MAXN ];
int in[ MAXN ];
int out[ MAXN ];
bool flag;
void init()
{
for(int i = 0; i < MAXN; i ++)
{
father[ i ] = i;
visited[ i ] = 0;
in[ i ] = 0;
out[ i ] = 0;
}
flag = true;
}
int find(int s)
{
int t = s;
while(t != father[ t ])
{
t = father[ t ];
}
while(s != father[ s ])
{
father[ s ] = t;
s = father[ s ];
}
return t;
}
void merge(int a, int b)
{
int fa = find(a);
int fb = find(b);
if(fa == fb)
{
flag = false;
return;
}
else
{
father[ fb ] = fa;
}
}
int main()
{
int a, b;
int cid = 1;
init();
while(cin>>a>>b)
{
if(a == -1 && b == -1)
{
break;
}
if(a == 0 && b == 0)
{
int cnt = 0;
for(int i = 0; i < MAXN; i ++)
{
if(visited[ i ] == 1 && father[ i ] == i)
{
cnt ++;
}
if(in[ i ] >= 2)
{
flag = false;
}
}
if(cnt == 0)
{
printf("Case %d is a tree.\n", cid ++);
}
else if(flag && cnt == 1)
{
printf("Case %d is a tree.\n", cid ++);
}
else
{
printf("Case %d is not a tree.\n", cid ++);
}
init();
}
else
{
merge(a, b);
out[ a ] ++;
in[ b ] ++;
visited[ a ] = 1;
visited[ b ] = 1;
}
}
return 0;
}
杭电ACM1325(并查集)
最新推荐文章于 2020-03-16 21:31:20 发布