LA 3027 Corporative Network

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   art   

這題感覺和 POJ 1988 Cube Stacking 很像,在路徑壓縮的同時遞迴出來的時候跟新distant數組

 

 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5  6 const int maxn = 20000 + 10; 7 int parent[maxn], distant[maxn]; 8  9 int GetParent(int a)10 {11     if(parent[a] == a)    return a;12     int root = GetParent(parent[a]);13     distant[a] += distant[parent[a]];14     return parent[a] = root;15 }16 17 int main(void)18 {19     #ifdef LOCAL20         freopen("3027in.txt", "r", stdin);21     #endif22 23     int T, n;24     scanf("%d", &T);25     while(T--)26     {27         scanf("%d", &n);28         for(int i = 1; i <= n; ++i)29         {30             parent[i] = i;31             distant[i] = 0;32         }33         getchar();34         char p;35         while(scanf("%c", &p) == 1 && p != ‘0‘)36         {37             int a, b;38             if(p == ‘E‘)39             {40                 scanf("%d", &a);41                 GetParent(a);42                 printf("%d\n", distant[a]);43             }44             else45             {46                 scanf("%d%d", &a, &b);47                 parent[a] = b;48                 distant[a] = abs(a - b) % 1000;49             }50             getchar();51         }52     }53     return 0;54 }
代碼君

 

LA 3027 Corporative 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.