无法通过不知为何?
#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;
}