Is it a tree?
来源:互联网 发布:福州淘宝托管 编辑:程序博客网 时间:2024/05/22 06:20
题目:
http://poj.org/problem?id=1308
大意:
给你一堆树枝,让你判断最后能不能构成一棵树。
思路:根据树的定义判断即可
树:
1.入度只能为1或者0(根结点)
2.根结点只有一个
3.出度可以有多个
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define MAXN 10010#pragma comment(linker, "/STACK:102400000,102400000")int pre[10010], son[10010], visit[10010];int unionsearch(int root) //查找根结点+路径压缩(递归容易破栈){int root2;root2 = root;while(root != pre[root])root = pre[root];while(root2 != root){int temp = pre[root2];pre[root2] = root;root2 = temp;}return root;}bool cmp(){int root = 0;for(int i = 0; i < MAXN; ++i) //累计根结点的子节点个数if(visit[i])++son[unionsearch(i)];for(int i = 0; i < MAXN; ++i) //累计根结点的个数if(son[i] > 1)root++;if(root > 1) //判断为树或者为森林return false;else return true;}int main(){//freopen("Input.txt", "r", stdin);int start, end, x, y, flag, T = 0;while(true){flag = 0;for(int i = 0; i < MAXN; ++i)pre[i] = i;memset(son, 0, sizeof(son));memset(visit, 0, sizeof(visit));while(scanf("%d%d", &start, &end) && start && end){if(start == -1 && end == -1) return 0;x = unionsearch(start);y = unionsearch(end);visit[start] = 1; // 树枝标记,忘了。调试了N久。visit[end] = 1;if(x == y || end != y) //x == y为环,end == y,入度为2flag = 1;elsepre[end] = start;}if(flag)printf("Case %d is not a tree.\n", ++T);else{if(cmp())printf("Case %d is a tree.\n", ++T);elseprintf("Case %d is not a tree.\n", ++T);}}return 0;}
改进后的程序(2011/10/25)
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define MAXN 10010int pre[10010], visit[10010];int unionsearch(int root) //查找根结点+路径压缩{int root2;root2 = root;while(root != pre[root])root = pre[root];while(root2 != root){int temp = pre[root2];pre[root2] = root;root2 = temp;}return root;}bool cmp(){int root = 0;for(int i = 0; i < MAXN; ++i) //累计根结点的子节点个数if(visit[i] && pre[i] == i)root++;if(root > 1) //判断为树或者为森林return false;else return true;}int main(){//freopen("Input.txt", "r", stdin);int start, end, x, y, flag, T = 0;while(true){flag = 0;for(int i = 0; i < MAXN; ++i)pre[i] = i;memset(visit, 0, sizeof(visit));while(scanf("%d%d", &start, &end) && start && end){if(start == -1 && end == -1) return 0;x = unionsearch(start);y = unionsearch(end);visit[start] = 1;visit[end] = 1;if(x == y || end != y)flag = 1;elsepre[end] = start;}if(flag)printf("Case %d is not a tree.\n", ++T);else{if(cmp())printf("Case %d is a tree.\n", ++T);elseprintf("Case %d is not a tree.\n", ++T);}}return 0;}
- Is It A Tree?
- Is it a tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- IS IT A tree
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- is it a tree
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- 匈牙利算法
- AS3的正则表达式 去除字符串首尾空格
- osip和exosip在windows下的编译静态库
- 解决hibernate中session的问题
- IPhone 开发中的错误
- Is it a tree?
- String, StringBuffer, StringBuilder
- 超实用的Linux/Unix快捷键大汇总(开发、管理)
- VC实现线程池
- ubuntu 11.04下搭建android开发/编译环境 安装必要工作用软件
- JDOM、Dom4j操作XML
- SQL Server 开发指南(经典教程)
- 基于VB6的餐卡系统二次开发
- eclipse 初探1-spring