View Code of Problem 1047

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

int father[N];
int dis[N];

int find(int x)
{
    if(father[x]==x)
        return father[x];

    int root = find(father[x]);
    dis[x]+=dis[father[x]];
    return father[x] = root;
}

int ads(int x)
{
    return x<0?-x:x;
}

int main()
{
    int t,x,y,i,j,n;
    char str[10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i = 0; i<=n; i++)
        {
            father[i] = i;
            dis[i] = 0;
        }
        while(scanf("%s",str))
        {
            if(str[0]=='O')
                break;
            if(str[0]=='E')
            {
                scanf("%d",&x);
                find(x);
                printf("%d\n",dis[x]);
            }
            if(str[0]=='I')
            {
                scanf("%d%d",&x,&y);
                father[x]=y;
                dis[x]=ads(x-y)%1000;
            }
        }
    }

    return 0;
}

Double click to view unformatted code.


Back to problem 1047