zoukankan      html  css  js  c++  java
  • hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 30000 + 5;
     8 int a[maxn];
     9 int fa[maxn];
    10 int Rank[maxn];
    11 int cnt[maxn];    //种类
    12 
    13 int Find(int x){
    14     if (x == fa[x])
    15         return x;
    16     int t = Find(fa[x]);
    17     cnt[x] += cnt[fa[x]];
    18     return fa[x] = t;
    19 }
    20 
    21 void init(){
    22     memset(cnt, 0, sizeof(cnt));
    23     for (int i = 0; i < maxn; i++){
    24         fa[i] = i;
    25         Rank[i] = 1;
    26     }
    27 }
    28 
    29 
    30 void set_union(int x, int y){
    31     int xx = Find(x);
    32     int yy = Find(y);
    33     if (xx == yy)
    34         return;
    35     fa[xx] = yy;
    36     cnt[xx] = Rank[yy];
    37     Rank[yy] += Rank[xx];
    38 }
    39 
    40 int main(){
    41     std::ios::sync_with_stdio(false);
    42     init();
    43     int t;
    44     cin >> t;
    45     while (t--){
    46         string s;
    47         int x, y;
    48         cin >> s;
    49         if (s == "M"){
    50             cin >> x >> y;
    51             set_union(x, y);
    52         }
    53         else{
    54             cin >> x;
    55             int t = Find(x);
    56             cout << cnt[x] << endl;
    57         }
    58     }
    59     //system("pause");
    60     return 0;
    61 }
  • 相关阅读:
    plsql-游标
    pl/sql--基本的语法及操作
    Oracle数据库管理
    JMS-ActiveMq-订阅发布模式
    JMS-ActiveMq-点对点模式
    JMS-ActiveMq
    poi之excel的模板导入(随后分享)
    数据流写出util
    dba_tables、all_tables、user_tables
    oracle的一些操作
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/7862270.html
Copyright © 2011-2022 走看看