题目链接 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. Virtual Friends HDU - 3172 (并查集+秩+map)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  2. Nordic Collegiate Programming Contest 2015​ D. Disastrous Downtime

    You're investigating what happened when one of your computer systems recently broke down. So far you ...

  3. Linux学习-systemctl 针对 service 类型的配置文件

    systemctl 配置文件相关目录简介 现在我们知道服务的管理是透过 systemd,而 systemd 的配置文件大部分放置于 /usr/lib/systemd/system/ 目录内. 该目录的 ...

  4. UVa 11695 树的直径 Flight Planning

    题意: 给出一棵树,删除一条边再添加一条边,求新树的最短的直径. 分析: 因为n比较小(n ≤ 2500),所以可以枚举删除的边,分裂成两棵树,然后有这么一个结论: 合并两棵树后得到的新树的最短直径为 ...

  5. 20188472 https://www.cnblogs.com/chenzg90826/

    我是一名学计算机的大一学生,对学计算机比较感兴趣,但是对于计算机的了解程度还不够深,所以我在这方面还只是一个初学者.经过了一个学期对计算机和编程语言的学习,我觉得要真正的学好这门专业真的还要更努力.在 ...

  6. day04 装饰器 迭代器&生成器 Json & pickle 数据序列化 内置函数

    回顾下上次的内容 转码过程: 先decode  为 Unicode(万国码 ) 然后encode 成需要的格式     3.0 默认是Unicode  不是UTF-8 所以不需要指定  如果非要转为U ...

  7. Scala学习-02-方法

    算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式  :  a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...

  8. [POJ 1007] DNA Sorting C++解题

        DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77786   Accepted: 31201 ...

  9. STL学习笔记1--vector

    C++STL(Standard Template Library)标准模板库是通用类模板和算法的集合.包含一些标准的数据结构的实现如queues(队列),lists(链表),stacks(栈)等.ST ...

  10. 大数据学习——spark运营案例

    iplocation需求 在互联网中,我们经常会见到城市热点图这样的报表数据,例如在百度统计中,会统计今年的热门旅游城市.热门报考学校等,会将这样的信息显示在热点图中. 因此,我们需要通过日志信息(运 ...