| ||||||||||
| 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 | |||||||||
Re:Hawk大哥,我这个O(m)的复杂度的C++程序,G++ RE, C++ TLE,真的搞不明白了啦。。。。。。In Reply To:Hawk大哥,我这个O(m)的复杂度的C++程序,G++ RE, C++ TLE,真的搞不明白了啦。。。。。。 Posted by:huicpc16 at 2005-11-02 21:47:14 #include<iostream>
#include<cstring>
#define N 1000000
using namespace std;
struct K{
int x,y;
}k[N];
bool vist[N];
int tot = 1;
int re[N];
bool flag = false;
bool dfs(int ceng,int pre)
{
re[ceng] = pre;
if(ceng==tot-1)
{
if(pre==1)
return true;
return false;
}
for(int i = 1;i<tot;i++)
{
if(!vist[i]&&k[i].x==pre)
{
vist[i] = true;
if(dfs(ceng+1,k[i].y))
return true;
vist[i] = false;
}
}
return false;
}
int main()
{
// freopen("in","r",stdin);
int n,m;
while(cin>>n>>m)
{
if(n==1)
{
cout<<"1"<<endl;
continue;
}
if(n==0&&m==0)
break;
tot = 1;
memset(vist,false,sizeof(vist));
int a,b;
int id,k1 = 0;
for(int i = 1;i<=m;i++)
{
cin>>a>>b;
if(a==1&&k1==0)
{
k1 = b;
id = tot;
}
k[tot].x = a;
k[tot++].y = b;
k[tot].x = b;
k[tot++].y = a;
}
flag = false;
vist[id] = true;
if(dfs(1,k1))
{
re[0] = 1;
for(int i = 0;i<tot;i++)
cout<<re[i]<<endl;
}
}
}
我的这个dfs跟你的差不多,但也是tle
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator