HDU 1325,POJ 1308 Is It A Tree

HDU认为1>2,3>2不是树,POJ认为是,
而Virtual Judge上引用的是POJ数据
这就是唯一的区别....(因为这个瞎折腾了半天)

此题因为是为了熟悉并查集而刷,其实想了下其实好好利用sort应该能更简单A掉,下次有空再去试试...

题目大意:判断是否为树,so:

1,无环;

2,除了根,所有的入度为1,根入度为0;

3,这个结构只有一个根,不然是森林了;
4.空树也是树,即第一次输入的两个数字为0 0则是树,其他时候输入只是结束条件

因为POJ和HDU题面一样,要求不一样,所以这题解法唯一区别就是
HDU上用所有节点的总父节点唯一判断,而POJ则每输入两个数,判
断他们总父节点是否相等,相等则成环了.

hdu AC代码:http://paste.ubuntu.com/25081598/
POJ AC代码:http://paste.ubuntu.com/25081600/

下面贴的是POJ上AC的代码:

//Is It A Tree?
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 1005
int pre[maxn],visB[maxn],root,vis[maxn],MAX,flag=0;
void iint()
{
    for(int i=0; i<=maxn; i++)
    {
        pre[i]=i;
        visB[i]=0;
        vis[i]=0;
    }
    MAX=0;  flag=0;
}
int find(int x)
{
    return x==pre[x]?x:find(pre[x]);
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)  pre[fx]=fy;
}
int main()
{
    int a,b,Case=1/*,real_flag1=0*/,FLAG=0;
    iint();
    while(~scanf("%d%d",&a,&b),a>=0&&b>=0)
    {
        if(a==0&&b==0&&flag==0)
        {   //空树也是树
            printf("Case %d is a tree.\n",Case++);
            iint();
            continue;
        }
        flag=1;
        visB[b]++;
        vis[a]=vis[b]=1;

        MAX=max(MAX,a);    MAX=max(MAX,b);
        /*for(int i=1; i<=MAX; i++)
        {
            if(vis[i]&&visB[i]>1)  real_flag1=1;
            //判断根节点是否都有且只有一次,或者说父节点只有一个
            if(vis[i]&&visB[i]==0)   real_flag2++;
            //非环时,最高父节点不会被指向,所以不被指的有且只有1个
            //POJ会WA可能是,如3->1  2->1都算一个树了,2333
        }*/
        /*if(real_flag1)   FLAG=1;*/   //天真

        if(find(a)==find(b)&&a!=0&&b!=0) FLAG=1;
       记住:1->2 ,3->2时候也是树,所以判断是否为环,直接在每次读       入两个数字时候看它们的最高父节点是否相等,相等泽成环,比如
1->2,2->3,3>1.

        join(a,b);

        if(a==0&&b==0)
        {
            int ans=0;
            for(int i=1; i<=MAX; i++)
                if(vis[i]&&pre[i]==i)  ans++;

            if(ans!=1||FLAG)  printf("Case %d is not a tree.\n",Case++);
            else  printf("Case %d is a tree.\n",Case++);

            iint();
            /*real_flag1=0;*/ FLAG=0;
            continue;
        }
    }
    return 0;
}

  

时间: 2024-07-31 14:20:03

HDU 1325,POJ 1308 Is It A Tree的相关文章

HDU 1816, POJ 2723 Get Luffy Out(2-sat)

HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 题意:N串钥匙.每串2把,仅仅能选一把.然后有n个大门,每一个门有两个锁,开了一个就能通过,问选一些钥匙,最多能通过多少个门 思路:二分通过个数.然后对于钥匙建边至少一个不选,门建边至少一个选,然后2-sat搞一下就可以. 一開始是按每串钥匙为1个结点,但是后面发现数据有可能一把钥匙,出如今不同串(真是不合

HDU 1815, POJ 2749 Building roads(2-sat)

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

POJ 1308 Is It A Tree? &amp;&amp; NYOJ 129 (树的判定+并查集)

[题目链接]click here~~ [题目大意]给定多对节点,判断所有节点能否组成一棵树 [解题思路]并查集的基本操作,定义node,edge,统计node和edge的数目,如果(edge==node-1||node==0)则可以成树 树的判定:n个节点,最多n-1条环,只有一个入度为边,不成0 的点,其他入度不大于1,不过要注意poj数据里如果1 1 0 0也会不符合要求,也就是不能自己指向自己 代码: /* Author:HRW 树的判定: n个节点,最多n-1条边 不成环 只有一个入度为

POJ 1308 Is It A Tree? (并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 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

POJ 1308 Is It A Tree?

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24299   Accepted: 8339 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

POJ 1308 Is It A Tree?--题解报告

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31092   Accepted: 10549 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 edg

POJ 1308 Is It A Tree? (并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23006   Accepted: 7898 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

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

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