acm 2236

Posted on 2007-07-14 20:10  少林  阅读(742)  评论(0编辑  收藏  举报
   今天在acm上做了一下2236题.一看觉的不太难,是一个典型的并查集运算.不到几分钟,就写了.但是提交了n次还没有通过,被wrong的没有感觉了,那个痛苦啊,别提了.结果费了九牛二虎之力,才把错误给找出了.原来,是update()函数里出现一点小错误.总的说来,太马虎了.开始我fm写的代码中注释的地方即:/*fm = find(m)*/;这样而在循环中中的语句,if(fm<fx){f[fm]=fx;}else if(fx<fm){f[fx]=fm;}中改变了fm的值,所以程序就不是进行并集操作了,所以总是wrong.告诉自己以后要细致,再细致些,,,..
#include<stdio.h>
typedef struct
{
int x,y;
}C;
C ele[1005];
long d;
int n,f[1005],flag[1005];
void update(int m)
{
         long len;
        int i,x,y,fx,fm;
         int find(int);
         /*fm =find(m);*/
          for(i=1;i<=n;i++)
                {
                       if(flag[i]==1&&i!=m)
                        {
                          
                  x=ele[i].x-ele[m].x;
                  y=ele[i].y-ele[m].y;
                  len=x*x+y*y;
                                 if(len<=d*d)
                                 {
                                                 fm=find(m);
                                                fx=find(i);
                                                if(fx<fm)
                                                 {  f[fx]=fm;}
                                                  else if(fx>fm)
                                               { f[fm]=fx;}
                                    }
                             }
                   }
}
int find(int x)
{
          int i,t;   
          for(i=x;f[i]>0;i=f[i]);
          while(i!=x)
          {
                 t=f[x];
                 f[x]=i;
                 x=t;
           }
          return i;
}
main()
{
    int m,i,x1,x2,fx1,fx2;
    char s;
                scanf("%d%d",&n,&d);
                for(i=1;i<=n;i++)
                {
                       scanf("%d%d",&ele[i].x,&ele[i].y);
                       f[i]=0;
                       flag[i]=0;
                   }
       while(getchar(),scanf("%c",&s)!=EOF)
                {
  
                 if(s=='O')
                 {
                                          scanf("%d",&m);
                                          flag[m]=1;
                                           update(m);
                 }
                                 else
                    {
                                 scanf("%d%d",&x1,&x2);
                                                fx1=find(x1);
                                    fx2=find(x2);
                                                if(fx1==fx2&&flag[fx1])
                                                {
                                                    printf("SUCCESS\n");
                                    }
                                                else printf("FAIL\n");
                                }
             }
}

Copyright © 2024 少林
Powered by .NET 9.0 on Kubernetes