poj1308

简单题

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

#define maxn 1006

struct Edge
{
int v, next;
}edge[
1000005];

int name[maxn];
int n, ecount, tot, root;
int in[maxn];
int head[maxn];
bool vis[maxn];
bool ok;

void addedge(int a, int b)
{
edge[ecount].next
= head[a];
edge[ecount].v
= b;
head[a]
= ecount;
in[b]++;
ecount
++;
}

int find(int a)
{
for (int i = 0; i < n; i++)
if (name[i] == a)
return i;
name[n
++] = a;
return n - 1;
}

void dfs(int root)
{
vis[root]
= true;
tot
++;
for (int i = head[root]; i != -1; i = edge[i].next)
{
if (vis[edge[i].v])
{
ok
= false;
return;
}
if (!ok)
return;
dfs(edge[i].v);
}
}

int main()
{
//freopen("t.txt", "r", stdin);
int t = 0;
while (1)
{
t
++;
memset(
in, 0, sizeof(in));
memset(head,
-1, sizeof(head));
memset(vis,
0, sizeof(vis));
ecount
= 0;
n
= 0;
tot
= 0;
ok
= true;
int a, b;
scanf(
"%d%d", &a, &b);
if (a < 0 && b < 0)
return 0;
if (a == 0 && b == 0)
{
printf(
"Case %d is a tree.\n", t);
continue;
}
a
= find(a);
b
= find(b);
addedge(a, b);
while (1)
{
scanf(
"%d%d", &a, &b);
if (a == 0 && b == 0)
break;
a
= find(a);
b
= find(b);
addedge(a, b);
}
root
= -1;
for (int i = 0; i < n; i++)
if (in[i] == 0)
root
= i;
if (root >= 0)
dfs(root);
if (ok && tot == n && root >= 0)
printf(
"Case %d is a tree.\n", t);
else
printf(
"Case %d is not a tree.\n", t);
}
return 0;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/06/21/2086368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值