zoukankan      html  css  js  c++  java
  • poj 1988 并查集(终于看懂一个了/(ㄒoㄒ)/~~)

    题意:有几个stack,初始里面有一个cube。支持两种操作:1.move x y: 将x所在的stack移动到y所在stack的顶部。2.count x:数在x所在stack中,在x之下的cube的个数。

    链接:点我

     1 /*
     2 POJ 1988
     3 */
     4 #include <stdio.h>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <string.h>
     8 using namespace std;
     9 const int MAXN=30010;
    10 int F[MAXN];
    11 int num[MAXN];//堆的数目
    12 int val[MAXN];//下面的个数
    13 int find(int x)
    14 {
    15     if(F[x]==-1)return x;
    16     int tmp=find(F[x]);
    17     val[x]+=val[F[x]];
    18     return F[x]=tmp;
    19 }
    20 void bing(int u,int v)//将u所在的堆放在v上面,注意顺序
    21 {
    22     int t1=find(u),t2=find(v);
    23     if(t1!=t2)
    24     {
    25         F[t1]=t2;
    26         val[t1]=num[t2];
    27         num[t2]+=num[t1];
    28     }
    29 }
    30 int main()
    31 {
    32     int P;
    33     int u,v;
    34     char op[10];
    35     while(scanf("%d",&P)==1)
    36     {
    37         for(int i=0;i<MAXN;i++)
    38         {
    39             F[i]=-1;
    40             val[i]=0;
    41             num[i]=1;
    42         }
    43         while(P--)
    44         {
    45             scanf("%s",&op);
    46             if(op[0]=='C')
    47             {
    48                 scanf("%d",&u);
    49                 find(u);
    50                 printf("%d
    ",val[u]);
    51             }
    52             else
    53             {
    54                 scanf("%d%d",&u,&v);
    55                 bing(u,v);
    56             }
    57         }
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    [zz]libvirt中CPU和内存的细粒度管理机制
    SAP 模块中文解释
    邪恶的Php一句话木马 assert函数制作简单木马
    PHP开发中三维数组的应用
    返回本机时间或服务器时间
    向SQL中插入数据
    Word的字体
    人生如锅
    打开指定的文件
    计算最大序号
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4480993.html
Copyright © 2011-2022 走看看