How far away ? - HDU 2586 LCA 或水题
来源:互联网 发布:做淘宝运营好吗 编辑:程序博客网 时间:2024/05/08 12:36
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5448 Accepted Submission(s): 2065
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
AC代码如下:
#pragma comment(linker, "/STACK:102400000,102400000")#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;struct node{ int v,dis;};vector<node> G[41010];int parent[35][41010],depth[41010],vis[41010],dis[41010],n,m,T,t;void dfs(int v,int p,int d){ parent[0][v]=p; depth[v]=d; vis[v]=t; int i,len=G[v].size(); for(i=0;i<len;i++) if(vis[G[v][i].v]!=t) { dis[G[v][i].v]=dis[v]+G[v][i].dis; dfs(G[v][i].v,v,d+1); }}void init(int V){ int v,k; dfs(1,-1,0); for(k=0;k<30;k++) for(v=1;v<=V;v++) if(parent[k][v]<0) parent[k+1][v]=-1; else parent[k+1][v]=parent[k][parent[k][v]];}int lca(int u,int v){ int k; if(depth[u]>depth[v]) swap(u,v); for(k=0;k<30;k++) if((depth[v]-depth[u])>>k &1) v=parent[k][v]; if(u==v) return u; for(k=29;k>=0;k--) if(parent[k][u]!=parent[k][v]) { u=parent[k][u]; v=parent[k][v]; } return parent[0][u];}int main(){ int u,v,i,j,k,d; scanf("%d",&T); node A; for(t=1;t<=T;t++) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) G[i].clear(); for(i=1;i<n;i++) { scanf("%d%d%d",&u,&v,&d); A.dis=d; A.v=v; G[u].push_back(A); A.v=u; G[v].push_back(A); } init(n); while(m--) { scanf("%d%d",&u,&v); d=lca(u,v); printf("%d\n",dis[u]+dis[v]-2*dis[d]); } //printf("\n"); }}
0 0
- How far away ? - HDU 2586 LCA 或水题
- HDU 2586 How far away? LCA模板
- HDU 2586 - How far away ? (LCA)
- hdu 2586 How far away ?(LCA)
- HDU--2586--How far away ?【LCA】
- HDU 2586 How far away ?(LCA)
- hdu 2586 How far away ?(LCA)
- hdu 2586 How far away ?(LCA)
- HDU - 2586 How far away ?(LCA)
- How far away ? (hdu 2586 LCA)
- hdu 2586 How far away ?(LCA)
- HDU---2586-How far away(LCA)
- HDU 2586 How far away ?(LCA)
- HDU 2586How far away ?(LCA)
- HDU 2586 How far away ?【LCA】
- hdu-2586 How far away ?(LCA)
- HDU 2586 How far away ?(lca)
- hdu 2586 How far away ?【rmq+lca】
- asp.net “非静态字段、方法或属性’System.Web.UI.Page.Response.get‘要求对象引用”
- Design the city - ZOJ 3195 LCA
- USRP2 FPGA debugging
- Java基础视频教程-交通灯管理系统
- sdutoj1500 Message Flood
- How far away ? - HDU 2586 LCA 或水题
- Mac 下GCC更新过程
- hdu1062 Text Reverse (c++输入输出流)
- BZOJ 1013: [JSOI2008]球形空间产生器sphere
- 复制代码如何处理前面的行号
- backtrace函数和addr2line工具 ---抛异常 类实现
- A-HDU4442
- 单链表排序问题、简单易懂
- 数字在排序数组中出现的起始索引号