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 newjunwei at 2008-03-02 15:47:48 on Problem 1419
#include<iostream>
using namespace std;
int re2[100];
int c[100];
int re[100];
int b,max;
int m,n,l;
struct list
{
	int i;
	list * next;
};
list * h[100],*t[100];
inline void add(int x,int y)
{
	list *p;
	if(h[x]==NULL)
	{
		p=new list;
		p->i=y;
		p->next=NULL;
		h[x]=p;
		t[x]=p;
	}
	else 
	{
		p=new list;
		p->i=y;
		p->next=NULL;
		t[x]->next=p;
		t[x]=p;
	}
}
void search(int x)
{
	if(x>n)
	{
		if(b>max)
		{
			max=b;
			int j=0;
			for(j=0;j<b;j++)
				re2[j]=re[j];
		}
		return ;
	}
	if(c[x]!=0)search(x+1);
	else
	{
		c[x]=1;re[b++]=x;
		list *p=h[x];
		while(p!=NULL)
		{
			c[p->i]=2;
			p=p->next;
		}
		search(x+1);
		p=h[x];
		while(p!=NULL)
		{
			c[p->i]=0;
			p=p->next;
		}	
		b--;c[x]=2;
		search(x+1);
		c[x]=0;
	}
}
int main()
{

	cin>>m;
	while(m--)
	{
		int i;
		b=0;max=0;
		cin>>n>>l;
		//init
		for(i=1;i<=n;i++)
			h[i]=NULL;
		for(i=1;i<=n;i++)
			c[i]=0;
		int x,y;
		for(i=0;i<l;i++)
		{
			cin>>x>>y;
			if(x<y)
	add(x,y);
			else 
	add(y,x);
		}
		/////////////////
		search(1);	
		cout<<max<<endl;
		for(i=0;i<max;i++)
			cout<<re2[i]<<" ";
		cout<<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