hdu 1325
。,。 还是待整理 #include <stdio.h>
const int max_num =+;
typedef struct
{
int num,root,conn;//数据、根、入度
}Node; Node node[max_num]; void init()
{
for(int i =; i < max_num; i++)
{
node[i].conn =;//入度初始化为0
node[i].root= i;//根记录为自身
node[i].num=;//标记数字是否被使用过,0:没有被使用过,1:使用过了
}
} int find_root(int a)
{
if(node[a].root!=a)
return node[a].root = find_root(node[a].root);
return node[a].root;
} void union_set(int a,int b)
{
a = find_root(a);
b = find_root(b);
if(a==b)//同一个根,说明是在同一个树下
return;
node[b].root=a;//把b的根赋为a的根,此时a已经是根,num==root
} int main()
{
int n,m;
int i =;
bool flag=true;//true:是个树,false:不是树
init();
while(scanf("%d%d",&n,&m)!=EOF&&n>=&&m>=)
{
if(!flag&&n!=&&n!=)continue;//已经确定不是树了,就继续循环
if(n==&&m==)
{
int root_num=;
for(int j =; j < max_num;j++)
{
//判断是否为森林,如果,root_num用来记录根的数目
if(node[j].num && find_root(j)==j)
root_num++;
if(node[j].conn>)//如果出现某个节点的入度超过1,不是树
{
flag = false;
break;
}
}
if(root_num>)//连通分支大于1,是森林不是树
flag=false;
if(flag)
printf("Case %d is a tree.\n",i++);
else printf("Case %d is not a tree.\n",i++);
flag = true;
init();
continue;
}
if(m!=n&&find_root(n)==find_root(m))
flag = false;
else
{
//将m,n,记录为节点
node[m].num =;
node[n].num =;
node[m].conn++;//入度增加一
union_set(n,m);
}
}
return;
}
hdu 1325的更多相关文章
- HDU 1325 有根树的判断
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- (并查集)POJ 1308 & HDU 1325
一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- hdu 1325 && 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 ...
- HDU 1325(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 1325 Is It A Tree?
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 【并查集】HDU 1325 Is It A Tree?
推断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
随机推荐
- Hibernate 基本使用
Hibernate框架概述 一.什么是框架 软件的一个半成品,已经帮你完成了部分功能. 把一些不确定的东西,按照框架要求,达到相应的功能 Hibernate是JavaEE技术三层架构所用到的技术 二. ...
- 研途APP——项目需求分析
研途APP--项目需求分析 1.作业描述 项目 内容 课程 软件工程实践 作业链接 团队作业第二次-需求规格说明书 团队名称 8rookies 作业目标 作业正文 研途APP--项目需求分析 其他参考 ...
- html中第一行是什么意思
html中第一行是什么意思 一.总结 一句话总结: 告诉浏览器,让浏览器得知自己要处理的内容时html 二.html中第一行是什么意思 转自或参考:HTML文件第一行是什么东东_百度知道https:/ ...
- springboot之freemarker 和thymeleaf模板web开发
Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...
- python小白之np功能快速查
np一些用法 np.a np.array([1,2,3],dtype=int) #建立一个一维数组, np.array([[1,2,3],[2,3,4]]) #建立一个二维数组. np.arang ...
- osg 自定义图元
osg::ref_ptr<osg::Geode> OSG_Qt_::createBox() { osg::ref_ptr<osg::Geode> osg_geode = new ...
- windows下打开binlog
上篇我们介绍了binlog(参见mysql的binlog),配置文件用的是linux下的my.cnf,那么在windows下如何打开binlog呢?道理是相同的,配置文件是不一样的.在windows下 ...
- Java 5-11新特性的整理(转)
Java 5-11新特性的整理(转) 作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892博客地址:https://it18monkey.github ...
- python 中 dict 的另一种用法
python 中 dic另一种用法 描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 dict={} dict.get(k1,Non ...
- 使用 Sublime + PlantUML 高效地画图
转自 http://www.jianshu.com/p/e92a52770832