码迷,mamicode.com
首页 > Web开发 > 详细

LA 3027 Corporative Network

时间:2014-08-24 19:22:02      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   art   

这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组

 

bubuko.com,布布扣
 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 LOCAL
20         freopen("3027in.txt", "r", stdin);
21     #endif
22 
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             else
45             {
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

标签:style   blog   http   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/3933069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!