题目链接 Connections between cities

LCA的模板题啦。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i,n)        for(int i(0); i <  (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int N = + ;
const int A = + ; int father[N], List[N], deep[N], E[N], X[N], H[N], V[N];
int f[N][A], g[N][A];
bool v[N];
int n, m, q, x, y, z;
int et; inline void Eddedge(int a, int b, int c){
E[++et] = b, X[et] = H[a], H[a] = et, V[et] = c;
E[++et] = a, X[et] = H[b], H[b] = et, V[et] = c;
} int getfather(int x){ return father[x] ? father[x] = getfather(father[x]) : x; } int Calc(int a, int b){
int ret = ; if (deep[a] < deep[b]) swap(a, b);
for (int i = , delta = deep[a] - deep[b]; delta; delta >>= , ++i)
if (delta & ) ret += g[a][i], a = f[a][i]; if (a == b) return ret; dec(i, , ) if (f[a][i] != f[b][i]){
ret += g[a][i];
ret += g[b][i];
a = f[a][i];
b = f[b][i];
} ret += g[a][];
ret += g[b][]; return ret;
} int main(){ while (~scanf("%d%d%d", &n, &m, &q)){ et = ;
memset(father, , sizeof father);
memset(H, , sizeof H); rep(i, , m){
scanf("%d%d%d", &x, &y, &z);Eddedge(x, y, z);
int fa = getfather(x), fb = getfather(y);
father[fa] = fb;
} int l = , r = , now = ;
memset(List, , sizeof List);
memset(deep, , sizeof deep);
memset(v, false , sizeof v);
memset(f, , sizeof f);
memset(g, , sizeof g); rep(p, , n) if (!v[p]){
for (l = , r = , v[p] = true, List[] = p; l <= r; ++l){
now = List[l];
for_edge(i, now) if (!v[E[i]]){
v[E[i]] = true;
f[E[i]][] = now;
g[E[i]][] = V[i];
deep[E[i]] = deep[now] + ; for (int j = ; f[E[i]][j]; ++j)
f[E[i]][j + ] = f[f[E[i]][j]][j],
g[E[i]][j + ] = g[E[i]][j] + g[f[E[i]][j]][j]; List[++r] = E[i];
}
}
} while (q--){
scanf("%d%d", &x, &y);
int fa = getfather(x), fb = getfather(y);
if (fa != fb) puts("Not connected"); else printf("%d\n", Calc(x, y));
} } return ; }

HDU 2874 Connections between cities(LCA)的更多相关文章

  1. HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...

  2. HDU 2874 Connections between cities(LCA离线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之 ...

  3. HDU 2874 Connections between cities(LCA+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2874 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...

  4. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. 【HDU 2874】Connections between cities(LCA)

    dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #in ...

  6. Connections between cities(LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description After World War X, a lot ...

  7. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  8. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  9. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. 在使用sql语句的一些注意事项(sql语句)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/4925483.html ①如果插入字段包含对应的表的所有字段, ...

  2. Yii2 AR模型搜索数据条数不对,AR模型默认去重

    最近在做Yii2的项目时, 发现了一个yii2 自带的Ar模型会自动对搜索出来的字段去重. 默认去重字段: id,  其他字段暂没发现 1. 例如: public function fields { ...

  3. 使用IAR在开发nordic问题记录

    使用IAR在开发nordic的sdk的时候,官方有一段话*****Note for IAR 8 users:(Libraries for IAR 8 require wchar_t to be of ...

  4. 利用PowerDesigner逆向工程导出PDM模型及生成文档

    原文:利用PowerDesigner逆向工程导出PDM模型及生成文档 最近需要对老项目进行重构优化,由于项目都是好几年前的,相关设计资料很不全,最基本的数据库设计文档都没有,只能利用PowerDesi ...

  5. 更改activity切换方式

    overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...

  6. java中,为什么char类型数组可以直接用数组名打印,打印结果居然不是地址值!

    char类型的数组就相当于一个字符串. 因为输出流System.out是PrintStream对象,PrintStream有多个重载的println方法,其中一个就是public void print ...

  7. 我的PC必装软件

    我的PC必装软件 TeXLive LaTeX套装 WinEdt LaTeX编辑器 PyCharm Python IDE Inkscape 矢量图制作,我根本不会用,但是常用来把SVG转成*.tex文件 ...

  8. PHP魔术函数、魔术常量、预定义常量

    一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...

  9. Linux Shell系列教程之(九)Shell判断 if else 用法

    本文是Linux Shell系列教程的第(九)篇,更多shell教程请看:Linux Shell系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.今天就给大家介绍下Shel ...

  10. 【bzoj4247】挂饰 背包dp

    题目描述 JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直接挂在手机上, ...