POJ 1308 Is It A Tree?--题解报告
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31092 | Accepted: 10549 |
Description
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
Output
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0 8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0 3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree. 题意很简单,输入一组数据表示存在父子节点关系的点的编号,判断是否为树 方法一:运用并查集,将有连接关系的点放入同一集合内,判断是否出现环和多个根。如果没有则为树。具体见代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
struct edge
{
int s,t;
}E[];
int par[],Rank[];
int k=,n=,ne=,num=;
bool used[];//used用于记录1到出现的最大数字之间的数字是否使用过
void init(){
memset(Rank,,sizeof(Rank));
for(int i=;i<=;i++)
par[i]=i;
} int Find(int i){
if(par[i]==i)return i;
else return Find(par[i]);
}
bool same(int x,int y){
return Find(x)==Find(y);
} void unite(int x,int y){
if(same(x,y))return;
else{
x=Find(x);
y=Find(y);
if(Rank[x]<Rank[y])
par[x]=y;
else{
par[y]=x;
if(Rank[x]==Rank[y]) Rank[x]++;
}
}
}
void tree(){
init();
bool flag=true;
int now=-;
for(int i=;i<=n;i++){
if(!same(E[i].s,E[i].t)){//判断是否首尾相连成环
unite(E[i].s,E[i].t);
}
else{
flag=false;
break;
}
} for(int i=;i<=ne;i++){
if(used[i]==true&&par[i]==i){//判断根的数目,如果多于1则不是树
num++;
if(num==){
flag=false;
break;
}
}
}
if(flag)printf("Case %d is a tree.\n",++k);
else printf("Case %d is not a tree.\n",++k);
} int main(){
int x,y;
while(scanf("%d%d",&x,&y)){
if(x==-&&y==-) break;
else if(x!=&&y!=){
++n;
E[n].s=y;
E[n].t=x;
ne=max(x,max(ne,y));
used[x]=true;//因为题目没有给出数目采用这种方法判断需要的循环次数
used[y]=true;
}
else{
tree();
memset(used,,sizeof(used));
init();
n=;
ne=;
num=;
}
}
return ;
}
方法二:利用树的性质:(1)点数=边数+1,这个性质可以去掉多根的情况。(2)一个点最多有一个父节点,这个性质可以去掉成环的情况,具体代码如下:
#include<cstdio>
#include<cstring> using namespace std; int x,y,a[]={},m,n,flag;
int main()
{
int k=;
while(scanf("%d%d",&x,&y))
{
if(x==-&&y==-) break;
m++;//输入一组边数+1
if(a[y]==){flag=;}//如果y已经作为过子节点,再一次做子节点,说明出现两个父节点(也可能是重复的边,也不是树),不是树
if(!a[x]&&x){a[x]=;n++;}//1为父节点,2为子节点标记各个点,点数+1
if(!a[y]&&y){a[y]=;n++;}
if(x==&&y==)
{
if(!flag&&((n==m)||(n==&&m==))) printf("Case %d is a tree.\n",k++);//由于0 0也算入边内,因此m为边数+1,m==n;需特判空树
else printf("Case %d is not a tree.\n",k++);
m=,n=,flag=;
memset(a,,sizeof(a));
}
}
return ;
}
方法二由@刘靖尧 金鱼同学提供POJ 1308 Is It A Tree?--题解报告的更多相关文章
- POJ 1308 Is It A Tree? 解题报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32052 Accepted: 10876 D ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...
- hdu 1325 && poj 1308 Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- HDU ACM 1325 / POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
- 并查集判树 poj 1308
例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...
随机推荐
- CodeForces 622C Not Equal on a Segment
预处理p[i],p[i]表示:[p[i],i]这段闭区间上所有数字都是a[i] 询问的时候,如果xi==a[ri]并且p[ri]<=li,一定无解 剩下的情况都是有解的,如果xi!=a[ri], ...
- 在Windows环境下设置terminal下调试adb
当我们想要查看某些程序运行的结果的时候.可能需要打开adb,输入相应的命令,在Windows环境下,需要配置环境变量. 当我们直接在Windows环境下输入adb shell,会提示adb是不内部命令 ...
- Android手势识别总结
一:首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouc ...
- iOS开发系统版本适配(未完待续。。。)
1.iOS9引入了新特性App Transport Security (ATS).新特性要求App内访问的网络必须使用HTTPS协议:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用T ...
- iOS开发者的福利 — — iOS9+Xcode7免越狱免证书直接调试
苹果发布Xcode7后, 开放了普通的AppleID也能真机调试( 非$99 或 $299, 只要能上AppStore下载应用的AppleID就行),下面教你具体做法,很简单的. 1.运行Xcode, ...
- 【转】成为Linux内核高手的四个方法
我曾经问别人如何开始内核编程的学习,他们基本上都说:①如果你不需要了解内核是如何为你工作的,你为何要尝试呢?②你应该订阅Linux内核邮件列表,然后努力去理解.③如果你不去编写针对Linux内核的代码 ...
- leetcode--007 word break I
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+4AAAC5CAIAAAA55fI7AAAZa0lEQVR4nO3dPW7bQIMG4L2MT6B7+A
- 用74HC165读8个按键状态(转)
源:用74HC165读8个按键状态 //-------------------------------------------------------------------------- //来源: ...
- jsp分页的不同实现方法
一.代码实现分页 定义四个分页变量 pageNow 表示第几页 该变量是由用户来决定的,因此是变化的 pageCount 表示总共有多少页,该变量是计算出来的, ---考虑算法 pag ...
- Mysql 多列形成主键(复合主键 )
什么是数据表的复合主键 所谓的复合主键 就是指你表的主键含有一个以上的字段组成 比如 create table test ( name varchar(19), id number, ...