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 ...
随机推荐
- Eclipse中离线安装ADT插件详细教程
在搭建Android开发环境的时候,我们需要为Eclipse安装ADT(Android Development Tools)插件,这个插件可以为用户提供一个强大的Android集成开发环境.通过给Ec ...
- 51nod 1183 编辑距离
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183. 题意不再赘述. 分析:大概和LCS差不多的吧 但是我用LCS ...
- 抽象类的继承,接口的实现,接口类型数组的使用,根据instanceof判断(返回)是否该是哪一个类型,类型的强转.
总觉得之前第2处有点问题,果然. 还需要instanceof判定一下,然后还需要把数组Animal[]转为Pet的才有方法play()~~~!
- java中new一个对象和对象=null有什么区别
原创:转载请注明出处 今天在写代码时,遇到一个问题,特此进行记录. for (ProfileDto profileDto : profile) { // Profile resP ...
- HDU 3466 Proud Merchants(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家 ...
- 通过gevent实现【单线程】下的多socket并发
server import sys import socket import time import gevent from gevent import socket,monkey monkey.pa ...
- python datetime模块来获取当前的日期和时间
#!/usr/bin/python # -*- coding: UTF- -*- import datetime i = datetime.datetime.now() print ("当前 ...
- Python day20正则表达式和re方法
元字符6个函数以及几个元字符1.'.'通配符2.'^'以什么开头3.'$'以什么结尾4.'*'紧挨着的字符0~∞次5.'+'紧挨着的字符1~∞次6.'?'紧挨的字符0次或1次7.'{}' {0,}== ...
- Beta冲刺三——《WAP团队》
β冲刺第三天 1. 今日完成任务情况以及遇到的问题. ①马麒.杜有海:管理员审核表的进一步完善 ②郝明宇:登录.注册界面的完善 ③马宏伟.周欣:继续完善前端数据借用与后台的连接 ④乌勒扎:登录与注册 ...
- C++指针详解(转)
指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占 ...