【带权并查集】银河英雄传说

洛谷P1196 [NOI2002]银河英雄传说

一道喜闻乐见的带权并查集qwq

出了以下锅

  • 没在主程序中写上预处理//撞墙吧
  • int k = f[x]   dis[x] += dis[k]
  • 写成 dis[x] += dis[f[x]]
  • 意识到f[x]在变以后把这一句写到了f[x] = find(f[x]) 前面QAQ
  • 可是这个dis[x] 是由x的祖先一步步推回来的啊QAQ 

贴代码QAQ

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 const int sz = 30030;
 6 int f[sz], num[sz], dis[sz];
 7 int T, ans = 0;
 8 char opt[2];
 9 void init() {
10     for(int i = 1; i <= 30000; i++) {
11         f[i] = i;
12         num[i] = 1;
13         dis[i] = 0;
14     }
15 }
16 int find(int x) {
17     if(x != f[x]) {
18         int k = f[x];
19         f[x] = find(f[x]);
20         dis[x] += dis[k];
21         num[x] = num[f[x]];
22     }
23     return f[x];
24 }
25 int merge(int u, int v) {
26     int r1 = find(u), r2 = find(v);
27     f[r1] = r2;
28     dis[r1] = dis[r2] + num[r2];
29     num[r2] += num[r1];
30     num[r1] = num[r2];
31 }
32 int main() {
33 //    freopen("qaq.out","w",stdout);
34     scanf("%d",&T);
35     init();
36     while(T--) {
37         int i, j;
38         scanf("%s",opt+1);
39         if(opt[1]=='M') {
40             scanf("%d%d",&i, &j);
41             merge(i, j);
42         }
43         else {
44             scanf("%d%d",&i, &j);
45             if(find(i) != find(j)) {
46                 printf("-1\n");
47                 continue;
48             }
49             else {
50                 ans = abs(dis[i] - dis[j]) - 1;
51                 printf("%d\n",ans);
52             }
53         }
54     }
55     return 0;
56 }

 

posted @ 2018-10-12 09:02  _Hwjia  阅读(144)  评论(0编辑  收藏  举报
                       ::                              ::
                      :;J7, :,                        ::;7:
                      ,ivYi, ,                       ;LLLFS:
                      :iv7Yi                       :7ri;j5PL
                     ,:ivYLvr                    ,ivrrirrY2X,
                     :;r@Wwz.7r:                :ivu@kexianli.
                    :iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
                   ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
                ;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
              :,, ,,:   ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
                 ,::,    ::rv77iiiriii:iii:i::,rvLq@huhao.Li
             ,,      ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
           :::  ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
          ,     2OBBOi,iiir;r::        ,irriiii::,, ,iv7Luur:
        ,,     i78MBBi,:,:::,:,  :7FSL: ,iriii:::i::,,:rLqXv::
        :      iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
       ,     ::::i   ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
      ,       :       , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
     ,               ,,,,::::i:  @arqiao.       ,:,, ,:::ii;i7:
    :,       rjujLYLi   ,,:::::,:::::::::,,   ,:i,:,,,,,::i:iii
    ::      BBBBBBBBB0,    ,,::: , ,:::::: ,      ,,,, ,,:::::::
    i,  ,  ,8BHWJBBBBBi     ,,:,,     ,,, , ,   , , , :,::ii::i::
    :      iZMOMAQBBM2::::::::::,,,,     ,,,,,,:,,,::::i:irr:i:::,
    i   ,,:;u0MBMOG1L:::i::::::  ,,,::,   ,,, ::::::i:i:iirii:i:i:
    :    ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
    :     :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
     :      5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
          , :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
              :jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
           ,:  :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
           ,,,     ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
        , , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
         :,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::