hdu 1325 Is It A Tree? 判断是否是树 巨坑

来源:互联网 发布:怎么开发java手机游戏 编辑:程序博客网 时间:2024/06/06 18:49

每组数据,给出,一些有向边,从父亲指向儿子结点,每组数据以0 0 结尾,

整个输入以小于0的两个数结尾,判断每组数据是否是一棵树。


这个题目最坑的在于结尾的样例给的是-1 -1 ,结果习惯性的认为是-1 -1结尾,最后莫名其妙的wrong answer。



判定是否是一棵树:


0.空树可以是树。就是输入0 0 的一组也是数。

1.存在一个且仅有一个入度为0的结点。

2.其余结点的入度必须是1。

3.整个图必须是连通的。(用并查集实现,把有向边当作无向边)



下面证明如果不是空树,只要满足1、2、3三点就一定是一棵树。


我们建立一个集合S={},不断将点放入S

首先我们根据1可以找到仅有的一个根结点root,将root放入S,

因为所有点是连通的(根据3),而且root的入度为0,所以必须有从root出发边,假设连接到a1,将a放入S集合,那么因为a1的入度是1(根据2),所以S集合现在只能发出边,不能进入边,因为所有点连通,之后依次加入各点,直到所有点都放入S。


这个实质就是形成树的一个过程。


你可以发现这样形成的树,每个结点只有一个父结点,满足n=m+1,而且绝对无环。


Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19561    Accepted Submission(s): 4385


Problem Description
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.



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
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.
 

Output
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).
 

Sample Input
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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.
 

Source
North Central North America 1997 


/**========================================== *   This is a solution for ACM/ICPC problem * *   @source:hdu 1325 *   @type: *   @author: wust_ysk *   @blog:  http://blog.csdn.net/yskyskyer123 *   @email: 2530094312@qq.com *===========================================*/#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn= 1000000  ;int n,m;int in[maxn+5],out[maxn+5],maxi;bool vis[maxn+5];int pre[maxn+5];int find(int x){    return x==pre[x]?x:pre[x]=find(pre[x]);}void merge(int x,int y){    int rootx=find(x);    int rooty=find(y);    if(rootx==rooty)  return;    pre[rootx]=rooty;}int main(){    int x,y,kase=0;    while(~scanf("%d%d",&x,&y)&&x>=0&&y>=0)    {        maxi=0;n=0,m=0;        if(x==0&&y==0)        {            printf("Case %d is a tree.\n",++kase);            continue;        }        memset(in,0,sizeof in);        memset(out,0,sizeof out);        memset(vis,0,sizeof vis);        out[x]++,in[y]++;        maxi=max(maxi,x);        maxi=max(maxi,y);        if(!vis[x])  pre[x]=x;        if(!vis[y])  pre[y]=y;        vis[x]=1,vis[y]=1;        merge(x,y);        m++;        while(~scanf("%d%d",&x,&y)&&x&&y )        {            out[x]++,in[y]++;            maxi=max(maxi,x);            maxi=max(maxi,y);            if(!vis[x])  pre[x]=x;            if(!vis[y])  pre[y]=y;            vis[x]=1,vis[y]=1;            merge(x,y);            m++;        }        int hasroot=0;        bool ok_in=1, uni=1;        for(int i=1;i<=maxi;i++)  if(vis[i])        {            n++;            if(in[i]==0)  hasroot++;            else  if(ok_in)   ok_in=in[i]==1;            if(uni)  uni=uni&& (find(i)==find(maxi)  );        }        if(hasroot!=1||!ok_in||!uni)  {  printf("Case %d is not a tree.\n",++kase);continue;        }        printf("Case %d is a tree.\n",++kase);    }   return 0;}








0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 楼道自来水爆了怎么办 工商证没年检怎么办 工行信用卡被锁怎么办 外地卡密码锁了怎么办 营业执照年审过期了怎么办 工商营业执照吊销了怎么办 小规模企业工商年检怎么办 血流变检查偏高怎么办 信誉卡没有邮箱怎么办 税务年报没报怎么办 工商忘记年审了怎么办 营业执照脱审了怎么办 公司年审没有弄怎么办 车检标志丢了怎么办 机动车年检丢了怎么办 汽车保险标志丢了怎么办 车辆年检贴丢失怎么办 车检标贴丢了怎么办 检验标贴丢了怎么办 小车换年检标志怎么办 职称证忘记审验怎么办 联合年报没报怎么办 职称年审过期了怎么办 嘉兴驾模预约怎么办 电大考试去不了怎么办 负债高怎么办房贷 宁波鄞州银行卡怎么办 硕士学位实验数据不好怎么办 小区外过车很吵怎么办 建筑物初级消防证书怎么办 毕业答辩去不了怎么办 卡波西水痘样疹怎么办 58企业认证失败怎么办 炸鸡店没生意怎么办 你好漂亮重名了怎么办 医保卡被冒用怎么办 理财钱被骗走怎么办 个体户怎么办三方协议 社保户籍错了怎么办 身份证号码变更后驾驶证怎么办 公司倒闭欠工资怎么办