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基础 __add__,__sub__ 两个类的实例相互加减
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- JSON类库Jackson与JSON-lib性能对比[转]
Jackson:http://jackson.codehaus.org/ JSON-lib:http://json-lib.sourceforge.net/ Gson:http://code.goog ...
- CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...
- Java ServletContext详解
转载: ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而servletConte ...
- Ubuntu 14.04下 Java通用安装方法
参考: 解决Floodlight1.2+Mininet问题及使用安装 Ubuntu下安装JDK1.7图文详解 Ubuntu 14.04下 Java通用安装方法 1.到oracle官网下下载对应jdk包 ...
- 关于ArrayList.clear()与=null以及new ArrayList<E>()
ArrayList是常用到的JCF类,用来保存类型相同的一组对象,并通过下标来快速访问指定对象.今天讨论的是当我们使用完ArrayList后应该选择怎样合适的处理方式. 这里现在有三种方式如下: 1. ...
- 非[无]root权限 服务器 下安装perl以及perl模块--转载
转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...
- Could NOT find Bullet (missing: BULLET_DYNAMICS_LIBRARY BULLET_COLLISION_LIBRARY BULLET_MATH_LIBRARY BULLET_SOFTBODY_LIBRARY BULLET_INCLUDE_DIR)
rosdep where-defined bullet sudo apt-get install libbullet-dev
- Jmeter工具做性能测试 常见的错误汇总
在Win机器上用Jmeter做性能测试,汇总下我自身遇到的错误和解决方案 java.net.BindException: Address already in use: JVM_Bind 原因分析:压 ...
- SQL 常用的命令
--修改表名 --格式:SP_RENAME TABLENAME,NEWTABLENAME SP_RENAME TABLENAME,NEWTABLENAME --只能对表,不能对临时表 --修改字段名 ...