HDU 2874 Connections between cities(LCA)
题目链接 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)的更多相关文章
- HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...
- HDU 2874 Connections between cities(LCA离线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之 ...
- HDU 2874 Connections between cities(LCA+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2874 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...
- hdu 2874 Connections between cities(st&rmq LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 【HDU 2874】Connections between cities(LCA)
dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #in ...
- Connections between cities(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description After World War X, a lot ...
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2874 Connections between cities(LCA Tarjan)
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- phpstorm 工具使用技巧(持续补充中。。。)
phpstorm 工具使用技巧(持续补充中...) 一.phpstorm大小写切换 1.选择要转换的目标字符串: //普通商家,普通折扣默认值'COMMON_DISCOUNT'=>10.00, ...
- 【kindle】【转发】kindle链接WIFI自动断开问题
在电脑上新建一个新文件,名为“WIFI_NO_NET_PROBE”,同时把后缀名删掉,让它变成一个无格式文件.Kindle 连接电脑,把新建的文件放进Kindle的根目录,断开Kindle之后重启Ki ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- STM8S与IAR程序常用错误
一.IAR中的重复定义问题 在自己写头文件时,要记得将常量定义在.c文件中,如果将常量定义在.h文件中,当在main.c或者其他地方包含该头文件时,会将头文件中的常量定义包 含到main.c中,同时, ...
- LA 7049 Galaxy 枚举
题意: \(x\)轴上有\(n\)个质量为\(1\)的点,他们的坐标分别为\(x_i\). 质心的坐标为\(\frac{\sum{x_i}} {n}\) 转动惯量为\(\sum{d_i^2}\),其中 ...
- Python之多线程与多进程(一)
多线程 多线程是程序在同样的上下文中同时运行多条线程的能力.这些线程共享同一个进程的资源,可以在并发模式(单核处理器)或并行模式(多核处理器)下执行多个任务 多线程有以下几个优点: 持续响应:在单线程 ...
- nginx的常用负载均衡算法,分别是
随机分配,hash一致性分配,最小连接数分配,主备分配 随机,轮训,一致性哈希,主备,https://blog.csdn.net/liu88010988/article/details/5154741 ...
- Python 综合应用小项目一
数据库报错重连机制 利用异常捕获来获取mysql断开的报错,然后再重连 import MySQLdb as mysql class DB: def __init__(self,host,user,pa ...
- loj2031 「SDOI2016」数字配对
跑最大费用最大流,注意到每次 spfa 出来的 cost 一定是越来越少的,啥时小于 \(0\) 了就停了吧. #include <iostream> #include <cstri ...
- SSM网上商城项目 01
开发环境与技术选型 操作系统:win7 IDE:Eclipse neno JDK:1.8 数据库:mysql5.6 Dao层:mybatis.数据库连接池(德鲁伊druid) 缓存:redis3.0. ...