个人一直觉得kuangbin大牛的LCA板子 感觉有点不太好用,于是在这个大牛的博客帮助下,更换了自己的板子;
http://www.cnblogs.com/scau20110726/archive/2013/05/26/3100265.html
hdu 2586,
给一颗树 ,每一条边有权值,然后m次询问,每一次询问两个点:i,j。 输出 i,j的最短距离。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 40010;
const int M = 410;
int head[N],__head[N]; //同理 一个链式存 树,另外一个链式存问题
struct edge{
int u,v,w,next;
}e[2*N];
struct ask{
int u,v,lca,next;
}ea[M];
int dir[N],fa[N],ance[N];
bool vis[N];
inline void add_edge(int u,int v,int w,int &k)
{
e[k].u = u; e[k].v = v; e[k].w = w;
e[k].next = head[u]; head[u] = k++;
u = u^v; v = u^v; u = u^v;
e[k].u = u; e[k].v = v; e[k].w = w;
e[k].next = head[u]; head[u] = k++;
}
inline void add_ask(int u ,int v ,int &k)
{
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
ea[k].next = __head[u]; __head[u] = k++;
u = u^v; v = u^v; u = u^v;
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
ea[k].next = __head[u]; __head[u] = k++;
}
int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
void Union(int u ,int v)
{
fa[v] = fa[u]; //可写为 fa[Find(v)] = fa[u];
}
void Tarjan(int u)
{
vis[u] = true;
ance[u] = fa[u] = u; //课写为 ance[Find(u)] = fa[u] = u;
for(int k=head[u]; k!=-1; k=e[k].next)
if( !vis[e[k].v] )
{
int v = e[k].v , w = e[k].w;
dir[v] = dir[u] + w;
Tarjan(v);
Union(u,v);
//ance[Find(u)] = u; //可写为ance[u] = u; //甚至不要这个语句都行
}
for(int k=__head[u]; k!=-1; k=ea[k].next)
if( vis[ea[k].v] )
{
int v = ea[k].v;
ea[k].lca = ea[k^1].lca = ance[Find(v)];
}
}
void init(){
memset(head,-1,sizeof(head));
memset(__head,-1,sizeof(__head));
memset(vis,0,sizeof(vis));
}
int main()
{
int cas,n,q,tot;
scanf("%d",&cas);
while(cas--)
{
init();
scanf("%d%d",&n,&q);
tot=0;
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w,tot);
}
tot = 0;
for(int i=0; i<q; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_ask(u,v,tot);
}
dir[1] = 0;
Tarjan(1);
for(int i=0; i<q; i++)
{
int s = i * 2 , u = ea[s].u , v = ea[s].v , lca = ea[s].lca;
printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
}
}
return 0;
}
poj 1986
题意:给一棵树,询问m次,也是输出他们的最短距离,这题貌似他们判断什么连通不连通,询问点相同不相同,但是用了这个模板啥都不用判断,直接过。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 40010;
const int M = 40010;
int head[N],__head[N]; //同理 一个链式存 树,另外一个链式存问题
struct edge{
int u,v,w,next;
}e[2*N];
struct ask{
int u,v,lca,next;
}ea[M];
int dir[N],fa[N],ance[N];
bool vis[N];
inline void add_edge(int u,int v,int w,int &k)
{
e[k].u = u; e[k].v = v; e[k].w = w;
e[k].next = head[u]; head[u] = k++;
u = u^v; v = u^v; u = u^v;
e[k].u = u; e[k].v = v; e[k].w = w;
e[k].next = head[u]; head[u] = k++;
}
inline void add_ask(int u ,int v ,int &k)
{
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
ea[k].next = __head[u]; __head[u] = k++;
u = u^v; v = u^v; u = u^v;
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
ea[k].next = __head[u]; __head[u] = k++;
}
int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
void Union(int u ,int v)
{
fa[v] = fa[u]; //可写为 fa[Find(v)] = fa[u];
}
void Tarjan(int u)
{
vis[u] = true;
ance[u] = fa[u] = u; //课写为 ance[Find(u)] = fa[u] = u;
for(int k=head[u]; k!=-1; k=e[k].next)
if( !vis[e[k].v] )
{
int v = e[k].v , w = e[k].w;
dir[v] = dir[u] + w;
Tarjan(v);
Union(u,v);
//ance[Find(u)] = u; //可写为ance[u] = u; //甚至不要这个语句都行
}
for(int k=__head[u]; k!=-1; k=ea[k].next)
if( vis[ea[k].v] )
{
int v = ea[k].v;
ea[k].lca = ea[k^1].lca = ance[Find(v)];
}
}
void init(){
memset(head,-1,sizeof(head));
memset(__head,-1,sizeof(__head));
memset(vis,0,sizeof(vis));
}
int main()
{
int n,m;
char ch[5];
while(~scanf("%d %d",&n,&m)){
init();
int tot=0;
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d %d %d %s",&u,&v,&w,ch);
add_edge(u,v,w,tot);
}
tot=0;
int q;
scanf("%d",&q);
for(int i=0;i<q;i++){
int u,v;
scanf("%d %d",&u,&v);
add_ask(u,v,tot);
}
dir[1]=0;
Tarjan(1);
for(int i=0;i<q;i++){
int s=2*i;
int a=ea[s].u,b=ea[s].v,c=ea[s].lca;
// printf("%d %d %d\n",a,b,c);
int ans=dir[a]+dir[b]-2*dir[c];
printf("%d\n",ans);
}
}
return 0;
}