hdu Is It A Tree?
判定给定的边序列是否过程一棵树。我用到的判定方法是:第一步:判定 边数是否等于顶点数-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?的更多相关文章
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Hdu 5416 CRB and Tree (bfs)
题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 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 ...
- hdu 5274 Dylans loves tree
Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 6161.Big binary tree 二叉树
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- 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 ...
随机推荐
- Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)
题目链接:http://codeforces.com/problemset/problem/164/A 思路:用vector分别保留原图和发图,然后分别从val值为1的点正向遍历,va值为2的点反向遍 ...
- WPD:Page Download Time Breakdown选项详解
WPD:Page Download Time Breakdown选项详解 “页面下载时间细分”图显示每个页面组件下载时间的细分,可以根据它确定在网页下载期间事务响应时间缓慢是由网络错误引起还是由服务器 ...
- Java Android 注解(Annotation) 及几个常用开源项目注解原理简析
不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...
- poj1745 dp
题目链接:http://poj.org/problem?id=1745 类似的题目之前写过一个差不多的(链接:http://www.cnblogs.com/a-clown/p/5982611.html ...
- HTTP基础06--网络安全
HTTP 的缺点 通信使用明文可能会被窃听 HTTP 报文使用明文(指未经过加密的报文)方式发送. 通信的加密 用 SSL 建立安全通信线路之后,就可以在这条线路上进行 HTTP 通信了.与 SSL ...
- UE4在C++中使用OnComponentBeginOverlap之类的时间
首先说明一下,官方文档是错的,在4.10版本下,绑定函数在角色类的构造函数中不起作用.2016.2.12 这里角色类为例 首先在头文件中添加: UFUNCTION() void OnOverlapBe ...
- BZOJ4471 : 随机数生成器Ⅱ
\[\begin{eqnarray*}x_i&=&x_{i-1}+x_{i-2}\\x_i^2&=&x_{i-2}^2+x_{i-1}^2+2x_{i-2}x_{i-1 ...
- BZOJ3553 : [Shoi2014]三叉神经树
设val[i]为i连出去的树突中输出值为0的个数 如果val[x]<=1,输出值为1,否则输出值为0 修改x就相当于val[f[i]]++或者val[f[i]]-- 用Link-cut Tree ...
- [转]使用EasyRsa3为OpenVPN生成密码
1. 下载Easy RSA3 下载完并解压后,拷贝一份到/etc/openvpn和/home/client下 #.3版本需要独立下载个easy-rsa,该包用来制作ca证书,服务端证书,客户端证书 w ...
- Spring Assert(方法入参检测工具类-断言)
Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方法入参进行合 法性检查,如果入参不符合要求,方法将通过抛出异 ...