POJ 应该是判断是否为简单无环连通图,用并查集直接秒杀即可,而HOJ的是有向树,还需判断所有点的入度必需小于2,用一个类似hash【】数组判断一下即可,

 ////判断树之一:入度<=1;三:点数-1==边数。用一个集合即可。二:无环,n次均为有效加入

#include<iostream> //判断是否是树  0MS
#include<cstdio>
#include<set>
using namespace std;
int fa[100001];
int mark[100001]; //入度不能大于1
int father(int x){return x==fa[x]?x:father(fa[x]);}
struct edge
{
int from;
int to;
};
int main()
{
int a,b;
int kcase=1;
for(int i=0;i<=100000;i++)
{
fa[i]=i;
mark[i]=0;
}
int num=0; int n=0; int flag=0;
set<int>se; //判断树之一:点数-1==边数。用一个集合即可。
while(~scanf("%d%d",&a,&b)&&a>=0&&b>=0)
{
if(a==0&&b==0)
{ if(n==0){printf("Case %d is a tree.\n",kcase);}
else
{
////判断树之一:入度<=1;三:点数-1==边数。用一个集合即可。二:无环,n次均为有效加入
if(flag==0&&n==num&&n==se.size()-1){printf("Case %d is a tree.\n",kcase);}
else{printf("Case %d is not a tree.\n",kcase);}
}
n=0;num=0; //初始化。
kcase++;
se.clear();
flag=0;
for(int i=1;i<=100000;i++)
{
fa[i]=i;
mark[i]=0;
}
}
else
{
edge temp;temp.from=a;temp.to=b;n++;
se.insert(a);se.insert(b);
mark[b]++; //入度++
if(mark[b]>1){flag=1;}
int xx=father(temp.from);int yy=father(temp.to);
if(xx!=yy)
{
fa[xx]=yy;
num++;
}
}
}
return 0;
}

poj1308+HOJ1325,判断是否为树的更多相关文章

  1. [剑指Offer]判断一棵树为平衡二叉树(递归)

    题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...

  2. HDU-1232 畅通工程 (并查集、判断图中树的棵数)

    Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相 ...

  3. 判断一棵树是否为二叉搜索树(二叉排序树) python

    输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...

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

    http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...

  5. 判断是否是树(Is It A Tree?)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  6. E - Is It A Tree? 并查集判断是否为树

    题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...

  7. 15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)

    Level:   Easy 题目描述: Given two non-empty binary trees s and t, check whether tree t has exactly the s ...

  8. [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树

    将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...

  9. LeetCode——Same Tree(判断两棵树是否相同)

    问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

随机推荐

  1. 富通天下(W 笔试)

    纸质算法题目 1.给你一个字符串,找出其中第一个只出现过一次的字符及其位置 正解:一层for循环,循环按序取出字符串中的单个字符,循环体内部使用String类的indexOf(),从当前字符下标往后搜 ...

  2. Format a Hard Drive in Csharp

    Article Author(s): Audric Thevenet All Rights Reserved. Here's how to format hard drives, floppies, ...

  3. css 给div 添加滚动条样式hover 效果

             css .nui-scroll { margin-left: 100px; border: 1px solid #000; width: 200px; height: 100px; ...

  4. int (*a)[10]和int *a[10]的区别

    有点晚了,放个链接,睡觉. https://stackoverflow.com/questions/13910749/difference-between-ptr10-and-ptr10

  5. 6 SQL 函数、谓词、CASE表达式

    6 函数.谓词.CASE表达式 6-1 各种各样的函数 /* 所谓函数,就是输入某一值得到相应输出结果的功能.输入值称为参数(parameter),输出值称为返回值. 函数大致可以分为以下几种 : 算 ...

  6. 大页(Huge Page)简单介绍

    x86(包括x86-32和x86-64)架构的CPU默认使用4KB大小的内存页面(getconf PAGESIZE),但是它们也支持较大的内存页,如x86-64系统就支持2MB大小的大页(huge p ...

  7. python:零散记录

    1.rstrip()删除末尾指定字符串 例如:A = '1,2,3,4,5,' B = A.rstrip(',') B = '1,2,3,4,5' 2.isdigit()方法 Python isdig ...

  8. rspec测试(使用guard自动测试和spork加速测试)配置

    Gemfile文件添加rspec.guard和spork,之后执行bundle install命令 gem 'rb-readline' group :development, :test do # C ...

  9. ruby on rails全局布局,局部视图,局部布局

    参考链接:http://guides.ruby-china.org/layouts_and_rendering.html#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E6 ...

  10. h5 页面 禁止网页缩放

    //禁用双指缩放: document.documentElement.addEventListener('touchstart', function (event) { if (event.touch ...