HDU 1325&&POJ 1308 Is It A Tree? (并差集)
来源:互联网 发布:松下自动焊编程 编辑:程序博客网 时间:2024/05/29 07:24
就是判断给出的是不是一棵树....
比起小希的迷宫那个题目,多出来一个入度最多为1...大于1就不是树了...
HDU的版本是输入两个负数结束而POJ就是和样例一样输入俩-1 结束..贴出来杭电的版本:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<iostream>using namespace std;int f[200000],m[200000];void inti(){ for(int i=0;i<=100000;i++) f[i]=i;}int find(int x){ if(x!=f[x]) return find(f[x]); return x;}int main(){ int x,y,num[200000]; int cas=1; while(cin>>x>>y,x>=0||y>=0) { inti(); memset(m,0,sizeof(m)); memset(num,0,sizeof(num)); int flag=1; if(x==0&&y==0)//空树也是树 { printf("Case %d is a tree.\n",cas); cas++; continue; } int p=99999999,q=-1; while(x!=0||y!=0) { if(x<p) p=x; if(x>q) q=x; if(y<p) p=y; if(y>q) q=y; m[x]=1,m[y]=1; num[y]++; int dx=find(x); int dy=find(y); if(dx==dy) flag=0; else f[dy]=dx; cin>>x>>y; } if(flag==0)//有环就不是树 { printf("Case %d is not a tree.\n",cas); cas++; continue; } int ans=0; for(int i=p;i<=q;i++)//多个根就不是树 { if(f[i]==i&&m[i]==1) ans++; } for(int i=p;i<=q;i++)//入度大于1就不是树 { if(num[i]>1&&m[i]==1) ans=0; } if(ans==1) printf("Case %d is a tree.\n",cas); else printf("Case %d is not a tree.\n",cas); cas++; } return 0;}
0 0
- HDU 1325&&POJ 1308 Is It A Tree? (并差集)
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- TOJ 1856 POJ 1308 HDU 1325 Is It A Tree? / 并查集
- POJ 1308 && HDU 1325 Is It A Tree? (并查集)
- poj 1308 &&HDU 1325 Is It A Tree?(并查集、树的定义)
- HDU 1325 POJ 1308 Is It A Tree? (并查集)
- POJ 1308 && HDU 1325 Is It A Tree?(并查集)
- HDU 1325&POJ 1308 Is it A tree ? [并查集+树判定]
- hdu 1325/poj 1308 Is It A Tree? 并查集
- POJ 1308 && HDU 1325 Is It A Tree?(并查集)
- POJ 1308 HDU 1358 Is It A Tree? 并查集 (好多WA点)
- hdu1325 Is It A Tree?并差集实现
- POJ 1308-Is It A Tree 并查集
- POJ 1308 Is It A Tree? (并查集)
- poj 1308 - Is It A Tree?(并查集)
- POJ-1308 并查集-Is It A Tree?
- POJ 1308 Is It A Tree? (并查集)
- poj 1308 Is It A Tree?(并查集)
- html中嵌入flash
- Android中的wifi 概述
- 安卓工具类-------->打开或关闭软键盘
- php源码断点调试
- Mac添加环境变量的三种方法
- HDU 1325&&POJ 1308 Is It A Tree? (并差集)
- vc++出现warningC4819的处理方法
- busybox 中文支持--记录
- 沃通EV国际认证,用绿色地址栏终结钓鱼网站
- 提取img标签src属性中的图片路径正则 (php)
- 版本控制Git(1)——理解暂存区
- [redis]redis命令汇总(二)
- 使用backbone.js、zepto.js和trigger.io开发HTML5 App
- 【C++ Primer】【学习笔记】【第十章】关联容器之:set类型