UVA 1329 Corporative Network【並查集】

來源:互聯網
上載者:User

標籤:uil   post   int   mem   amp   iostream   add   key   std   

題目連結:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075

題意:
有n個結點,開始都是單獨的結點,如今有I操作和E操作,I u v表示吧u的父親結點設為,距離為|u - v| % 1000,E操作詢問u到根的距離

代碼:

#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#include <stack>#include <map>using namespace std;int fa[1000010];int d[1000010];int fd(int x){    if (fa[x] != -1)    {        int rot = fd(fa[x]);        d[x] += d[fa[x]];        return fa[x] = fd(fa[x]);    }    else return x;}int main(){    int a, b;    int t, n;    scanf("%d",&t);    char cmd[10];    while (t--)    {        memset(fa, -1, sizeof(fa));        memset(d,0,sizeof(d));        scanf("%d", &n);        while (scanf("%s", cmd) && cmd[0] != ‘O‘)        {            if (cmd[0] == ‘E‘)            {                scanf("%d", &a);                fd(a);                printf("%d\n", d[a]);            }            else            {                scanf("%d%d", &a, &b);                fa[a] = b;                d[a] = abs(a - b) % 1000;            }        }    }    return 0;}

UVA 1329Corporative Network【並查集】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.