IS IT A tree

本文介绍了一种用于判断给定数据集是否形成树形结构的方法,详细阐述了算法流程,并通过实例验证了其实用性和正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
IS IT  A tree
注意: 本代码只能针对是一个连通分量的图判断是否为树 !!!!!!! 
*/

#include<stdio.h>
#include<string.h>
#define M 50
int Locate(int data[], int i, int x) ; //函数声明 

int main()
{
    freopen("tree.in", "r", stdin);
//  freopen("estdout.pc2", "w", stdout);
    int data[M]={0}, prior[M]={0};  //data存数据,prior[i]统计data[i]前驱个数 
    int T=0, a,b, i=0, k, count0=0, yes;  //T统计输入数据组数, count0统计前驱为0的结点个数。 

    while(scanf("%d %d", &a,&b)==2)
    {   
        if(a==-1&&b==-1) break;  //结束全部输入 

        if(a!=0&&b!=0)   //如果不是某组数据的结束标志,则存数据、统计前驱 
        {   
            if( Locate(data,i,a)==-1 )  //若a没有存过,则存储 
                data[i++]=a; 
            k=Locate(data,i,b);   //在data数组中找b. 其位置下标给k 
            if(k==-1)   {       //k为-1,说明b没有存过,则将其存入 
                data[i++]=b;    
                prior[i-1]++;   //让b的前驱个数增加 
            }
            else    prior[k]++;  //b已经存在data[k]中,直接让b的前驱个数增加 
        }

        else {    //一组信息存储完毕, 共i个结点,各结点前驱个数已经统计完毕 ,
                     //分析各结点前驱个数    
            T++;   //统计组数 
            for(k=0; k<i; k++)
            {   
                //printf("%d, p[%d]=%d\n", data[k], k, prior[k]);  //测试用 
                if(prior[k]>1)  {   //发现有结点前驱个数大于1,则不是树         
                    yes=0;
                    break;
                }
                if(prior[k]==0)  count0++;  //统计前驱为0的结点个数 
            }
            if(!yes||count0!=1)  //树中只有一个结点(根)的前驱数为0 
                    printf("Case %d is not a tree.\n", T);
            else
                printf("Case %d is a tree.\n", T);

            i=0; count0=0; //  准备接收新的一组数据,data数组下标归0,计数器清0,
            memset(data, 0, sizeof(data));  //将data数组中的数全部置0 ,此处也可以不写 
            memset(prior, 0, sizeof(prior)); //将Prior数组中的计数器全部置0 
        }
    }
    return 0;
}

//在数组data的前i个数中,判断是否有x,
// 若有则返回下标位置, 若无则返回-1 
int Locate(int data[], int i, int x)  
{   
    int n;
    for(n=0; n<i; n++)
        if(data[n]==x) return n;
    return -1;      
}



A-4 Is It A Tree(c++题目,代码不要有注释) 分数 30 作者 陈越 单位 浙江大学 By definition, a tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root; and zero or more nonempty (sub)trees, each of whose roots are connected by a directed edge from r. In other words, a tree is a spacial case of a directed graph. Given a directed graph, your job is to tell whether or not it is a tree. Input Specification: Each input file contains one test case, which starts from a line containing a positive integer n (≤10 4 ), the number of nodes. Hence we assume that all the nodes are numbered from 1 to n. Then several lines follow, each describes a directed edge in the format source destination where both are the nodes' indices. It is guaranteed that there are no more than 10 4 edges, and that source is never the same as destination. The input ends with source being zero, and that line must not be processed. Note: duplicated edges are counted as ONE edge (as shown by the first sample). Output Specification: If the given graph is a tree, out put in a line yes root, where root is the index of the root. Or if not, output no k where k is the number of nodes with 0 indegree. Sample Input 1: 7 2 1 2 1 4 3 4 2 6 5 6 4 6 7 0 Sample Output 1: yes 6 Sample Input 2: 7 2 1 4 3 4 2 6 4 6 7 5 3 0 Sample Output 2: no 2 鸣谢柳汀洲补充数据! 代码长度限制 16 KB Java (javac) 时间限制 800 ms 内存限制 256 MB 其他编译器 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值