您的位置:首页 > 其它

HDU 2586

2015-10-08 21:17 375 查看

How far away ?

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9563    Accepted Submission(s): 3363
[/b]

[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

[align=left]Sample Input[/align]

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 

[align=left]Sample Output[/align]

10
25
100
100

 

[align=left]Source[/align]
ECJTU 2009 Spring Contest
 

[align=left]Recommend[/align]
lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 3234 2818 

给一颗树,询问两个子树的距离。显然等于子树到根节点的距离减去他们到最近公共祖先的距离。这里用st算法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //不需要申请系统栈
const int N = 40010;
const int M = 25;

int _pow[M];
int tot,head
,ver[2*N],R[2*N],first
,dir
;
int dp[2*N][M]; //这个数组记得开到2*N,因为遍历后序列长度为2*n-1
bool vis
;
struct edge
{
int u,v,w,next;
} e[2*N];

inline void add(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++;
}

void dfs(int u ,int dep)
{
vis[u] = true;
ver[++tot] = u;
first[u] = tot;
R[tot] = dep;
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; //表示u到v的距离
dfs(v,dep+1);
ver[++tot] = u;
R[tot] = dep;
}
}

void ST(int len)
{
int K = (int)(log((double)len) / log(2.0));
for(int i=1; i<=len; i++) dp[i][0] = i;
for(int j=1; j<=K; j++)
for(int i=1; i+_pow[j]-1<=len; i++)
{
int a = dp[i][j-1] , b = dp[i+_pow[j-1]][j-1];
if(R[a] < R[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}

int RMQ(int x ,int y)
{
int K = (int)(log((double)(y-x+1)) / log(2.0));
int a = dp[x][K] , b = dp[y-_pow[K]+1][K];
if(R[a] < R[b]) return a;
else return b;
}

int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}

int main()
{
for(int i=0; i<M; i++) _pow[i] = (1<<i);
int cas;
scanf("%d",&cas);
while(cas--)
{
int n,q,num = 0;
scanf("%d%d",&n,&q);
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,num);
}
tot = 0;
dir[1] = 0;
dfs(1,1);
//
// printf("节点 ");
// for(int i=1; i<=2*n-1; i++) printf("%d ",ver[i]);
// cout << endl;
// printf("深度 ");
// for(int i=1; i<=2*n-1; i++) printf("%d ",R[i]);
// cout << endl;
// printf("首位 ");
// for(int i=0; i<n; i++) printf("%d ",first[i]);
// cout << endl;
// printf("距离 ");
// for(int i=0; i<n; i++) printf("%d ",dir[i]);
// cout << endl;

ST(2*n-1);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
int lca = LCA(u,v);
// printf("lca = %d\n",lca);
printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
}
}
return 0;
}


离线算法tarjan

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 40010;
const int M = 410;

int head
,__head
;
struct edge{
int u,v,w,next;
}e[2*N];
struct ask{
int u,v,lca,next;
}ea[M];
int dir
,fa
,ance
;
bool vis
;

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)];
}
}

int main()
{
int cas,n,q,tot;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&q);
memset(head,-1,sizeof(head));
memset(__head,-1,sizeof(__head));
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);
}
memset(vis,0,sizeof(vis));
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM算法