Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

开始总是runtime error

Posted by zpdlut at 2010-09-13 16:46:14 on Problem 1330
注意parent的指针,id不要是-1;
#include<iostream>
#define MAX 10005
using namespace std;
bool bf[MAX];
struct nodes{
	nodes * parent;
	int pid;
	int cid;
	int id;
};
int Find(nodes a,nodes b)
{
    memset(bf,0,sizeof(bf));
    while(1)
    {
		bf[a.id]=true;
		if(a.parent == NULL)
		{
			bf[a.id]=true;
			break;
		}
		a = *a.parent;    
	}
    while(1)
    {
        if(bf[b.id]==true)
            return b.id;
        else
			b=*b.parent;          
    }
}
int main()
{
	int nums;
	cin>>nums;
	nodes set[MAX];
	for(int i=0;i<nums;i++)
	{
		for(int i=0;i<MAX;i++)
		{
			set[i].parent = NULL;
			set[i].id = -1;
			set[i].pid = -1;
			set[i].cid = -1;
		}
		int edge,parent,son;
		int target1,target2;
		cin>>edge;
		for(int i=0;i<edge-1;i++)
		{
			cin>>parent>>son;
			set[son].parent=&set[parent];
			set[son].pid=parent;
			set[son].id = son;
			set[parent].cid = set[son].id; 
		}
		for(int i=0;i<edge;i++)
		{
			if(set[i].pid==-1)
			{
				int child = set[i].cid;
				set[i].id = set[child].pid;
				set[i].parent = NULL;
				set[i].pid = 0;
			}
		}
		cin>>target1>>target2;
		cout<<Find(set[target1],set[target2])<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator