HDU 1325 Is It a Tree?
来源:互联网 发布:打印工厂软件下载 编辑:程序博客网 时间:2024/05/21 06:51
Is It A Tree?
Total Submission(s): 4099 Accepted Submission(s): 962
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.
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.这道题是一道比较简单的并查集,用于判断给定的节点是否可以构成一棵树。我的思路是按照以下三点,运用排除法进行判断的:(1)、判断两个节点的父节点是否相等,如果相等,则不能构成树;(2)、判断节点的入度是否<=1,如果大于1,则说明不是树;(3)、判断顶点数是否等于边数加1,如果不等,则说明不是树。代码如下:#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;
int father[100005];int ingree[100005];int flag[100005];
int find_father(int x){ int r=x; while(r!=father[r]) r=father[r]; while(x!=r) { int temp=father[x]; father[x]=r; x=temp; } return r;}
void unit(int a,int b){ int r1=find_father(a),r2=find_father(b); if(r1!=r2) father[r1]=r2;}
int main(){ int a,b; int cnt=0; while(1) { scanf("%d%d",&a,&b); for(int i=1;i<=100000;i++) { father[i]=i; ingree[i]=0; flag[i]=0; } bool p=0; int edge=0; cnt++; if(a<0&&b<0) break; if(!a&&!b) { printf("Case %d is a tree.\n",cnt); continue; } if(find_father(a)!=find_father(b)) unit(a,b); else p=1; ingree[b]++; flag[a]=flag[b]=1; int min=a>b?b:a; int max=a>b?a:b; edge++; while(scanf("%d%d",&a,&b),a||b) { if(find_father(a)!=find_father(b)) unit(a,b); else p=1; ingree[b]++; if(ingree[b]>1) p=1; flag[a]=flag[b]=1; if(min>a) min=a; if(min>b) min=b; if(max<a) max=a; if(max<b) max=b; edge++; } if(!p) { int k=0; for(int i=min;i<=max;i++) if(flag[i]) k++; if(k!=edge+1) p=1; } if(p) printf("Case %d is not a tree.\n",cnt); else printf("Case %d is a tree.\n",cnt); } return 0;}
- HDU 1325 Is It a Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 is it a tree?
- HDU-1325-Is It A Tree?
- HDU 1325 Is It A Tree? ---树
- HDU 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is it a tree
- hdu 1325 &&poj1308 Is It A Tree?
- HDU 1325 Is It A Tree?
- HDU 1325 Is It A Tree?
- HDU 1325 Is It A Tree?
- hdu 1325 is it a tree?
- yum安装lamp
- extern的用法
- TCH值为0的block为什么也出现在X$BH中?
- 学习java相关知识的网站
- linux yum安装mysql后要注意的一些初始化问题
- HDU 1325 Is It a Tree?
- Android 中Service的生命周期
- 深度分析数据库的热点块问题
- 关于 v$waitstat 和 x$bh.class 的关系
- Extjs的作用域讲解,,,(转载)
- SQL SERVER 视图的建立
- linux chmod命令参数及用法详解--文件文件夹权限设定命令
- “AndroidManifest.xml 系统找不到指定的文件”解决方案
- 解决war包发布时报:java.lang.ClassCastException:org.apache.catalina.util.DefaultAnnotationProcessor cannot b