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 ...
随机推荐
- Python3基础 list str转成list
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- git如何生成指定两个commit之间的补丁
答:git format-patch <base commit id>..<latest commit id> 如git log输出以下内容: commit 2222222 y ...
- LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解
题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢.假如先手必赢,输出所有能必赢的第一步,否则输出0. 思路:显然如果一个X周围两格有X那么肯定 ...
- hdu 2586 How far away ? 倍增求LCA
倍增求LCA LCA函数返回(u,v)两点的最近公共祖先 #include <bits/stdc++.h> using namespace std; *; struct node { in ...
- 51nod 1137 矩阵乘法
基本的矩阵乘法 中间for(int j=0;i<n;i++) //这里写错了 应该是j<n 晚上果然 效率不行 等会早点儿睡 //矩阵乘法 就是 两个矩阵 第一个矩阵的列 等与 第 ...
- [NLP/Attention]关于attention机制在nlp中的应用总结
原文链接: https://blog.csdn.net/qq_41058526/article/details/80578932 attention 总结 参考:注意力机制(Attention Mec ...
- 如何插入sql数据
原:http://blog.csdn.net/Weicleer/article/details/47608289
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- Selenium UI自动化测试 Selenium Automatic Testing
https://www.cnblogs.com/sunada2005/archive/2013/12/22/3486314.html UI Automatic Testing 1. 什么样的项目适合自 ...
- Python day18模块介绍2(使用BASE_DIR修改临时path,os模块)
1.BASE_DIR修改path(别人导入py项目时不会因为绝对路径无法解释) #sys修改环境变量 #使用BASE_DIR将绝对路径改为相对路径 import sys,os BASE_DIR=os. ...