hdu2586【How far away ?】
来源:互联网 发布:原笔迹手写软件 编辑:程序博客网 时间:2024/05/01 06:08
[tarjian离线求lca]
hdu2586【How far away ?】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13605 Accepted Submission(s): 5101
【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.
【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 Input23 21 2 103 1 151 22 32 21 2 1001 22 1Sample Output1025100100Source ECJTU 2009 Spring Contest
【题目大意】:就是tarjian离线求LCA的裸体。。 但是这道题我卡了很久,,原因就是罪恶的HDU,,,他的M不是200,,,只是不止200,我把M也开成40000才过,,,,事实证明,用OJ必须随意开数组,,,kao.
友链:[这个lca讲的不错](http://blog.csdn.net/hnust_xiehonghao/article/details/9109295)
【代码】
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define cle(x) memset(x,0,sizeof(x))
const int N=40010,M=210;
struct ii{
int to,ne,v;
ii(int to=0,int ne=0,int v=0):to(to),ne(ne),v(v){ }
}ed[N*2];
struct tt{
int to,ne;
tt(int to =0,int ne=0):to(to),ne(ne){ }
}qu[N*2];
int head[N],first[N],num[N],fa[N],ufa[N];
bool vis[N];
inline int readin(){
int res=0;
char ch;
while((ch = getchar())>'9'||ch<'0');
res=ch-'0';
while((ch = getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return res;
}
int lca[M],lca_dis[M],dis[N];
int find_fa(int x){return (x==ufa[x])?x:ufa[x]=find_fa(ufa[x]);}//查找的是ufa,,不是fa,,!!!
void tarjian(int u,int w){
vis[u]=true;
dis[u]=w;
ufa[u]=u;
for(int i = head[u];i;i=ed[i].ne){
int v=ed[i].to;
if(fa[u]==v)continue;
fa[v]=u;
tarjian(v,w+ed[i].v);
ufa[v]=u;
}
for(int i = first[u];i;i=qu[i].ne){
int v=qu[i].to;
if(!vis[v])continue;
int p=num[i];
lca[p]=find_fa(v);
lca_dis[p]=dis[u]+dis[v]-dis[lca[p]]*2;
}
}
int main(){
freopen("hdu.in","r",stdin);
freopen("hdu.out","w",stdout);
int T=readin();
for(int o = 1; o<= T; o++){
int n=readin(),m=readin();
cle(ed),cle(qu),cle(head),cle(first),cle(num),cle(lca),cle(lca_dis),cle(fa),cle(ufa);
cle(dis),cle(vis);//数组记得清空完全。。
for(int i=1;i<=n-1;i++){
int x=readin(),y=readin(),z=readin();
ed[i*2-1]=ii(y,head[x],z);
head[x]=i*2-1;
ed[i*2]=ii(x,head[y],z);
head[y]=i*2;
}
for(int i=1; i<=m;i++){
int x=readin(),y=readin();
qu[i*2-1]=tt(y,first[x]);
first[x]=i*2-1;
qu[i*2]=tt(x,first[y]);
first[y]=i*2;
num[i*2-1]=num[i*2]=i;//存储是第几个询问,必须双向。
}
fa[1]=1;//这个并不知道是否有问题,,,记得实验一次!!!!
tarjian(1,0);
for(int i=1;i<=m;i++)
printf("%d\n",lca_dis[i]);
}
}
- HDU2586 How far away ?
- HDU2586 How far away ?
- hdu2586【How far away ?】
- HDU2586 How far away ?
- hdu2586 How far away
- hdu2586 How far away? LCA
- hdu2586(How far away ?)
- hdu2586(How far away ?)
- hdu2586 step5.3.4.cpp How far away ?
- [LCA Problem] hdu2586 How far away ?
- HDU2586 How far away ?(LCA模板题)
- hdu2586 How far away ?(LCA->RMQ)
- 离线LCA hdu2586 How far away ?
- 倍增法LCA hdu2586 How far away ?
- HDU2586 How far away ? LCA_Tarjan解法
- 【HDU2586】How far away ?【离线】【TarjanLCA】
- hdu2586 How far away ? (LCA离线)
- hdu2586 How far away ?--tarjan & LCA
- sublime 使用问题、快捷键
- CS0234: 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)
- 用Firebird自带的工具管理数据库
- 局部更新商品属性的标准方法
- C++学习摘要2
- hdu2586【How far away ?】
- Stack实现
- 基于内嵌浏览器内核(如XULRunner、CEF)来开发桌面软件
- 基于java后台,angular1为框架的admin管理系统完整项目体验报告
- Linux 防火墙开放特定端口 (iptables)
- 基于TrustZone和E-Cash的移动匿名支付方案 (ISC 2016)PPT:AEP-M: Practical Anonymous E-Payment for Mobile Device
- 数学在机器学习中的作用给的评述
- MYSQL:Lock wait timeout exceeded; try restarting transaction ;ERROR 1205
- python os.stat()