HDU 2586 (Tarjan)
来源:互联网 发布:ice框架 java 编辑:程序博客网 时间:2024/04/28 10:07
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12442 Accepted Submission(s): 4587
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#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int N = 800010;typedef long long LL;struct node1{ int to, wight, next;}p1[N];struct node2{ int to, index, next;}p2[N];int head1[N], head2[N], visit[N], par[N];LL dist[N], res[N];int cnt1, cnt2;void init();int n, m;void add1(int u,int v,int c);void add2(int u,int v,int c);void tarjan(int u);int ser(int x);int main(){ int t; scanf("%d", &t); while(t--) { init(); scanf("%d %d", &n, &m); init(); int x, y, cost; for(int i=1;i<=n-1;i++) { scanf("%d %d %d", &x, &y, &cost); add1(x,y,cost); add1(y,x,cost); } for(int i=1;i<=m;i++) { scanf("%d %d", &x, &y); add2(x,y,i); add2(y,x,i); } dist[1]=0; tarjan(1); for(int i=1;i<=m;i++) { printf("%I64d\n",res[i]); } } return 0;}void init(){ memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); memset(visit, 0, sizeof(visit)); for(int i=1;i<=n;i++) { par[i]=i; } cnt1=0,cnt2=0; return ;}void add1(int u,int v,int c){ p1[cnt1].to=v; p1[cnt1].wight=c; p1[cnt1].next=head1[u]; head1[u]=cnt1++; return ;}void add2(int u,int v,int c){ p2[cnt2].to=v; p2[cnt2].index=c; p2[cnt2].next=head2[u]; head2[u]=cnt2++; return ;}void tarjan(int u){ visit[u]=1; for(int i=head1[u];i!=-1;i=p1[i].next) { int v=p1[i].to; if(!visit[v]) { dist[v]=dist[u]+p1[i].wight; tarjan(v); par[v]=u; } } for(int i=head2[u];i!=-1;i=p2[i].next) { int v=p2[i].to; if(visit[v]) { res[p2[i].index]=dist[u]+dist[v]-2*dist[ser(v)]; } } return ;}int ser(int x){ int l=x, r=x, j; while(l!=par[l]) l=par[l]; while(r!=l) j=par[r], par[r]=l, r=j; return l;}
0 0
- hdu 2586 Tarjan离线
- HDU 2586 (Tarjan)
- hdu 2586 tarjan 板子
- hdu 2586 lca离线tarjan
- HDU 2586 【LCA-Tarjan-模板】
- LCA之tarjan HDU 2586
- tarjan hdu How far away ? 2586
- HDU 2586 LCA离线算法 tarjan算法
- hdu 2586 How far away ?(Tarjan)
- [hdu 2586](LCA Tarjan算法)
- HDU 2586 How far away ?LCA Tarjan
- hdu 1269 tarjan
- hdu 4547(tarjan LCA)
- hdu 4547(tarjan LCA)
- hdu 4547(tarjan LCA)
- hdu 1269 (tarjan)
- HDU 5266 【tarjan--水】
- HDU 1827 Tarjan
- NoClassDeFoundException-----解决的终极之道
- lsof 查看进程打开那些文件 或者 查看文件给那个进程使用
- NSMutableArray的初始化
- android之旅----源泉
- Swift开发-保持屏幕常亮
- HDU 2586 (Tarjan)
- 关于网页F12后的世界
- 查询 只选择必要数据
- 【杭电oj1232】畅通工程
- Linux每天一点文件权限与目录配置
- eclipse 工程导入到Android Studio 出现的问题Error:Execution failed for task ':app:mergeDebugResources'. >
- Python脚本裁切数据库
- 打造最潮收听体验,树立智能音响标准
- linux + boa + cgi web视频播放器