LA 3027 Corporative Network

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

 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!!

 

 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         char cmd[9];

29         for(int i = 1; i <= n; ++i)

30         {

31             parent[i] = i;

32             distant[i] = 0;

33         }

34         while(scanf("%s", cmd) && cmd[0] != 'O')

35         {

36             int a, b;

37             if(cmd[0] == 'E')

38             {

39                 scanf("%d", &a);

40                 GetParent(a);

41                 printf("%d\n", distant[a]);

42             }

43             else

44             {

45                 scanf("%d%d", &a, &b);

46                 parent[a] = b;

47                 distant[a] = abs(a - b) % 1000;

48             }

49         }

50     }

51     return 0;

52 }
代码君

 

你可能感兴趣的:(NetWork)