HDOJ2586 lca查询 tarjan模板程序
2017-08-25 09:50
190 查看
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.
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
2
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
Sample Output
10
25
100
100
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; }
相关文章推荐
- hdoj 2586 How far away ? 【Tarjan离线LCA】
- HDU 2586 【LCA-Tarjan-模板】
- LCA tarjan 算法 练习: hdu 2586 + poj 1986
- hdu 2586 How far away ?(离线tarjan求LCA)
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- How far away ? HDU - 2586 tarjan求LCA
- 洛谷 P3379 【模板】最近公共祖先(LCA) (在线倍增+离线Tarjan)
- Hdoj 2586 How far away ? 【LCA】
- HDU OJ 2586 How far away ?【LCA的Tarjan离线算法】
- hdoj 2586 How far away ? 【LCA转RMQ入门题】
- POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
- poj3694(lca + tarjan求桥模板)
- LCA模板 Tarjan
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- How far away ? HDU - 2586 tarjan求LCA
- Hdu 2586 学习 LCA 的 Tarjan 算法
- LCA tarjan hdu 2586代码详细步骤(转)
- hdoj 2586 <LCA转RMQ算法--学习之路>
- LCA Tarjan及倍增模板(POJ 1330)
- HDU—— 2586 How far away ?(LCA tarjan)