| ||||||||||
| 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 | |||||||||
就一层循环怎么还TLE 帮帮忙!郁闷死了!!这是我的代码:#include<iostream.h>
int c[2005];
struct two
{
int a,b;
};
two str [2001];
int N,M;
int pre[2001],rank[2001];
void makeset(int x)
{
pre[x]=-1;
rank[x]=0;
}
int find(int x)
{
int r=x;
while(pre[r]!=-1)
r=pre[r];
//压缩路径
while(x!=r)
{
int q=pre[x];
pre[x]=r;
x=q;
}
return r;
}
void unionone(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(rank[t1]>rank[t2])
pre[t2]=t1;
else
pre[t1]=t2;
if(rank[t1]==rank[t2])
rank[t2]++;
}
void main()
{
int i,o,u,p,t;
cin>>t;
for(int l=1;l<=t;l++)
{
for(int ii=0;ii<2005;ii++)
c[ii]=0;
cin>>N>>M;
p=0;
for(i=1;i<=N;i++)
makeset(i);
for(i=1;i<=M;i++)
{
cin>>o>>u;
if(find(o)==find(u))
p=1;
if(c[o]==0)
c[o]=u;
if(c[u]==0)
c[u]=o;
str[i].a=o;
str[i].b=u;
if(p!=1)
{
if(c[o]!=0)
{
int q=c[o];
if(q!=u)
unionone(q,u);
}
if(c[u]!=0)
{
int q=c[u];
if(q!=o)
unionone(q,o);
}
}
}
if(p==1)
{
cout<<"Scenario #"<<l<<':'<<endl;
cout<<"Suspicious bugs found!"<<endl;
}
else
{
cout<<"Scenario #"<<l<<':'<<endl;
cout<<"No Suspicious bugs found!"<<endl;
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator