| ||||||||||
| 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 | |||||||||
各位大牛,小弟初学并查集,按discuss里的提示写了一个,不知道为什么总是WA...请提示....(附代码)//pku 2236
#include "stdio.h"
int n,d;
int f[1005];
int flag[1005];
struct node
{
int x;
int y;
}com[1005];
int getfather(int p)
{
int father;
if(f[p]==p) father=p;
else {
int k=getfather(f[p]);
f[p]=k;
father=f[p];
}
return father;
}
void solve(int p)
{
flag[p]=1;
int s=getfather(p);
for(int i=1;i<=n;i++)
{
if(flag[i]&&i!=p)
{
if((com[p].x-com[i].x)*(com[p].x-com[i].x)+(com[p].y-com[i].y)*(com[p].y-com[i].y)<=d*d)
{
int t=getfather(i);
if(s!=t)
f[t]=s;
}
}
}
}
void ans(int p,int q)
{
if(getfather(p)==getfather(q)) printf("SUCCESS\n");
else printf("FALL\n");
}
int main()
{
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&com[i].x,&com[i].y);
f[i]=i;
flag[i]=0;
}
char ch;
while(getchar(),scanf("%c",&ch)!=EOF)
{
if(ch=='O')
{
int p;
scanf("%d",&p);
solve(p);
}
else if(ch=='S')
{
int p,q;
scanf("%d%d",&p,&q);
ans(p,q);
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator