Corporative Network

#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <stdio.h>

using namespace std;

int A[20010];

int top[20010];
int TT = 0;

void Link(int x, int y)
{
    A[x] = abs(x-y)%1000;
    top[x] = y;
}

int Get(int x)
{
    if(top[x] == x)
    {
        TT = x;			//每次找到根节点
        return A[x];
    }

    A[x] += Get(top[x]);

    top[x] = TT;	//更新根节点

    return A[x];
}

int main(void)
{
    freopen("G:\\data.txt", "r", stdin);

    int T;
    cin >> T;

    while(T--)
    {
        int N;
        char cc;
        scanf("%d", &N);

        for(int i = 1; i <= N; i++)
        {
            top[i] = i;
            A[i] = 0;
        }

        while(cin >> cc, cc != 'O')
        //while(scanf("%c", &cc), cc != 'O')
        {
            if(cc == 'E')
            {
                int c1;
                scanf("%d", &c1);
                printf("%d\n", Get(c1));

            }else if(cc == 'I')
            {
                int c1, c2;
                scanf("%d%d", &c1, &c2);
                Link(c1, c2);
            }

        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/canhelove/article/details/88382915