POJ-1308-Is It A Tree?
来源:互联网 发布:黑马就业班java视频 编辑:程序博客网 时间:2024/05/21 07:50
这个题要求你判断所给的点集是否是一棵树,不算难吧,用并查集可以做,注意细节即可,0 0空树也要算树,
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=10011;int p[maxn];bool vis[maxn];int find(int x){ if(p[x]==-1)return x; return p[x]=find(p[x]);}void Union(int x,int y){ p[min(x,y)]=max(x,y);}int main(){ int a,b,cas=1; while(scanf("%d%d",&a,&b)!=EOF) {if(a==-1&&b==-1) break;if(a==0&&b==0){ printf("Case %d is a tree.\n",cas++); continue;}memset(p,-1,sizeof(p));memset(vis,0,sizeof(vis));int n=0,ans=0;n=max(a,max(n,b));int x=find(a);int y=find(b);vis[a]=vis[b]=1;if(x==y) ans++;else Union(x,y);while(scanf("%d%d",&a,&b)&&(a+b)){ n=max(a,max(n,b)); vis[a]=vis[b]=1; x=find(a); y=find(b); if(x==y)ans++; elseUnion(x,y);}for(int i=1;i<=n;i++) if(vis[i]&&p[i]==-1)ans++;if(ans==1) printf("Case %d is a tree.\n",cas++);else printf("Case %d is not a tree.\n",cas++); } return 0;}
- 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?
- 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
- 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?
- Android下app生成coredump方法
- Linux makefile 教程
- CFI
- poj 3624 DP
- DWR框架配置
- POJ-1308-Is It A Tree?
- 中位数和顺序统计
- Redmine 项目管理软件的下载 、安装
- #pragma once
- Hadoop操作HDFS命令
- 转载VS2008 编译出来的exe文件,运行遇到“应用程序配置不正确,程序无法启动”的问题
- java操作处理Excel表格数据
- 为什么移动Web应用程序很慢(译)
- eclipse搭建Log4J日志环境