判定给定的边序列是否过程一棵树。我用到的判定方法是:第一步:判定  边数是否等于顶点数-1  第二:判定是否只有一个根节点  。当然还要考虑是否为空树的情况。

但是代码交上去,好几遍都是Runtime Error。。。看了一下discuss,并没有觉得自己的程序有错误。但是别人类似的代码却能过掉。。。说明自己的代码写的还是有很大的不足啊。。。

先附上自己的代码,是在不知道有什么错误。看discuss,好像有的代码数组只开到20就a掉了。

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string"
#include"string.h"
#include"cmath"
#include"queue"
#include"stack"
#include"vector"
#include"ctype.h"
using namespace std;
const int mx=;
int fa[mx];
bool visited[mx];
bool flag1,flag2;
int src,dest,edgenum,vernum,icase=; void Set()
{
for(int i=;i<mx;i++)
fa[i]=i;
} int Find(int x)
{
while(x!=fa[x]) x=fa[x];
return fa[x];
} void Union()
{
int fsrc=Find(src);
int fdest=Find(dest);
fa[fdest]=fsrc;
}
void Init()
{
Set();
memset(visited,false,sizeof(visited));
edgenum=;
vernum=;
flag1=flag2=false;
}
bool judge()//判断是否只有一个根节点
{
int i,j;
for(i=;i<mx;i++)
if(visited[i]) break;
int temp=Find(i);
for(j=i;j<mx;j++)
if(visited[j]&&Find(j)!=temp) return true;
return false;
} int main()
{
// freopen("E:\\in.txt","r",stdin);
Init();Set();
while(scanf("%d%d",&src,&dest))
{
if(src==-||dest==-) break;
if(src==&&dest==)
{
if(edgenum==){printf("Case %d is a tree.\n",icase++);Init();continue;}
if(edgenum!=vernum-) flag1=true;
if(judge()) flag2=true;
if(flag1||flag2)
printf("Case %d is not a tree.\n",icase++);
else
printf("Case %d is a tree.\n",icase++);
Init();Set();
continue;
}
edgenum++;
if(!visited[src]) {visited[src]=true;vernum++;}
if(!visited[dest]) {visited[dest]=true;vernum++;}
Union();
}
return ;
}

下面是我参考别人的代码写的,思路和我基本一样,然而,却还是runtime error。。。有点不爽。

#include"iostream"
#include"stdio.h"
#include"cmath"
#define mx 100005
using namespace std;
struct node
{
bool num;//判断这个数字是否用过
int root;//记录其父亲节点
int indegree;//用来记录该节点的入度
};
node tnode[mx];//定义这棵树的结点数组
int src,dest;//边的起点和终点
int root_num;//根节点的数目
int icase=;//案例个数
bool is_tree;//判断是否为一棵树 int Find(int x)//查找根节点
{
if(tnode[x].root!=x)
return tnode[x].root = Find(tnode[x].root);
return tnode[x].root;
} void Union()//将两个结合合并起来
{
int fsrc=Find(src);
int fdest=Find(dest);
if(fsrc!=fdest)
tnode[fdest].root=fsrc;
else if(src!=dest)
is_tree=false;
} void Init()//初始化函数
{
int i;
is_tree=true;
root_num=;
for(i=;i<mx;i++)
{
tnode[i].num=false;
tnode[i].root=i;
tnode[i].indegree=;
}
} void Output()//输出函数
{
int i;
for(i=;i<mx;i++)
{
if(tnode[i].num&&tnode[i].indegree>)
{is_tree=false;break;}
if(tnode[i].num&&tnode[i].root==i)
root_num++;
}
if(root_num>) is_tree=false;
if(is_tree)
printf("Case %d is a tree.\n",icase++);
else
printf("Case %d is not a tree.\n",icase++);
} void Input()//输入函数
{
while(scanf("%d%d",&src,&dest))
{
if(!is_tree&src!=&&dest!=) continue;
if(src==-||dest==-) return;
if(src==&&dest==)
{
Output();
Init();
continue;
}
if(src==dest) is_tree=false;
tnode[src].num=true;
tnode[dest].num=true;
tnode[dest].indegree++;
Union();
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
Init();
Input();
return ;
}

Runtime Error(ARRAY_BOUNDS_EXCEEDED) // array bounds exceed     数组越界
Runtime Error(DIVIDE_BY_ZERO) //divisor is nil                                   除零
Runtime Error(ACCESS_VIOLATION) //illegal memory access     非法内存读取//是我出现的错误,然而我既没有用到指针,数组也没有越界。。。
Runtime Error(STACK_OVERFLOW) //stack overflow                             系统栈过载

这是discuss里的一个程序,感觉和我写的差不多,居然过掉了。。。我也是醉了。

#include <stdio.h>

const int max_num = +;
typedef struct
{
int num,root,conn;//数据、根、入度
}Node; Node node[max_num]; void init()
{
for(int i = ; i < max_num; i++)
{
node[i].conn = ;//入度初始化为0
node[i].root= i;//根记录为自身
node[i].num=;//标记数字是否被使用过,0:没有被使用过,1:使用过了
}
} int find_root(int a)
{
if(node[a].root!=a)
return node[a].root = find_root(node[a].root);
return node[a].root;
} void union_set(int a,int b)
{
a = find_root(a);
b = find_root(b);
if(a==b)//同一个根,说明是在同一个树下
return;
node[b].root=a;//把b的根赋为a的根,此时a已经是根,num==root
} int main()
{
int n,m;
int i = ;
bool flag=true;//true:是个树,false:不是树
init();
while(scanf("%d%d",&n,&m)!=EOF&&n>=&&m>=)
{
if(!flag&&n!=&&n!=)continue;//已经确定不是树了,就继续循环
if(n==&&m==)
{
int root_num=;
for(int j = ; j < max_num;j++)
{
//判断是否为森林,如果,root_num用来记录根的数目
if(node[j].num && find_root(j)==j)
root_num++;
if(node[j].conn>)//如果出现某个节点的入度超过1,不是树
{
flag = false;
break;
}
}
if(root_num>)//连通分支大于1,是森林不是树
flag=false;
if(flag)
printf("Case %d is a tree.\n",i++);
else printf("Case %d is not a tree.\n",i++);
flag = true;
init();
continue;
}
if(m!=n&&find_root(n)==find_root(m))
flag = false;
else
{
//将m,n,记录为节点
node[m].num = ;
node[n].num = ;
node[m].conn++;//入度增加一
union_set(n,m);
}
}
return ;
}

hdu Is It A Tree?的更多相关文章

  1. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  3. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  4. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  5. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. hdu 5274 Dylans loves tree

    Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDU 6161.Big binary tree 二叉树

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...

  9. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. java中的负数的问题

    在计算机中是使用二制数中的最高位表示来正负. 二进制的储存中都是用的补码,正数的原码.反码和补码相同,负数的原码是最高位为1,反码最高位不变,其余各位取反,补码为其反码+1(重要!!) 首先得知道最高 ...

  2. HDU 5652 India and China Origins(经典并查集)

    特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...

  3. python限定类属性的类属性:__slots__

    __slots__ 由于Python是动态语言,任何实例在运行期都可以动态地添加属性. 如果要限制添加的属性,例如,Student类只允许添加 name.gender和score 这3个属性,就可以利 ...

  4. json入门(二)

    背景 之前最早的时候,也见过类似于这样的字符串: {"list":[           {"ArticleId":7392749,"BlogId&q ...

  5. 性能优化之Java(Android)代码优化

    最新最准确内容建议直接访问原文:性能优化之Java(Android)代码优化 本文为Android性能优化的第三篇——Java(Android)代码优化.主要介绍Java代码中性能优化方式及网络优化, ...

  6. Android ListView滑动过程中图片显示重复错乱闪烁问题解决

    最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...

  7. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  8. jdbc 各驱动写法

    1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...

  9. 循环遍历泛型集合List绑定到table

    <%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false&quo ...

  10. TCP Wrapper 特殊使用

    更多,更好内容请参见: http://www.ibm.com/developerworks/cn/aix/library/au-tcpwrapper/ 一. 用处和用法 没有符合hosts.allow ...