LA 3027 Corporative Network
来源:互联网 发布:mac迅雷怎么用 编辑:程序博客网 时间:2024/05/17 01:51
算法思路:带权并查集。
但是要注意“权值”的选取,下面的代码采取的方案是:初始的时候保存每个节点到父节点的距离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 cinconst 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 Networkint 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;}
- LA - 3027 - Corporative Network
- LA--3027--Corporative Network
- LA 3027 Corporative Network
- LA 3027 - Corporative Network
- LA 3027 Corporative Network
- LA 3027 - Corporative Network
- LA 3027 Corporative Network
- LA 3027 Corporative Network
- LA 3027 - Corporative Network
- Corporative Network(LA 3027)
- LA 3027Corporative Network
- LA 3027 Corporative Network
- LA 3027(p192)----Corporative Network
- LA 3027 Corporative Network / 并查集
- LA 3027 Corporative Network 并查集
- LA 3027 Corporative Network [并查集]
- LA-3027-Corporative Network-更新深度的并查集
- LA 3644 X-Plosives && LA 3027 Corporative Network (并查集入门)
- struts-2.3.12环境搭建
- 查看MySql事务日志
- 为什么要设置session过期时间
- const的作用
- Java中subString和split导致的内存溢出和对策
- LA 3027 Corporative Network
- Makefile例子
- android应用程序fps meter[帧数显示]的分析 —— 浅谈root的风险 (1)
- 黑马程序员:Java基础总结----类 File
- 关于FID的解释非常经典藏
- Memcached源码分析(线程模型)
- java合并一维数组
- LINUX内核中的xx_initcall初始化标号
- 黑马程序员:Java基础总结----递归的应用&File流的应用