📄 pku1308.cpp
字号:
#include <stdio.h>
int st[100];
int getid(int x)
{
int root;
if (st[x] == x)
{
return x;
}
root = getid(st[x]);
st[x] = root;
return root;
}
int main()
{
int s, e, i, l, istree, t = 0, totalid;
while (scanf("%d%d", &s, &e) != -1)
{
t++;
istree = 1;
for (i = 0; i < 100; i++)
{
st[i] = -1;
}
if (s == -1 && e == -1)
{
break;
}
if (s == 0 && e == 0)
{
printf("Case %d is a tree.\n", t);
continue;
}
st[e] = e;
st[s] = s;
if (s == e)
{
istree = 0;
}
totalid = 2;
st[e] = s;
l = 1;
while (scanf("%d %d", &s, &e) != -1)
{
if (s == 0 && e == 0)
{
break;
}
l++;
if (st[s] == -1)
{
st[s] = s;
totalid++;
}
if (st[e] == -1)
{
st[e] = e;
totalid++;
}
if (getid(s) == getid(e))
{
istree = 0;
}
else st[getid(e)] = getid(s);
}
if (istree == 1 && l + 1 == totalid)
{
printf("Case %d is a tree.\n", t);
}
else
{
printf("Case %d is not a tree.\n", t);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -