hdu 2874 Connections between cities(st&rmq LCA)
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11964 Accepted Submission(s): 2786
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Hint
Huge input, scanf recommended.
题目大意:
输入n个节点,m条边,q个询问
接着输入i j k,表示i和j城市相连,路长为k
如果两个城市不能到达则输出Not connected,否则输出两个城市之间的距离
题解:
最近公共祖先+并查集
关键是需要把那些分开的树建立起关联,
所以弄个虚拟的0节点,把这些树的根都连在一起
#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std;
const int N=;
const int M=;
int tot,cnt,n,m,q,s,t;
int head[N],team[N];
int ver[*N]; //ver:保存遍历的节点序列,长度为2n-1,从下标1开始保存
int R[*N]; // R:和遍历序列对应的节点深度数组,长度为2n-1,从下标1开始保存
int first[N]; //first:每个节点在遍历序列中第一次出现的位置
int dir[N]; //dir:保存每个点到树根的距离,很多问题中树边都有权值,会询问两点间的距离,如果树边没权值,相当于权值为1
int dp[*N][M]; struct edge
{
int u,v,w,next;
}e[*N]; void dfs(int u ,int fa,int dep)
{
ver[++tot]=u;
R[tot] = dep;
first[u]=tot;
for(int k=head[u]; k!=-; k=e[k].next)
{
int v=e[k].v, w=e[k].w;
if (v==fa) continue;
dir[v]=dir[u]+w;
dfs(v,u,dep+);
ver[++tot]=u;
R[tot]=dep;
}
} void ST(int n)
{
for(int i=; i<=n; i++) dp[i][] = i;
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
int a = dp[i][j-], b = dp[i+(<<(j-))][j-];
dp[i][j] = R[a]<R[b]?a:b;
}
}
}
int RMQ(int l,int r)
{
int k=(int)(log((double)(r-l+))/log(2.0));
int a = dp[l][k], b = dp[r-(<<k)+][k]; //保存的是编号
return R[a]<R[b]?a:b;
}
int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}
void addedge(int u,int v,int w)
{
e[++cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int findteam(int k)
{
if (team[k]==k) return k;
else return team[k]=findteam(team[k]);
}
int main()
{ while(~scanf("%d%d%d",&n,&m,&q))
{
memset(head,-,sizeof(head));
for(int i=;i<=n;i++) team[i]=i; //并查集
tot=; cnt=;
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z);
addedge(y,x,z);
int fx=findteam(x);
int fy=findteam(y);
if (fx!=fy) team[fy]=fx;
}
for(int i=;i<=n;i++)
if (team[i]==i) //最关键的是给那些分开的树连一个0节点,那样就变成了一棵树
{
addedge(i,,);
addedge(,i,);
}
dir[]=;
dfs(,-,);
ST(*n-);
for(;q>;q--)
{
scanf("%d%d",&s,&t);
int croot=LCA(s,t);
if (croot==) printf("Not connected\n");
else printf("%d\n",dir[s]+dir[t]-*dir[croot]);
}
}
return ;
}
hdu 2874 Connections between cities(st&rmq LCA)的更多相关文章
- 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(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 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...
- HDU 2874 Connections between cities(LCA离线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: 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 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 ...
随机推荐
- 【第七章】 springboot + retrofit
retrofit:一套RESTful架构的Android(Java)客户端实现. 好处: 基于注解 提供JSON to POJO,POJO to JSON,网络请求(POST,GET,PUT,DELE ...
- NS3 利用Gnuplot生成拥塞窗口例子fifth.cc的png图像
参考链接:一个ns-3的Gnuplot例子 命令: (1)首先将fifth.cc拷贝到scratch目录下(由于环境变量的因素,./waf编译只对scratch目录下的文件有效,也可以忽略此步,直接. ...
- 深蓝色 --ppt
Deep Learning of Binary Hash Codes for Fast Image Retrieval [Paper] [Code-Caffe] 1. 摘要 针对图像检索问题,提出简单 ...
- HTML5 Plus 拍照或者相册选择图片上传
HBuilder+HTML5 Plus+MUI实现拍照或者相册选择图片上传,利用HTML5 Plus的Camera.Gallery.IO.Storage和Uploader来实现手机APP拍照或者从相册 ...
- miRNA几大常用的数据库
1.miRbasehttp://www.mirbase.org/2.miRDBhttp://www.mirdb.org/miRDB/policy.html3.miRandahttp://www.mic ...
- BZOJ 2339 【HNOI2011】 卡农
题目链接:卡农 听说这道题是经典题? 首先明确一下题意(我在这里纠结了好久):有\(n\)个数,要求你选出\(m\)个不同的子集,使得每个数都出现了偶数次.无先后顺序. 这道题就是一道数学题.显然我们 ...
- vue router菜单 存在点哪个但还是会显示active
<router-link to="/" exact>Home</router-link> <router-link to="/add&quo ...
- 使用R的数据库查询
JS 很多方法可以用R查询数据.这篇文章展示了三种最常见的方法: 运用 DBI 使用dplyr语法 使用R note book 背景 最近的一些软件包改进可以更轻松地将数据库与R一起使用.下面的查询示 ...
- tp3.x和tp 5的区别
由于TP5.0是一个全新的颠覆重构版本,所以现在面试很多面试官喜欢问TP3.2和TP5之间的区别,那他们之间到底有哪些区别呢?一.目录 TP5目录 二.需要摒弃的 3.X 旧思想 模型的变动 ...
- Python BeautifulSoup的使用
2017-07-24 22:39:14 Python3 中的beautifulsoup引入的包是bs4 import requests from bs4 import * r = requests.g ...