poj 1962 Corporative Network

题目链接:http://poj.org/problem?id=1962

思路:每个集合中用根节点标记这个集合,每个点到根节点的距离。

code:

<span style="font-size:18px;">#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>

using namespace std;

const int maxn=20005;

int pa[maxn],d[maxn];

int findset(int x)   //找出x节点到根节点的距离
{
    if(pa[x]!=x)
    {
        int root=findset(pa[x]);
        d[x]+=d[pa[x]];       //更新x节点的距离
        return pa[x]=root;
    }
    else return x;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        char str[10];
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            pa[i]=i;
            d[i]=0;
        }
        while(scanf("%s",str))
        {
            if(str[0]=='O') break;
            if(str[0]=='E')
            {
                int x;
                scanf("%d",&x);
                findset(x);
                printf("%d\n",d[x]);
            }
            if(str[0]=='I')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                pa[x]=y;
                d[x]=abs(x-y)%1000;
            }
        }
    }
    return 0;
}
</span>
时间: 2024-10-09 15:54:18

poj 1962 Corporative Network的相关文章

poj 1962 Corporative Network(带权并查集)

题意: 在n个站点间建电线:两种操作: I a b表示以a为中心站点建线: E a表示查询以a站点为中心,相连的电线总长度: 思路: 带权并查集:中心站点就是父亲,电线长度为权值: #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int fa[500010],w[500010]; int t,n,m; char ch[5];

3027 - Corporative Network

3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<stack> 7 #include<set> 8 #inc

POJ1962:Corporative Network(并查集)

Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the se

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

UVa 1329 - Corporative Network Union Find题解

UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用.主要使用路径压缩.甚至不需要合并树了,因为没有重复的连线和修改单亲节点的操作. 郁闷的就是不太熟悉这个Oj系统,居然使用库中的abs就会WA,自己写了个abs小函数就过了. 题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075 #include <s

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

[LA] 3027 - Corporative Network [并查集]

A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the