hdu 1325 Is It A Tree?
来源:互联网 发布:java获取系统当前年份 编辑:程序博客网 时间:2024/05/21 09:12
题目分析:给定很多边,判断是否为树,,,,
1.有且仅有一个根节点,每个节点的入度不大于1(开始没考虑到这点,一直wa)。
2.无圈
2.0 0 是棵空树
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct node{ int v; bool flag; int indegree;}parent[100100];int find(int x){int i;for(i=x;i!=parent[i].v;i=parent[i].v);while(x!=i){int temp=parent[x].v;parent[x].v=i; x=temp;}return i;}int main(){int x,y,num=0,c=0;bool flag=true;for(int i=1;i<=100000;i++){parent[i].v=i;parent[i].flag=false;parent[i].indegree=0;}while(scanf("%d %d",&x,&y)!=EOF){if(x+y==-2)break;if(x==0&&y==0){c++;for(int i=1;i<=100000;i++){if(parent[i].flag!=0 && parent[i].v==i) num++;}if(flag==true && (num==1||num==0) )printf("Case %d is a tree.\n",c);elseprintf("Case %d is not a tree.\n",c); //printf("%d***\n",flag);for(int i=1;i<=100000;i++){parent[i].v=i;parent[i].flag=false;parent[i].indegree=0;}num=0;flag=true;}else{parent[x].flag=true;parent[y].flag=true;if(parent[y].indegree>=1)flag=false;elseparent[y].indegree=1;int fx=find(x);int fy=find(y);if(fx==fy)flag=false;else if(fx<fy)parent[fy].v=fx;elseparent[fx].v=fy;}}//system("pause");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 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 &&poj1308 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?
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
- (转自java中文网)jdbc连接数据库大全 留着以后用
- 企业IT架构的规划思考
- SQL - UNION 及 UNION ALL 语句
- js日期控件整理
- hdu 1325 Is It A Tree?
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore
- 编写高效的Android代码
- 用js操作table、tr、td 「字体样式及TD背景图片」
- FastDelegate
- request中文乱码解决方法
- linux下解压命令大全
- Linux 函数--fstat/stat/lstat系统调用
- 设计模式_策略模式(Strategy Pattern)