| ||||||||||
| 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 | |||||||||
为什么 CE?代码如下 杭电的OJ能过. 这里却CE CE解释还看不懂..
#include<stdio.h>
#define maxn 1001
int i,j,father[maxn];
bool flag[maxn];
void makeset()
{
for(i=0;i<1001;i++)
{
father[i]=i;
flag[i]=0;
}
}
int findfather(int x)
{
if(x!=father[x])
father[x]=findfather(father[x]);
return father[x];
}
void Union(int a,int b)
{
int x,y;
x=findfather(a);
y=findfather(b);
if(x!=y) father[x]=y;
}
int main()
{
int a,b,max,min,cnt,t=1,n,m;
while(scanf("%d%d",&a,&b)!=EOF&&a!=-1&&b!=-1)
{
max=-1; min=maxn;
if(a==0&&b==0)
{
printf("Case %d is a tree.\n",t++);
continue;
}
makeset(); cnt=0;
cnt=0;
while(a||b)
{
if(a<min) min=a; if(b<min) min=b;
if(a>max) max=a; if(b>max) max=b;
flag[a]=flag[b]=1;
n=findfather(a);
m=findfather(b);
if(n==m) cnt=1;
else Union(n,m);
scanf("%d%d",&a,&b);
}
if(cnt==1) printf("Case %d is not a tree.\n",t++);
else
{
for(i=min;i<=max;i++)
if(flag[i]&&father[i]==i)
{
cnt++;
if(cnt>1) break;
}
if(cnt==1) printf("Case %d is a tree.\n",t++);
else printf("Case %d is not a tree.\n",t++);
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator