nyoj 129 poj 1308 Is a tree
来源:互联网 发布:中法翻译软件 编辑:程序博客网 时间:2024/05/17 02:26
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
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.
- 输入
- The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1. - 输出
- For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
- 样例输入
6 8 5 3 5 2 6 4 5 6 0 08 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 03 8 6 8 6 4 5 3 5 6 5 2 0 0-1 -1
- 样例输出
- Case 1 is a tree.
- Case 2 is a tree.
- Case 3 is not a tree.
- 此题思路比较简单,判断是否为一颗根据题意定义的树,题目是英文,理解有点出入,遗漏判断图联通了.
- 1.用并查集确定图中所有顶点是否在集合中.同时也可以判断是否边连通.
- 2.判断是否存在入度为1以上的点,若存在,则不为数.
accode:
#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int father[10006];
int num[10006];
int b[10006];
vector<int> a[10006];
void Init()
{
memset(num,0,sizeof(num));
for(int i=0;i<10006;++i)
father[i]=i;
}
int findfather(int i)
{
for(i;i!=father[i];i=father[i]) ;
return father[i];
}
void Union(int a,int b)
{
if(findfather(a)==findfather(b)) return;
else
{
int x=findfather(a);
int y=findfather(b);
if(num[x]>num[y])
{
father[y]=x;
num[x]+=num[y];
}
else
{
father[x]=y;
num[y]+=num[x];
}
}
}
int main()
{
int row,col,flag,flag2,n,i,j,Count=0;
while(1)
{
flag2=1;
flag=1;
n=0;
Init();
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
while(scanf("%d%d",&row,&col))
{
if(row==0&&col==0) break;
if(row==-1&&col==-1) { flag2=0;break;}
b[n++]=col;
a[col].push_back(row);
if(findfather(row)==findfather(col)) flag=0; //判断是否存在边连通
Union(row,col);
}
int fat=findfather(b[0]),temp;
for(i=0;i<n;++i)
{
temp=b[i];
for(j=0;j<a[temp].size();++j)
{
if(findfather(a[temp][j])!=fat)
{
flag=0;
break;
}
if(a[temp].size()>1)
{
flag=0;
break;
}
}
}
if(!flag2) break;
Count++;
if(flag)
cout<<"Case "<<Count<<" is a tree."<<endl;
else
cout<<"Case "<<Count<<" is not a tree."<<endl;
}
}
- nyoj 129 poj 1308 Is a tree
- HDU 1325 || NYOJ 129 || POJ 1308 Is It A Tree?
- POJ 1308 Is It A Tree? && NYOJ 129 (树的判定+并查集)
- POJ 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- poj 1308 Is it a Tree?
- POJ 1308 Is it a tree??
- poj 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- Poj 1308 Is It A tree
- poj 1308 is it a tree
- POJ 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- MFC控件篇之对话框中的消息与数据
- Java String对象的经典问题(new String())
- Ios-多视图
- struts.xml配置json
- myeclipse 项目 红感叹号 黄色感叹号的jar文件
- nyoj 129 poj 1308 Is a tree
- ajax asp.net ashx用法
- IE 下面神奇的Firebug
- 故障定位四法
- Eclipse 安装maven 插件
- Windows 下修改DLL(Exe)的版本号信息
- JavaWEB开发的相关知识
- 判断两个链表是否相交
- HTML5