hdu 5286 How far away ? tarjan/lca
How far away ?
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2586
Description
Input
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
Sample Output
10
25
100
100
HINT
题意
题解:
tarjan离线算法
代码:
# include<stdio.h>
# include<string.h>
# define N
# define M
struct node{
int from,to,next,val;
}edge[*N];
struct node1{
int from,to,next,num;
}edge1[*M];
int tol,head[N],head1[N],tol1,father[N],dis[N],LCA[M],n,m;
bool visit[N];
void add(int a,int b,int c)
{
edge[tol].from=a;edge[tol].to=b;edge[tol].next=head[a];edge[tol].val=c;head[a]=tol++;
}
void add1(int a,int b,int c)
{
edge1[tol1].from=a;edge1[tol1].to=b;edge1[tol1].next=head1[a];edge1[tol1].num=c;head1[a]=tol1++;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void tarjan(int u)
{
int j,v;
visit[u]=;
father[u]=u;
//////////////////
for(j=head1[u];j!=-;j=edge1[j].next)
{
v=edge1[j].to;
if(visit[v]) LCA[edge1[j].num]=find(v);
}
//////////////////
for(j=head[u];j!=-;j=edge[j].next)
{
v=edge[j].to;
if(!visit[v])
{
dis[v]=dis[u]+edge[j].val;
tarjan(v);
father[v]=u;
}
}
}
int main()
{
int i,ncase,a,b,c;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&n,&m);
tol=;
memset(head,-,sizeof(head));
for(i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
memset(visit,,sizeof(visit));
tol1=;
memset(head1,-,sizeof(head1));
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
add1(a,b,i);
add1(b,a,i);
}
///LCA是一种离线算法,所以刚开始需要把所有的询问都输入,然后用邻接表进行存储,i表示第i次询问
dis[]=;
tarjan();
for(i=;i<tol1;i+=)
{
a=edge1[i].from;
b=edge1[i].to;
c=edge1[i].num;
printf("%d\n",dis[a]+dis[b]-*dis[LCA[c]]);
}
}
return ;
}
hdu 5286 How far away ? tarjan/lca的更多相关文章
- hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...
- HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 5286 How far away ? lca
题目链接: 题目 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 2586 How far away ? 离线lca模板题
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
随机推荐
- vector容器使用总结 .xml
pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- Building nginx from Sources(从源代码安装nginx)
Building nginx from Sources(从源代码安装nginx) The build is configured using the configure command. 安装用配置 ...
- WCF配置文件全攻略
參考:http://www.cnblogs.com/wayfarer/archive/2007/08/24/867987.html http://www.cnblogs.com/scy251147/a ...
- 【调试】路由器设置不了静态IP -- clwu
办公室的路由器是IP-COM,原来可以设置静态IP 的,但后来不知道为什么比较长一段时间内设置不了,原来设置的静态IP 也不见了.现象是这样 什么都没有. 今天的网络被网管重调整过了,需要重新设置静态 ...
- Node-APN 开源推送服务
Node-APN是一个开放的结合了苹果推送通知的Node.js模块,该源码模块使用简单,反馈服务支持.错误处理,在发送出错时自动重发.遵从苹果的最佳实践. Node-APN(github)
- ef6 code first
http://www.cnblogs.com/Bce-/p/3684643.html http://www.cnblogs.com/Gyoung/tag/Entity%20Framework/ htt ...
- 第三百五十九天 how can I 坚持
在家待了一天,鼓捣了下linux,总算能连上网了,懂得还是少啊. 晚上去华北电力大学跑了会步,十圈,还挺有成就感呢,就是没带手环,哎. 以后学习一定要记笔记,上了这么多年学,都 没学会怎么记笔记,也是 ...
- 第三百五十三天 how can I 坚持
今天买了床被子,凑合盖吧,也不是多好. 下午去了趟华北电力大学,和刘路聊了聊,还是话太多了..不好. 还有买了桶油和大米.. 洗澡,睡觉,一天过得好快.
- SQL SERVER安装提示“安装了 Microsoft Visual Studio 2008 的早期版本
工作共遇到的问题记录: 安装Sql Server 2008 R2时提示错误:“此计算机上安装了 Microsoft Visual Studio 2008 的早期版本.请在安装 SQL Server 2 ...