• 【裸的并查集】POJ 1611 The Suspects


    http://poj.org/problem?id=1611

    【Accepted】

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include <string>
     6 using namespace std;
     7 const int maxn=3e4+3;
     8 int fa[maxn];
     9 int n,m;
    10 int a[maxn];
    11 
    12 void init()
    13 {
    14     for(int i=0;i<n;i++)
    15     {
    16         fa[i]=i;
    17     }
    18 }
    19 int find(int x)
    20 {
    21     return x==fa[x]?x:fa[x]=find(fa[x]);    
    22 }
    23 
    24 void mix(int x,int y)
    25 {
    26     int fx=find(x);
    27     int fy=find(y);
    28     if(fx!=fy)
    29     {
    30         fa[fx]=fy;
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     while(~scanf("%d%d",&n,&m))
    37     {
    38         if(n==0&&m==0)
    39         {
    40             break;
    41         }
    42         init();
    43         for(int i=0;i<m;i++)
    44         {
    45             int k;
    46             scanf("%d",&k);
    47             if(k>=1)
    48             {
    49                 scanf("%d",&a[0]);
    50                 for(int j=1;j<k;j++)
    51                 {
    52                     scanf("%d",&a[j]);
    53                     mix(a[0],a[j]); 
    54                 }
    55             }
    56         }
    57         int sum=0;
    58         for(int i=0;i<n;i++)
    59         {
    60             if(find(i)==fa[0])
    61             {
    62                 sum++;
    63             }
    64         }
    65         printf("%d
    ",sum);
    66     }
    67     return 0;
    68 }
    View Code

    【不太明白】

    为啥find(i)==fa[0]能A,fa[i]==fa[0]就是WA。find(0)一定等于fa[0]?

  • 相关阅读:
    理解HTTP幂等性
    企业技术树
    数据库MySQL-Oracle-DB2-SQLServer分页查询
    Redis安装教程
    Redis VS Memcached
    Redis简介
    Redis系列文章导读
    坐标轴
    图例
    画网格
  • 原文地址:https://www.cnblogs.com/itcsl/p/7153016.html
Copyright © 2020-2023  润新知