判定给定的边序列是否过程一棵树。我用到的判定方法是:第一步:判定  边数是否等于顶点数-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. CentOS7安装PHP简易步骤

    安装前准备 yum update yum install -y vim yum install -y wget yum install -y bzip2 yum install -y gcc gcc+ ...

  2. 【转载】C++中public,protected,private访问

    第一:private, public, protected 访问标号的访问范围. 假如我们约定: 类内部-----指的是当前类类型的定义中,以及其成员函数的声明和定义中: 类外部-----指的是不在当 ...

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

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

  4. JavaScript 入门 (1)

    一. javascript的调用 JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html> <hea ...

  5. canvas之2D上下文

    1.填充和描边 (1)fillStyle (2)strokeStyle 2.绘制矩形 (1)fillRect() (2)strokeRect() (3)clearRect()   :清除画布上的矩形区 ...

  6. 模数转换(A/D)和数模转换(D/A) 图示

    (从书中截图) 在时间域和频率域中示意图: 1.A/D转换 2.D/A转换

  7. python 线程之 threading(一)

    threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能. 使用定制类的方式继承 threading.Threa ...

  8. js小例子(简单模糊匹配输入信息)

    该例子实现的是用户输入信息或者字母时可以搜索出来,鼠标点击选择 <!DOCTYPE html> <html> <style> p{ width:200px; hei ...

  9. BZOJ总览

    1040: [ZJOI2008]骑士 树上加了一条边 断边再树形DP 断边调了好久 要了解题目性质 1045: [HAOI2008] 糖果传递 中位数水题 内含数学方程 变量搞一搞 bzoj1053: ...

  10. Java NIO之通道Channel

    channel与流的区别: 流基于字节,且读写为单向的. 通道基于快Buffer,可以异步读写.除了FileChannel之外都是双向的. channel的主要实现: FileChannel Data ...