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

郁闷……

Posted by songzhenqi at 2010-07-19 09:35:36 on Problem 1308
算法虽然笨了点,但是个人觉得没问题了啊……测试数据都过了,就是WA…………
我是新手,哪位大哥可以赐教?


#include <iostream>
using namespace std;
int tree(int &m)
{   
	m++;
	int fa[100];
	int ch[100];
	int i,p,q;//定义三个循环变量
	int f=0,n=0;
	int flag=1;//定义判断变量,附初值为“真”
	int bootn=0;//判断根的数目,附初值为0;
	int boot=-10;//定义根变量
	for(i=1;i<100;i++)//存储所需数据
	{
		cin>>fa[i];
		cin>>ch[i];
		if(fa[1]<0&&ch[1]<0)//判断是否结束输入
		{
			f=1;
			break;
		}
		n=n+1;
		if(fa[i]==ch[i]&&fa[i]!=0)//判断是否指向自己
			f=2;
		
		if(fa[i]==0&&ch[i]==0)  
			break;
	}
	if(f==2) return (flag=0);//如果自己指向自己则不是树
	if(fa[1]==0&&ch[1]==0) return (flag=1);
	if(f==1) return (flag=2);
	
	else
	{
	for(p=1;p<n;p++)//一个“孩子”不能有两个“父亲”
	{
       int y;
	   for(y=1;y<n;y++)
	   {
		   if(ch[p]==ch[y]&&p!=y)
			   return (flag=0);
	   }
	
	}
	//cout<<"go on"<<endl;
	for(q=1;q<n;q++)//“父亲”中不能有两个不相同的根
	{   
		if(fa[q]==boot) 
			break;
        int x,f2=1;
		for(x=1;x<n;x++)
		{   
			if(fa[q]==ch[x])  //如果在子数组中存在,则不为根
			{
				f2=0;
				break;
			}
		}
		    if(f2==1)
			{
				boot=fa[q];
		        bootn++;
			}
		
	}
//	cout<<"bootn="<<bootn<<" boot="<<boot;
	if(bootn!=1)
		flag=0;
	//cout<<"  "<<flag<<endl;
	return flag;
	}
	
}


int main()
{   //freopen("D:/in.txt","r",stdin);
	int tree(int &);
    int k=0;
	int flag[100];
	int i=1,p;
    while(i<100)
	{
		flag[i]=tree(k);
		if(flag[i]==2) 
			break;
		i++;
	}
	for(p=1;p<k;p++)
	{
		if(flag[p]==0) cout<< "Case "<<p<<" is not a tree." <<endl;
		if(flag[p]==1) cout<< "Case "<<p<<" is a tree." <<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