Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么总是WA呀,有没有好点的测试数据啊???花了好几个小时了。到底那儿错了啊? 自己的给的测试数据都过了的,什么样的数据会不过呀? #include <iostream.h> class Node{ public: int id; bool isFather; bool isChild; Node *father; Node *next; Node(int id) { this->id = id; next = NULL; father = NULL; isFather = false; isChild = false; } ~Node() { if(next) delete next; } }; /*void print(Node *n) { cout<<"Node "<<n->id<<": "; if( n->isFather && n->isChild) cout<<"isFather isChild:"<<n->father->id; if( n->isFather &&!n->isChild) cout<<"isFather "; if(!n->isFather && n->isChild) cout<<" isChild:"<<n->father->id; cout<<endl; if(n->next!=NULL) print(n->next); }*/ void main() { int father,child; bool isTree; Node *fatherNode; Node *head,*temp,*tail,*t,*root,*p; int num=0; while(true) { num++; isTree = true; head = NULL; cin>>father>>child; if(father<0 && child<0) break; temp = new Node(father); temp->isFather = true;head = temp; tail = temp; fatherNode = temp; temp = new Node(child); temp->isChild = true; temp->father=fatherNode; tail->next = temp; tail = temp; //print(head); while(true) { cin>>father>>child; if(father==0 && child ==0) break; t = head; while(t!=NULL) { if(t->id == father ) break; t = t->next; } if(t == NULL) { temp = new Node(father); temp->isFather = true; tail->next = temp; tail = temp; fatherNode = temp; } else { t->isFather = true; fatherNode = t; } //////////////////////// 下面处理新输入的子节点 t=head; while(t!=NULL) { if(t->id == child ) break; t = t->next; } if(t==NULL) { temp = new Node(child); temp->isChild = true; temp->father = fatherNode; tail->next = temp; tail = temp; } else { if(t->isChild == true) { //有两条边指向了这个节点了,不是Tree了 isTree = false; while(true) { cin>>father>>child; if(father==0 && child==0) break; } //cout<<"Node "<<t->id<<" has two fathers"<<endl; break; } else { t->isChild = true; t->father = fatherNode; } } //cout<<"\\\\\\\\\\\\\\\\\\\\\\gooood"<<endl; //print(head); } if(isTree == false ) cout<<"Case "<<num<<" is not a tree."<<endl; else { int rootnum = 0; for(t=head; t!=NULL; t=t->next) { if(!t->isChild) { rootnum++; root = t;} } if(rootnum==1) { //注意有圈的情况 isTree = true; for(t=head->next;t!=NULL;t=t->next){ if(t==root) continue; temp = t->father; p = t; while(true) { //循环检查是否含有圈 if(p==temp) {isTree = false; break;} if(temp->father == NULL || temp->father->father==NULL) break; else temp = temp->father->father; p = p->father; } if(!isTree) break; //只要没有圈,就一定是树了 //下面查找这个节点是否有到根节点的一条路径 temp = t->father; bool findpath = false; while(temp != NULL) { if(temp == root ) { findpath = true; break;} temp = temp->father; } if(! findpath) isTree = false; } if(isTree) cout<<"Case "<<num<<" is a tree."<<endl; else cout<<"Case "<<num<<" is not a tree."<<endl; } // root 的个数不为1 else cout<<"Case "<<num<<" is not a tree."<<endl; } //print(head); delete head; head = NULL; } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator