Is It A Tree?----poj1308

http://poj.org/problem?id=1308

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff
using namespace std;

int f[N],vis[N];

int Find(int x)
{
    if(x!=f[x])
        f[x]=Find(f[x]);
    return f[x];
}

int main()
{
    int a,b,t=1,pa,pb,flag,i,first;
    while(scanf("%d%d",&a,&b),a!=-1||b!=-1)
    {
        for(i=0;i<N;i++)
            f[i]=i;
        memset(vis,0,sizeof(vis));
        flag=0;
        if(a==0&&b==0)
        {
            printf("Case %d is a tree.\n",t++);//空树也是树;
            continue;
        }
        vis[a] = vis[b] = 1;//说明此点出现过;
        first = a;
        if(a == b) flag = 1;
        pa = Find(a);
        pb = Find(b);
        if(pa != pb)
            f[pb] = pa;
        else
            flag = 1;
        while(scanf("%d%d", &a, &b), a+b)
        {
            vis[a] = vis[b] = 1;
            if(a == b) flag = 1;
            pa = Find(a);
            pb = Find(b);
            if(pa != pb)
                f[pb]=pa;
            else
            {
                flag=1;
            }
        }
        for(i=1; i<N; i++)
        {
            if(vis[i]==1 && Find(i) != Find(first))
            {
                flag=1;break;
            }
        }
        if(flag==1)
            printf("Case %d is not a tree.\n",t++);
        else
            printf("Case %d is a tree.\n",t++);
    }
    return 0;
}
时间: 2024-08-27 21:24:12

Is It A Tree?----poj1308的相关文章

hdu1325is it a tree?&amp;&amp;poj1308 is it a tree?(并查集)

题目链接: huangjing||huangjing 建议做hdu上的这个题,因为poj上面的数据很弱,就是因为只做了poj上面的导致我在一次比赛中一直wa到比赛结束,因为比赛的那次挂的是hdu上的题... 题意: 判断由给出的数据得到的是否是一棵树... 思路: 这个题有几个要注意的地方. [1]首先一棵树只能有一个入度为0的点即根节点..所以根节点唯一.. [2]除根节点外其他点的入度均为1. 题目: Language: Default Is It A Tree? Time Limit: 1

poj1308 Is It A Tree?(并查集)详解

poj1308   http://poj.org/problem?id=1308 题目大意:输入若干组测试数据,输入 (-1 -1) 时输入结束.每组测试数据以输入(0 0)为结束标志.然后根据所给的所有(父亲, 孩子)数据对判断 是否能构成一棵树. 分析: 都以了解树只有一个根节点,那么我们就判断是不是有多个树:又知道每个节点只有一个父亲节点,那么我们就判断他是不是构成环,成环则不是树. 注意: ①可以是空树: ②所给的节点构成森林(多个树)是不可以的,必须只能构成一棵树. #include<

hdu-1325 &amp; poj-1308 Is It A Tree?

http://acm.hdu.edu.cn/showproblem.php?pid=1325 题意: 判断一个图是不是说树 只有树的根入度为 0 ,其余结点入度均为 1; Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15503    Accepted Submission(s): 3443 Problem

POJ1308——Is It A Tree?

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22631   Accepted: 7756 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edge

POJ1308 Is It A Tree?

题目大意:和HDU1272-小希的迷宫题目一样, 如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路).小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路.比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8.  Input 输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号.房间的编号至少为1,

POJ1308 Is It A Tree? (easy but...)

Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, t

HDU-1325&amp;&amp;POJ-1308 Is It A Tree?(基础并查集)

Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26976    Accepted Submission(s): 6213 Problem Description A tree is a well-known data structure that is either empty (null, void, no

POJ-1308 Is It A Tree?(并查集判断是否是树)

http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.There is exactl

POJ1308

1.题目链接地址 http://poj.org/problem?id=1308 2.源代码 #include<iostream> using namespace std; #define MAXN 100 int set[MAXN]; //set[]记录每个节点的父节点 int FindSet(int x) //寻找x所在根的根节点 { if(set[x]==x) return x; if(set[x]==-x) return -x; else set[x]=FindSet(set[x]);