LA 3027 Corporative Network

算法思路:带权并查集。

但是要注意“权值”的选取,下面的代码采取的方案是:初始的时候保存每个节点到父节点的距离d[i], 当进行一次findset的时候同时压缩路径和依次更新路径上的每个结点的d[i], 使得此时的的d[i]为该节点到根节点的距离,执行findset 后cout<<d[i]就是题目的结果啦。(本菜鸟个人认为这种思想比较经典,有代表性,有助于类似的题目启发思维)

代码来源:刘汝佳 - 信息学竞赛入门经典(强烈推荐,难得的好书)


//模板开始
#include <string>   
#include <vector>   
#include <algorithm>   
#include <iostream>   
#include <sstream>   
#include <fstream>   
#include <map>   
#include <set>   
#include <cstdio>   
#include <cmath>   
#include <cstdlib>   
#include <ctime>
#include<iomanip>
#define SZ(x) (int(x.size()))
using namespace std;

int toInt(string s){
	istringstream sin(s); 
	int t; 
	sin>>t; 
	return t;
}
template<class T> string toString(T x){
	ostringstream sout; 
	sout<<x; 
	return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
	istringstream sin(s); 
	int64 t; 
	sin>>t;
	return t;
}
template<class T> T gcd(T a, T b){ 
	if(a<0) 
		return gcd(-a, b);
	if(b<0) 
		return gcd(a, -b);
	return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)

#define ifs cin

const int maxn = 20000 + 10;
int pa[maxn], d[maxn];

int findset(int x)
{
	if(pa[x] != x)
	{		
		int root = findset(pa[x]);
		d[x] += d[pa[x]];
		return pa[x] = root;
	}
	else
	{
		return x;
	}
}

//LA 3027 Corporative Network

int main()
{
	//ifstream ifs("shuju.txt", ios::in);
	int T;
	ifs>>T;
	while(T--)
	{
		int n, u, v;
		char cmd[9];
		ifs>>n;
		for(int i = 1; i <= n; i++)		//初始化,每个节点单独是一颗树
		{
			pa[i] = i;
			d[i] = 0;
		}
		while(ifs>>cmd && cmd[0] != 'O')
		{
			if(cmd[0] == 'E')
			{
				ifs>>u;
				findset(u);
				cout<<d[u]<<endl;

			}
			if(cmd[0] == 'I')
			{
				ifs>>u>>v;
				pa[u] = v;
				d[u] = abs(u - v) % 1000;
			}
		}
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值