标签:最近公共祖先
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7072 Accepted Submission(s): 2575
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
#include <cstdio>
#include <iostream>
#include <vector>
const int M = 4e4+5;
using namespace std;
struct node{
int to, val;
};
vector<node > map[M];
vector<int> q[M], num[M];
bool vis[M];
int dis[M], ans[300], fat[M];
int f(int x){
if(x != fat[x]) fat[x] = f(fat[x]);
return fat[x];
}
void Lca(int u){
vis[u] = 1;
for(int i = 0; i < map[u].size(); ++i){
int v = map[u][i].to;
if(!vis[v]){
dis[v] = dis[u]+map[u][i].val;
Lca(v);
fat[v] = u;
}
}
for(int i = 0; i < q[u].size(); ++ i){
int v = q[u][i];
if(vis[v]){
ans[num[u][i]] = dis[u]+dis[v]-2*dis[f(v)];
}
}
}
int main(){
int t, n, m;
//ios::sync_with_stdio(false);
scanf("%d", &t);
while(t --){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++ i){
map[i].clear(); q[i].clear();
fat[i] = i; vis[i] = 0;
}
int a, b, c;
for(int i = 0; i < n-1; ++i){
scanf("%d%d%d", &a, &b, &c);
node temp;
temp.to = a; temp.val = c;
map[b].push_back(temp);
temp.to = b;
map[a].push_back(temp);
}
for(int i = 0; i < m; ++i){
scanf("%d%d", &a, &b);
q[a].push_back(b);
q[b].push_back(a);
num[a].push_back(i);
num[b].push_back(i);
}
//memset(vis, 0, sizeof(vis));
dis[1] = 0;
Lca(1);
for(int i = 0; i < m; ++ i) printf("%d\n", ans[i]);
}
return 0;
}
附RE代码:
#include <cstdio>
#include <iostream>
#include <vector>
const int M = 4e4+5;
using namespace std;
struct node{
int to, val;
};
vector<node > map[M];
vector<int> q[M], num[M];
bool vis[M];
int dis[M], ans[300], fat[M];
int f(int x){
if(x != fat[x]) fat[x] = f(fat[x]);
return fat[x];
}
void Lca(int u, int cur){ //就是这里多了一个参数,溢出了。。
vis[u] = 1;
dis[u] = cur;
for(int i = 0; i < map[u].size(); ++i){
int v = map[u][i].to;
if(!vis[v]){
Lca(v, cur+map[u][i].val);
fat[v] = u;
}
}
for(int i = 0; i < q[u].size(); ++ i){
int v = q[u][i];
if(vis[v]){
ans[num[u][i]] = dis[u]+dis[v]-2*dis[f(v)];
}
}
}
int main(){
int t, n, m;
//ios::sync_with_stdio(false);
scanf("%d", &t);
while(t --){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++ i){
map[i].clear(); q[i].clear();
fat[i] = i; vis[i] = 0;
}
int a, b, c;
for(int i = 0; i < n-1; ++i){
scanf("%d%d%d", &a, &b, &c);
node temp;
temp.to = a; temp.val = c;
map[b].push_back(temp);
temp.to = b;
map[a].push_back(temp);
}
for(int i = 0; i < m; ++i){
scanf("%d%d", &a, &b);
q[a].push_back(b);
q[b].push_back(a);
num[a].push_back(i);
num[b].push_back(i);
}
//memset(vis, 0, sizeof(vis));
Lca(1, 0);
for(int i = 0; i < m; ++ i) printf("%d\n", ans[i]);
}
return 0;
}
Hdoj 2586 How far away ? 【LCA】
标签:最近公共祖先
原文地址:http://blog.csdn.net/shengweisong/article/details/45047687