#include#include#includeusingnamespacestd;typedeflonglongLL;intset[111111];intcnt[111111];LLsum[111111];intp[111" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

uva 11987 Almost Union-Find (并檢查集合)

系統 2148 0

標題效果:

三操作。

1. 合并兩個集合

2.代替所述第二組的第一個元素

3.輸出設置數量,并。。


IDEAS:

使用p該元素的記錄數,其中集合,建立并查集。


      #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long LL;
int set[111111];
int cnt[111111];
LL sum[111111];
int p[111111];

int find(int x)
{
    if(x!=set[x])
    set[x]=find(set[x]);
    return set[x];
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)set[i]=i,cnt[i]=1,sum[i]=i,p[i]=i;

        while(m--)
        {
            int op;
            scanf("%d",&op);
            int l,r;
            if(op==1)
            {
                scanf("%d%d",&l,&r);
                int xx=find(p[l]);
                int yy=find(p[r]);
                if(xx==yy)continue;
                else
                {
                    set[xx]=yy;

                    cnt[yy]+=cnt[xx];
                    sum[yy]+=sum[xx];
                }
            }
            else if(op==2)
            {
                scanf("%d%d",&l,&r);
                int xx=find(p[l]);
                int yy=find(p[r]);
                if(xx==yy)continue;
                else {
                    sum[xx]-=l;
                    cnt[xx]--;
                    sum[yy]+=l;
                    cnt[yy]++;
                    p[l]=yy;
                }
            }
            else
            {
                scanf("%d",&l);
                printf("%d %lld\n",cnt[find(p[l])],sum[find(p[l])]);
            }
        }
    }
    return 0;
}

    


版權聲明:本文博客原創文章,博客,未經同意,不得轉載。

uva 11987 Almost Union-Find (并檢查集合)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 祁东县| 麟游县| 商丘市| 天峻县| 南昌市| 惠水县| 沧源| 拜城县| 萨嘎县| 平阴县| 桃园县| 梓潼县| 东海县| 都江堰市| 宜黄县| 绵竹市| 高尔夫| 景德镇市| 安化县| 阿拉尔市| 抚顺县| 双峰县| 儋州市| 钦州市| 陇川县| 尼木县| 玉林市| 新建县| 故城县| 阿城市| 邢台县| 甘孜| 榆林市| 工布江达县| 时尚| 天柱县| 枣阳市| 徐水县| 芜湖县| 彭水| 无为县|