关闭

poj1308——Is It A Tree?(判断是否为树)

标签: poj结构itacmc
927人阅读 评论(0) 收藏 举报
分类:

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, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
这里写图片描述

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output

For each test case display the line “Case k is a tree.” or the line “Case k is not a tree.”, where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input

6 8 5 3 5 2 6 4
5 6 0 0

8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0

3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

和hdu1272有异曲同工之妙,但这个更早出现,而且从无向图变成了有向图,代码也只稍微一改

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 100005
#define Mod 10001
using namespace std;
int f[MAXN],vis[MAXN],edge,v;
bool flag;
int set_find(int a)
{
    if(a!=f[a])
    {
        f[a]=set_find(f[a]);
    }
    return f[a];
}
void set_join(int a,int b)
{
    a=set_find(a);
    b=set_find(b);
    if(a!=b)
    {
        f[b]=a;
        edge++;
    }
    else
        flag=true;
}
int main()
{
    int a,b,cnt=1;
    while(~scanf("%d%d",&a,&b))
    {
        if(a==-1&&b==-1)
            break;
        if(a==0&&b==0)
        {
            printf("Case %d is a tree.\n",cnt++);
            continue;
        }
        memset(vis,0,sizeof(vis));
        edge=0,v=0;
        flag=false;
        for(int i=0;i<MAXN;++i)
            f[i]=i;
        vis[a]=1;
        vis[b]=1;
        set_join(a,b);
        while(~scanf("%d%d",&a,&b))
        {
            if(a==0&&b==0)
                break;
            vis[a]=1;
            vis[b]=1;
            set_join(a,b);
        }
        for(int i=1;i<MAXN;++i)
            if(vis[i])
                v++;
        if(!flag&&edge+1==v)
            printf("Case %d is a tree.\n",cnt++);
        else
            printf("Case %d is not a tree.\n",cnt++);
    }
    return 0;
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

poj1308 判断是否为树

题目:已知有m条边连接顶点,判断其是否为一棵树 思路:树的条件: 要么是空树,要么满足连通无环 1.无环(可通过并查集判断) 2.连通(不能出现森林,即所有输入的点必须只在一个集合里边) 自己wa了好几遍。 总结下来有几个原因: 1.空树特判 2.输入问题,无论是不是树,都必须输入完...
  • yuanba_xs
  • yuanba_xs
  • 2017-02-18 13:54
  • 102

Is it a tree(判断是否是树)

http://bailian.openjudge.cn/practice/1308/ #include #include #include #include #include #include #define N 100 /* 1判断是否是空树 2判断节点数=边数+1 3判断是否有环(并查集) ...
  • jangleise
  • jangleise
  • 2016-09-16 07:43
  • 60

关于easyui tree控件树节点的搜索实现

easyui tree树我们开发人员一般都不会陌生,很多时候我们会有这么一个需求,让子树层级比较多的时候,我们要寻找我们需要的子树就变得有些麻烦,所以我们可以做一个树的搜索功能,废话不多说,我们立刻上代码: <input class="easyui-searchbo...
  • kry1201
  • kry1201
  • 2016-04-13 14:32
  • 1663

python 得到树的深度、判断树是否为平衡树

0. 树中结点的结构: class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None 1. 树的深度: ...
  • u012560212
  • u012560212
  • 2017-05-10 21:24
  • 360

判断一棵树是否为AVL树

NO.12判断一棵树是否为AVL树: 平衡二叉树(AVL树)是满足下面条件的二叉树:要么是一棵空树,要么左右子树都是AVL树,并且左右子树的深度之差的绝对值不大于1。由此可知,要判断一棵树是不是AVL树,只要判断它的左右子树的深度之差。问题落到了如何求一棵树的深度上去了。下面使用递归的方法求一棵树的...
  • baidu_29885977
  • baidu_29885977
  • 2015-10-10 23:23
  • 1273

Merkle Tree算法详解

Merkle Tree是Dynamo中用来同步数据一致性的算法,Merkle Tree是基于数据HASH构建的一个树。它具有以下几个特点: 1、数据结构是一个树,可以是二叉树,也可以是多叉树(本BLOG以二叉树来分析) 2、Merkle Tree的叶子节点的value是数据集合的单元数据或者单元...
  • u012935160
  • u012935160
  • 2014-03-29 11:14
  • 7643

flex4 Tree默认打开指定项并选中指定节点

<![CDATA[ [Bindable] private var zxljd:XML = getZxljd();//动态生成xml作为tree数据源 public function getZxljd():XML{ var treexml:XML = new XML(...
  • luohuayuan69
  • luohuayuan69
  • 2013-05-31 11:58
  • 1433

java 判断两个二叉树是否完全相同

java 判断两个二叉树是否完全相同 包括了创建二叉树,前序遍历输出(递归),比较二叉树是否相同。 package com.yuxin.learn; import java.util.LinkedList; public class Main { private static int[] ...
  • yuxin6866
  • yuxin6866
  • 2016-08-05 21:38
  • 1429

判断tl树中是否有与t2树拓扑结构完全相同的子树

两棵彼此独立的二叉树A和B,请编写一个高效算法,检查A中是否存在一棵子树与B树的拓扑结构完全相同。 给定两棵二叉树的头结点A和B,请返回一个bool值,代表A中是否存在一棵同构于B的子树。 【解析】把树转化成字符串,用KMP即可。 代码如下: /* struct ...
  • FreeeLinux
  • FreeeLinux
  • 2016-12-09 21:50
  • 488

判断一颗树是否为二叉搜索树

10 / \ 5 15 -------- binary tree (1) / \ 6 20
  • njuptsxd
  • njuptsxd
  • 2014-05-22 16:09
  • 1079
    个人资料
    • 访问:380662次
    • 积分:10835
    • 等级:
    • 排名:第1747名
    • 原创:675篇
    • 转载:2篇
    • 译文:0篇
    • 评论:115条
    博主信箱
    博客专栏
    文章分类
    最新评论