HDU1325---Is It A Tree? 无法通过

无法通过不知为何?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LineNode{
	int FirstData;
	int SecondData;
	struct LineNode *next;
}LineNode;

int PathComplete(LineNode *head,int root){
	int n=0;
	int total=0;
	LineNode *p=head;
	for(p=head;p->next!=NULL;p=p->next){
		if(p->FirstData==root){
			n=PathComplete(head,p->SecondData);
			total=total+n+1;
		}
	}

	return total;
}

int TreeJudge(LineNode *head){
	int num1;
	int num2;
	int value1;
	int value2;
	int root=0;
	LineNode *pre;
	LineNode *p=head->next;
	for(pre=head;pre->next!=NULL;pre=pre->next){
		for(p=pre->next;p->next!=NULL;p=p->next){
			if(pre->SecondData==p->SecondData)
				return 1;
		}
		num1++;
	}
	for(pre=head;pre->next!=NULL;pre=pre->next){
		for(p=head;(p->next!=NULL)&&(pre->FirstData!=p->SecondData);p=p->next);
		if(pre->FirstData!=p->SecondData){
			root++;
			if(root==1){
				value2=pre->FirstData;
			    value1=value2;
			}else{
				value2=pre->FirstData;
			}
			if(value1!=value2)
				return 1;
		}
	}
	if(root==0)
		return 1;
	num2=PathComplete(head,value1);
	if(num1!=num2)
		return 1;
	return 0;
}

int main(void) {
	int n=0;
	int judge;

	int FirstData,SecondData;
	LineNode *pre;
	LineNode *p;
	LineNode *head;
	FirstData=1;
	SecondData=1;
	while(FirstData>=0||SecondData>=0){
		scanf("%d%d",&FirstData,&SecondData);
		if(FirstData==0&&SecondData==0){
			printf("case %d is a tree.",++n);
			printf("\n");
			continue;
		}else{
			head=(LineNode*)malloc(sizeof(LineNode));
			head->FirstData=FirstData;
			head->SecondData=SecondData;
			pre=head;
			head->next=NULL;
		}
		while((FirstData!=0||SecondData!=0)&&(FirstData>0||SecondData>0)){
			scanf("%d%d",&FirstData,&SecondData);
			p=(LineNode*)malloc(sizeof(LineNode));
			p->FirstData=FirstData;
			p->SecondData=SecondData;
			pre->next=p;
			p->next=NULL;
			pre=p;
		}
		if(FirstData==0&&SecondData==0){
			judge=TreeJudge(head);
			if(judge==1){
				printf("case %d is not a tree.",++n);
			}else{
				printf("case %d is a tree.",++n);
			}
		}
		printf("\n");
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值