zoukankan      html  css  js  c++  java
  • poj1988

    并查集 r[]记录节点到跟节点的距离 num[] 树的节点数

    View Code
     1 #include <stdio.h>
     2 int father[30005],r[30005],num[30001];
     3 int find(int x)
     4 {
     5     if(x!=father[x])
     6     {
     7         int t = father[x];
     8         father[x] = find(father[x]);
     9         r[x]+=r[t];
    10     }
    11     return father[x];
    12 }
    13 
    14 void union1(int x, int y)
    15 {
    16     father[y] = x;
    17     r[y]+=num[x];
    18     num[x]+=num[y] ;
    19 
    20 }
    21 int main()
    22 {
    23     int n, i, j, x, y, k;
    24     char c;
    25     scanf("%d", &n);
    26     for(i = 1 ; i <= 30000 ; i++)
    27     {
    28         father[i] = i;
    29         r[i] = 0;
    30         num[i] = 1;
    31     }
    32     while(n--)
    33     {
    34         getchar();
    35         scanf("%c", &c);
    36         if(c == 'M')
    37         {
    38             scanf("%d%d", &x,&y);
    39             int pa = find(x);
    40             int pb = find(y);
    41             if(pa!=pb)
    42             {
    43                 union1(pa,pb);
    44             }
    45         }
    46         else
    47         {
    48             scanf("%d", &x);
    49             y = find(x);
    50             printf("%d\n", num[y]-r[x]-1);
    51         }
    52     }
    53     return 0;
    54 }
    View Code
     1 #include <stdio.h>
     2  int father[30005],r[30005],num[30001];
     3  int find(int x)
     4  {
     5      if(x!=father[x])
     6      {
     7          int t = father[x];//这个跟下面的那条不能反 保证t不为跟节点
     8          father[x] = find(father[x]);//回溯时把节点都指向根节点  方便下次查找     
     9          r[x]+=r[t];
    10      }
    11      return father[x];
    12  }
    13 
    14  void union1(int x, int y)
    15  {
    16      father[y] = x;
    17      r[y]+=num[x];
    18      num[x]+=num[y] ;
    19 
    20  }
    21  int main()
    22  {
    23      int n, i, j, x, y, k;
    24      char c;
    25      scanf("%d", &n);
    26      for(i = 1 ; i <= 30000 ; i++)
    27      {
    28          father[i] = i;
    29          r[i] = 0;
    30          num[i] = 1;
    31      }
    32      while(n--)
    33      {
    34          getchar();
    35          scanf("%c", &c);
    36          if(c == 'M')
    37          {
    38              scanf("%d%d", &x,&y);
    39              int pa = find(x);
    40              int pb = find(y);
    41              if(pa!=pb)
    42              {
    43                  union1(pa,pb);
    44              }
    45          }
    46          else
    47          {
    48              scanf("%d", &x);
    49              y = find(x);
    50              printf("%d\n", num[y]-r[x]-1);
    51          }
    52      }
    53      return 0;
    54  }
  • 相关阅读:
    服务器 空间
    android 手动打包
    sql server 2000 完全卸载 2
    apk 优化
    asp.net 画 数据图表
    java 运行 bat win linux
    vb 生成 批处理
    国内平台
    委托
    observer 观察者模式
  • 原文地址:https://www.cnblogs.com/shangyu/p/2579411.html
Copyright © 2011-2022 走看看