zoukankan      html  css  js  c++  java
  • GANG BLACK GANG WHITE:自己找出BUG来了

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<math.h>
     4 #include<string.h>
     5 using namespace std;
     6 int a[50100];
     7 int find(int x){
     8   if (a[x]==x) return x;
     9   else {
    10     a[x]=find(a[x]);
    11     return a[x];
    12   }
    13 }
    14 int main()
    15 {
    16     freopen("input.txt","r",stdin);
    17     int num;
    18     int x,y,u,v,person,item;
    19     scanf("%d",&num);
    20     for(int i=1;i<=num;i++)
    21     {
    22         memset(a,0,sizeof(0));
    23         scanf("%d%d",&person,&item);
    24         if(item==0) {printf("Test case #%d:\n",i);printf("Nothing special.\n\n");continue;}
    25         for(int j=1;j<=person;j++) a[j]=j;
    26         a[50010]=50010;a[50011]=50011;
    27         int ok=1;
    28         for(int j=1;j<=item;j++)
    29         {
    30            scanf("%d%d",&x,&y);
    31            if(!ok) continue;
    32            u=find(x);v=find(y);
    33            printf(" ~%d %d~ ",u,v);
    34            if(u==v) ok=0;
    35            else if(u==50010&&v!=50011) {a[y]=50011;}
    36            else if(u==50011&&v!=50010) {a[y]=50010;}
    37            else if(v==50010&&u!=50011)  a[x]=50011;
    38            else if(v==50011&&u!=50010)  a[x]=50010;
    39            else if((u==50010&&v==50011)||(u==50011&&v==50010));
    40            else {a[x]=50011;a[y]=50010;}
    41         }
    42         if(ok==1)
    43          {printf("Test case #%d:\n",i);printf("Nothing special.\n");}
    44         else {printf("Test case #%d:\n",i);printf("Something wrong!\n");}
    45          printf("\n");
    46     }
    47     return 0;
    48 }

    上面那个代码是错误的,自己提交了那么多次终于找到BUG了。其实这样也好,知道代码错了比一直都在纠结中强。

    再一个错误就是自己随便给father是不对的。

    比如数据:1组:1 2 3 4

                   2组:5 6 7 8

                  则 给出询问1 5

                                6 2

                                6 1 就是错误的

    正确的做法:a和b为敌人,a和c为敌人。 则合并b和c。

    顺便说一句:给出算法思想,直接照他这个思路做一般是不会做出来的,少了一些类似赋初始状态神马的。甚至是动态规划。

  • 相关阅读:
    内部类&匿名内部类
    Object:
    多 态★★★★★(面向对象特征之三)
    接 口:
    继 承(面向对象特征之二)
    封 装(面向对象特征之一)
    三:面向对象:★★★★★
    Codeforces 719 E. Sasha and Array (线段树+矩阵运算)
    uestc oj 1218 Pick The Sticks (01背包变形)
    uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
  • 原文地址:https://www.cnblogs.com/cgf1993/p/3089542.html
Copyright © 2011-2022 走看看