POJ1308 Is It A Tree?

题目大意:和HDU1272-小希的迷宫题目一样,

如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。 

Input

输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。 
整个文件以两个-1结尾。

题目思路:我们想一想,当从一个房间到达另一个房间存在不止一条路时会发生什么,首先肯定存在一个包含这两个点的环,既然存在环,那么这两点的根节点应该相同。好了问题解决了,只要发现新读入的两个相连的房间的根节点相同则证明,着不是个树,标记一下。思路有了,代码就是并查集模板了~

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1000005

using namespace std;

int father[MAX],vis[MAX];

int Find(int x)
{
    while(x!=father[x])
    {
        x=father[x];
    }
    return x;
}

int main()
{
    int a,b,ok,i,k,cnt=1;

    while(scanf("%d%d",&a,&b),a!=-1 || b!=-1)
    {
        k=0;
        memset(vis,0,sizeof(vis));

        vis[a]=1;
        vis[b]=1;

        ok=1;

        if(!a && !b)
        {
            printf("Case %d is a tree.\n",cnt++);
            continue;
        }

        for(i=0; i<MAX; i++)
            father[i]=i;

        int x=Find(a);
        int y=Find(b);

        if(x!=y)
        {
            father[x]=y;
        }

        else
            ok=0;

        while(scanf("%d%d",&a,&b),a||b)
        {
            vis[a]=1;
            vis[b]=1;
            int x=Find(a);
            int y=Find(b);

            if(x!=y)
            {
                father[x]=y;
            }
            else
                ok=0;//标记一下
        }

        if(ok)//当ok=0时就不进入了,节省下时间
        {
            for(i=0;i<MAX;i++)
            {
                if(vis[i] && father[i]==i)
                    k++;
            }
        }

        if(k>1)
            ok=0;

        if(ok)
            printf("Case %d is a tree.\n",cnt++);
        else
            printf("Case %d is not a tree.\n",cnt++);
    }

    return 0;
}

时间: 2024-10-09 16:55:39

POJ1308 Is It A Tree?的相关文章

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

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? (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

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基