HDU1325 &&poj1308 基础并查集
来源:互联网 发布:翻译硕士学校排名知乎 编辑:程序博客网 时间:2024/05/03 23:09
和上一道小希的迷宫差不多,但是在HDU上提交一直WA,POJ过了
HDU的数据太强了吧,强的我感觉数据有问题
题意:输入若干对点,判断是否是一颗树,转化过来也就是是否存在环
点数-边数=1,还要判断每个点的入度都<=1
POJ AC代码
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>const int INF = 1e8;using namespace std;int father[1010];bool vis[1010];int rudu[1010];int findx(int r){ int i = r,j; while(father[r]!=r) { r=father[r]; } while(father[i]!=r) { j = father[i]; father[i] = r; i = j; } return r;}bool Merge(int x,int y){ int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy){ father[fx]=fy; return 1;}elsereturn 0;}void init(){ for(int i=0;i<1010;i++){father[i]=i;vis[i] = 0 ;rudu[i] = 0;}}int main(){int a,b,c = 0;while(scanf("%d%d",&a,&b)!=EOF){ c++; if(a<0&&b<0) break; intflag=1,t=0; if(a==0 && b==0) { printf("Case %d ",c); puts("is a tree."); continue; }init(); intnum = 0;while(1){ if(a==0&&b==0) break; if(flag==1) {if(!vis[a]) {num++; } if(!vis[b]) {num++; rudu[b]++;}if(rudu[b]>1) flag = 0;vis[a]=1; vis[b]=1; if(Merge(a,b)==1) { t++; } else flag = 0; } scanf("%d%d",&a,&b);}printf("Case %d ",c);if(num-t==1 &&flag == 1)puts("is a tree.");elseputs("is not a tree.");}return 0;}
0 0
- HDU1325 &&poj1308 基础并查集
- hdu1325&&poj1308 Is It A Tree? 基础并查集
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
- HDU 1272并查集 开始错的很郁闷 另外HDU1325&&POJ1308
- HDU1325 Is It A Tree? + POJ1308 Is It A Tree?【并查集】
- hdu1325并查集
- HDU1325 并查集
- POJ1308-----并查集基础题
- hdu1856&&poj1308(基础并查集)
- POJ1308-并查集
- Poj1308 并查集
- 并查集POJ1308
- poj1308【并查集】
- poj1308 并查集
- 并查集之POJ1308
- 并查集 poj1308 hd1272
- POJ1308 Is it a tree? 基础并查集
- hdu1325 hdu1272 hdu1232 并查集应用
- 移动视频技术——新增API可手工修正视频方向
- ns3使用doxygen生成离线api文档
- C++STL之变异算法
- PHP json_encode() 函数介绍
- java中instanceof的用法
- HDU1325 &&poj1308 基础并查集
- 【ThinkPHP学习】模板标签
- App推广资料
- iphone刷机---iphone4s ios8降级ios7.1.1教程
- ios开发中Certificate,AppId和Provisioning三者的含义和联系
- python装饰器实现线程同步
- poj 2090 Two-Stacks Solitaire
- 思科路由器的VPN
- Objective-c的内存管理MRC与ARC