HDU 1325 Is It A Tree?(并查集)
来源:互联网 发布:淘宝复制拼多多专家 编辑:程序博客网 时间:2024/04/29 02:45
题意:给你一些有向边,问是否能构成一棵树。。
思路:1、把树的边当成是无向边,则所有的点属于同一个集合,使用并查集
2、所有点至多只能有一个入度。
3、根节点只能有一个
4、特判0,0,
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;const int N = 1009;int fa[N] ;bool visit[N];int finfa(int k){ if(k==fa[k]) return k; return fa[k] = finfa(fa[k]);}void un(int a,int b){ fa[finfa(a)] = fa[finfa(b)];}int in[N],root;int main(){ freopen("in.txt","r",stdin); int a,b,T = 1; while(scanf("%d%d",&a,&b)&&(a>=0||b>=0)) { bool fig = false; if(a==0&&b==0) { printf("Case %d is a tree.\n",T++); continue; } memset(visit,false,sizeof(visit)); memset(in,0,sizeof(in));root = 0 ; for(int i=0;i<N;i++) fa[i] = i; un(a,b);visit[a] = visit[b] = true; in[b]++; while(~scanf("%d%d",&a,&b)&&(a+b)) { un(a,b); visit[a] = visit[b] = true; in[b]++; // cout<<in[b]<<" LLL"<<endl; } int rt = -1; for(int i=0;i<N;i++) if(visit[i]) { //cout<<i<<endl; if(!in[i]) root++; if(in[i]>1) fig = true; if(rt==-1) rt = finfa(i); else if(rt!=finfa(i)) fig = true; } if(root!=1||fig) { printf("Case %d is not a tree.\n",T++); } else { printf("Case %d is a tree.\n",T++); } } 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 Problem 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? (并查集)
- Is It A Tree? HDU 1325 并查集
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- 【并查集】Is it a tree?
- xcode 编译器禁用 ARC (Automatic Referencing Counting)
- Xcode 4.2 中的Automatic Reference Counting (ARC)
- OpenGL库在64位机的安装
- Shell笔记第一天 软链接
- java中的File类
- HDU 1325 Is It A Tree?(并查集)
- poj 1125 Stockbroker Grapevine
- ASIHTTPRequest 详解(转载)
- 关于编写日志文件笔记
- SD9027: 各浏览中某些标记通过鼠标触发 Click 事件后不会获得焦点并触发 Focus 事件
- HTML中的标记详解
- Struts与hibernate整合关键代码(课堂案例)
- UINavigationItem如何设置backBarButtonItem
- 惠普Pro 3380MT PC Intel I5-2400 cpu 在BIOS中开启VT