HDU 2586 How far away ? LCA离线tarjan思想
来源:互联网 发布:盛势网络剧哪里可以看 编辑:程序博客网 时间:2024/06/06 01:09
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15746 Accepted Submission(s): 5982
Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer T(T<=10), indicating the number of test cases.
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.
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
23 21 2 103 1 151 22 32 21 2 1001 22 1
Sample Output
1025100100
解题思路:
1,输入的图和查询的点都告诉你了,离线算法就可以做了
2,tarjan经常用于LCA的离线算法,他本来是一个求联通分量的算法
#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 40005 ;struct Edge { int form,to,dist;};vector<Edge> edges ;vector<int>G[maxn] ;void add_edge(int from,int to,int dist) { edges.push_back((Edge){from,to,dist}) ; int mm = edges.size() ; G[from].push_back(mm-1) ;}vector<Edge> edges2 ;vector<int>G2[maxn] ;void add_edge2(int from,int to,int dist=0) { edges2.push_back((Edge){from,to,dist}) ; int mm = edges2.size() ; G2[from].push_back(mm-1) ;}int f[maxn] ;bool vis[maxn] ;int len[maxn] ;int find(int x){ return x==f[x]?x:f[x]=find(f[x]);}void LCA(int u){ f[u] = u ; vis[u] = true ; for(int i=0;i<G[u].size();i++){ if(!vis[edges[G[u][i]].to]){ len[edges[G[u][i]].to] = len[u]+edges[G[u][i]].dist; LCA(edges[G[u][i]].to) ; f[edges[G[u][i]].to] = u ; } } for(int i=0;i<G2[u].size();i++){ if(vis[edges2[G2[u][i]].to]){///这里给双向边都存了结果,得益于神奇的^1运算 edges2[G2[u][i]^1].dist = edges2[G2[u][i]].dist = len[edges2[G2[u][i]].to]+len[u]-2*len[find(edges2[G2[u][i]].to)]; } }}int main() { int t ; scanf("%d",&t) ; while(t--) { memset(G,0,sizeof(G)) ; memset(G2,0,sizeof(G2)) ; memset(len,0,sizeof(len)) ; memset(vis,false,sizeof(vis)) ; edges.clear() ; edges2.clear() ; int n,m; scanf("%d%d",&n,&m) ; for(int i=0; i<n-1; i++) { int u,v,w ; scanf("%d%d%d",&u,&v,&w) ; add_edge(u,v,w) ; add_edge(v,u,w) ; } for(int i=0; i<m; i++) { int u,v ; scanf("%d%d",&u,&v) ; add_edge2(u,v) ; add_edge2(v,u) ; } LCA(1) ; for(int i=0; i<m; i++) { printf("%d\n",edges2[2*i].dist) ; } } return 0;}
阅读全文
0 0
- HDU 2586 How far away ? LCA离线tarjan思想
- HDU 2586 How far away? LCA 离线tarjan
- HDU 2586 How far away ? (离线LCA Tarjan算法模板)
- hdu 2586 How far away ?(离线tarjan求LCA)
- hdu 2586 How far away?(LCA离线Tarjan算法)
- 【Lca 离线Tarjan算法】hdu 2586 How far away ?
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- hdu 2586 How far away ? (LCA 离线tarjan)
- HDU 2586 How far away ?LCA Tarjan
- HDU 2586 How far away ? 离线LCA
- hdoj 2586 How far away ? 【Tarjan离线LCA】
- HDU OJ 2586 How far away ?【LCA的Tarjan离线算法】
- HDU 2586 How far away ? LCA的Tarjan离线算法
- HDU ACM 2586 How far away ?LCA->并查集+Tarjan(离线)算法
- HDU 2586 How Far Away?(Tarjan离线算法求lca)
- HDU 2586How far away ? tarjan算法求LCA
- HDU 2586 How far away ? Tarjan 离线最近公共祖先
- HDU 2586 How far away ?(Tarjan离线算法)
- 百度前端作业-01
- 从源码上分析ListView/GridView调用setEmptyView不起作用的原因及解决办法
- 随机梯度下降和批量梯度下降的区别
- Linux运维 -- day04 常用命令
- df命令出现的xvda1的意思
- HDU 2586 How far away ? LCA离线tarjan思想
- 如何限制网页只能在微信内置浏览器中打开?
- MyEclipse不能打开文件问题
- X.805安全体系框架
- 精通比特币
- nginx 拦截请求信息与响应信息并切割日志
- 接口list
- keyup,keydown事件在IOS上无效问题
- win7 64bit下安装Anaconda3-4.4.0-Windows-x86_64报错failed to create anacoda menu