Is it a tree(判断是否是树)
2016-09-16 07:43
141 查看
http://bailian.openjudge.cn/practice/1308/
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string> #include<string.h> #include<algorithm> #define N 100 /* 1判断是否是空树 2判断节点数=边数+1 3判断是否有环(并查集) */ using namespace std; struct E{ int x,y; }edge ; int size; int root ; int point ; int cnt; int findroot(int x) { if(root[x]==-1) return x; else { int tmp=findroot(root[x]); root[x]=tmp; return tmp; } } void init() { memset(point,0,sizeof(point)); memset(root,-1,sizeof(root)); size=cnt=0; } bool solve1() {//顶点数量=边数量+1 sort(point,point+cnt); int sum=1;//顶点个数 for(int i=1;i<cnt;i++) { if(point[i]!=point[i-1]) sum++; } if(sum==size+1) return true; else return false; } bool solve2() {//是否有环(并查集) for(int i=0;i<size;i++) { int u=findroot(edge[i].x); int v=findroot(edge[i].y); if(u==v) { return false; } else { root[v]=u; } } return true; } int main() { int a,b; int t=0; init(); while(~scanf("%d%d",&a,&b) && a!=-1 && b!=-1) { if(a==0 && b==0) {//空树 if(size==0) printf("Case %d is a tree.\n",++t); else {//一个测例的结束 if(solve1()&&solve2()) { printf("Case %d is a tree.\n",++t); } else printf("Case %d is not a tree.\n",++t); init(); } } else { edge[size].x=a; edge[size++].y=b; point[cnt++]=a; point[cnt++]=b; } } return 0; }
相关文章推荐
- 判断是否是树(Is It A Tree?)
- PAT 1043. Is It a Binary Search Tree (25)(判断是否是搜索树并构建和后序输出)(待修改)
- hdu 1325 Is It A Tree? 判断是否是树 巨坑
- 并查集之判断图是否成环—小希的迷宫and is it a tree
- poj1308——Is It A Tree?(判断是否为树)
- hdu1325 Is It A Tree? 判断是否为树 并查集
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25) 判断序列是否为BST的先序遍历,递归
- [POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)
- HDU 1325 Is It A Tree? 判断是否为一棵树
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
- POJ 1308 Is It A Tree?(判断是否是一棵树)
- HDU 1325 Is It A Tree?(判断是否为树)
- HDOJ 1325 Is It A Tree?(并查集+入度判断)
- poj 1308 Is It A Tree? 并查集,有向树的判断
- HDU 1325 Is It A Tree? POJ 1308 Is It A Tree? (并查集+入度,判断一个有向图是树)
- HDU 1325 Is It A Tree? ( 有向图判断环
- PAT 1123. Is It a Complete AVL Tree (30) 平衡树构建+ 完全二叉树判断
- (hdu step 5.1.2)Is It A Tree?(判断是不是一棵树)
- N - Is It A Tree?(判断环)
- hdu1325 Is It A Tree?(二叉树的判断)