HDU 2874 Connections between cities (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874
题意是给你n个点,m条边(无向),q个询问。接下来m行,每行两个点一个边权,而且这个图不能有环路。然后接下来q行,每行给你两个点,问你这两个点的最短距离是多少,要是不相连,则输出一串英文。
首先想到的是用(二分)倍增LCA,但是这题的坑点是两个点可能不在同一个图中,所以我dfs的时候用block[i]标记这个点属于哪一个图中,要是这个点在同一个图中,答案就是cost[u] + cost[v] - 2*cost[lca(u , v)]。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN = ;
struct data {
int next , to , w;
}edge[MAXN * ];
int head[MAXN] , par[MAXN * ][] , dep[MAXN] , cont , cost[MAXN] , block[MAXN]; void init() {
cont = ;
//memset(par , -1 , sizeof(par));
memset(head , - , sizeof(head));
memset(block , , sizeof(block));
} inline void add(int u , int v , int w) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].w = w;
head[u] = cont++;
} void dfs(int u , int p , int d , int node , int w) {
dep[u] = d;
par[u][] = p;
block[u] = node;
cost[u] = w;
int v;
for(int i = head[u] ; ~i ; i = edge[i].next) {
v = edge[i].to;
if(v == p)
continue;
dfs(v , u , d + , node , w + edge[i].w);
}
} int lca(int u , int v) {
if(dep[u] < dep[v])
swap(u , v);
for(int k = ; k < ; k++) {
if((dep[u] - dep[v]) >> k & ) {
u = par[u][k];
}
}
if(u == v)
return v;
for(int k = ; k >= ; k--) {
if(par[u][k] != par[v][k]) {
u = par[u][k];
v = par[v][k];
}
}
return par[u][];
} int main()
{
int n , m , q , u , v , w;
while(~scanf("%d %d %d" , &n , &m , &q)) {
init();
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &u , &v , &w);
add(u , v , w);
add(v , u , w);
}
int f = ;
for(int i = ; i <= n ; i++) {
if(!block[i]) {
dfs(i , - , , ++f , );
}
}
for(int k = ; k < ; k++) {
for(int i = ; i <= n ; i++) {
if(par[i][k] <= )
par[i][k + ] = par[i][k];
else
par[i][k + ] = par[par[i][k]][k];
}
}
while(q--) {
scanf("%d %d" , &u , &v);
if(block[u] != block[v]) {
printf("Not connected\n");
}
else {
printf("%d\n" , cost[u] + cost[v] - * cost[lca(u , v)]);
}
}
}
}
HDU 2874 Connections between cities (LCA)的更多相关文章
- 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 LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU——2874 Connections between cities
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...
随机推荐
- hdu 4901 The Romantic Hero (dp)
题目链接 题意:给一个数组a,从中选择一些元素,构成两个数组s, t,使s数组里的所有元素异或 等于 t数组里的所有元素 位于,求有多少种构成方式.要求s数组里 的所有的元素的下标 小于 t数组里的所 ...
- poj 1061 青蛙的约会(扩展gcd)
题目链接 题意:两只青蛙从数轴正方向跑,给出各自所在位置, 和数轴长度,和各自一次跳跃的步数,问最少多少步能相遇. 分析:(x+m*t) - (y+n*t) = p * L;(t是跳的次数,L是a青蛙 ...
- HDU 1672 Cuckoo Hashing
Cuckoo Hashing Description One of the most fundamental data structure problems is the dictionary pro ...
- uva10820Send a Table
筛法. 首先使cnt[i]=sqr(n/i),这样cnt[i]就表示gcd(x,y)大于等于i的数对的个数,然后倒序枚举减去gcd大于i的个数就可以得到ans[i].最终得到ans[1]. 这个算法单 ...
- BZOJ2693: jzptab
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2693 题意:同2154 多组数据 题解:按2154再往后转化一下就可以把n,m放到一边儿,然后 ...
- BZOJ 1861 书架
(╯-_-)╯╧╧ 此处为错误代码. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- 《C++ Primer 4th》读书笔记 第7章-函数
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3912413.html
- Data Binding(数据绑定)用户指南
1)介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- 它 ...
- 【剑指offer 面试题23】从上往下打印二叉树
思路: 没啥好说的,BFS. C++: #include <iostream> #include <queue> using namespace std; struct Tre ...
- Web服务器(Apache)虚拟主机的配置
一.定义 所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务. 二.虚拟主机的实现方法有三种: 基于IP的方法,基于主机名的方法和基于端口的法官法. ...