HDOJ2586 lca查询 tarjan模板程序
来源:互联网 发布:linux查看电源功率 编辑:程序博客网 时间:2024/06/06 18:13
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17516 Accepted Submission(s): 6786
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
Source
ECJTU 2009 Spring Contest
Recommend
lcy | We have carefully selected several similar problems for you: 3486 2874 2888 3234 2818
好像离线的算法效率比较高???
然后我就用了离线的tarjan算法了。。。
#include <iostream>#include <ctime>#include <vector>#include <cstring>#include <algorithm>using namespace std;typedef pair<int,int>P;const int maxn = 4e4+10;const int maxm = 205;int father[maxn],dis[maxn],ans[maxm];int i,n,m;bool vis[maxn];vector<P> e[maxn],q[maxn];void init(){ int x,y,z; cin >> n >> m; for (i=0; i<=n; i++) { e[i].clear(); q[i].clear(); vis[i] = 0; father[i] = i; dis[i] = 0; } for (i=1; i<n; i++) { cin >> x >> y >> z; e[x].push_back(make_pair(y,z)); e[y].push_back(make_pair(x,z)); } for (i=0; i<m; i++) { cin >> x >> y; if (x==y) continue; q[x].push_back(make_pair(y,i)); q[y].push_back(make_pair(x,i)); }}int Find(int x) { if (father[x]==x) return x; return father[x] = Find(father[x]);}void Union(int x, int y){ int fx = Find(x), fy = Find(y); if (fx!=fy) father[fx] = fy;}void tarjan(int x){ int i,v,w,lca; vis[x] = 1; for (i=0; i<e[x].size(); i++) { v = e[x][i].first; w = e[x][i].second; if (!vis[v]) { dis[v] = dis[x] + w; tarjan(v); Union(v,x); father[Find(x)] = x; } } for (i=0; i<q[x].size(); i++) { v = q[x][i].first; w = q[x][i].second; if (vis[v]) { lca = father[Find(v)]; ans[w] = dis[x] + dis[v] - 2*dis[lca]; } }}int main(){ std::ios::sync_with_stdio(false); int T; cin >> T; while (T--){ init(); tarjan(1); for (i=0; i<m; i++) cout << ans[i] << endl; } return 0;}
阅读全文
0 0
- HDOJ2586 lca查询 tarjan模板程序
- tarjan&&LCA模板
- 【模板】LCA Tarjan
- LCA之tarjan模板
- LCA Tarjan模板 HDU2586
- lca 离线tarjan模板
- LCA模板 Tarjan
- LCA离线tarjan算法模板
- HDU 2586 【LCA-Tarjan-模板】
- 单向无权LCA模板(Tarjan)
- LCA离线tarjan算法模板
- 【hdoj2586】【lca】How far away lca裸题
- LCA Tarjan及倍增模板(POJ 1330)
- 【模板】LCA Tarjan算法 (模板题:洛谷P3379)
- LCA离线模板(Tarjan)倍增模板 hdu2586
- Tarjan LCA
- Tarjan lca
- Tarjan LCA
- 51采集PCF8591数据通过ESP8266上传C#上位机
- 采访优秀学长学姐后的感悟与体会
- 机器学习笔记——半监督学习
- Shell中的数组及其相关操作
- Python匿名函数
- HDOJ2586 lca查询 tarjan模板程序
- listview+webview
- 腾讯2018应届毕业生校招之技术类招聘详细资讯
- 阿里云部署php项目Linux(7.3.1611)+XAMPP(5.6.31)
- sublime error trying to parse settings:expected value in packages\user\Defaul.....
- Python调用Matlab代码
- appiumapi详解(13)
- Qtree4
- net反射