LCA Tarjan模板 HDU2586
来源:互联网 发布:2016淘宝打假新规则 编辑:程序博客网 时间:2024/05/18 18:17
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>using namespace std;const int maxn = 40005;typedef long long ll;int Par[maxn], vis[maxn], dist[maxn];vector<int> G[maxn];vector<int> Dis[maxn];vector<int> Gx[maxn];vector<int> Gxnum[maxn];int getPar (int x) { int r = x, i = x; while(r != Par[r]) r = Par[r]; while(i != r) { int tmp = Par[i]; Par[i] = r; i = tmp; } return r;}void merge (int t1, int t2) { int f1 = getPar(t1); int f2 = getPar(t2); if(f1 != f2) Par[f2] = f1;}struct node { int x, y, zx;} nod[205];void dfs (int st) { for(int i=0; i<G[st].size(); ++i) { if(!vis[G[st][i]]) { vis[G[st][i]] = 1; dist[G[st][i]] = dist[st] + Dis[st][i]; dfs(G[st][i]); merge(st, G[st][i]); } } for(int i=0; i<Gx[st].size(); ++i) { if(vis[Gx[st][i]]) nod[Gxnum[st][i]].zx = getPar(Gx[st][i]); }}int main (void) { ios::sync_with_stdio(false); int T, n, m, a, b, c; cin>>T; while(T --) { for(int i=0; i<maxn; ++i) { Par[i] = i, vis[i] = dist[i] = 0; G[i].clear(), Dis[i].clear(); Gxnum[i].clear(), Gx[i].clear(); } cin>>n>>m; for(int i=1; i<n; ++i) { cin>>a>>b>>c; G[a].push_back(b); Dis[a].push_back(c); G[b].push_back(a); Dis[b].push_back(c); } for(int i=1; i<=m; ++i) { cin>>a>>b; Gx[a].push_back(b); Gx[b].push_back(a); nod[i].x = a, nod[i].y = b; Gxnum[a].push_back(i); Gxnum[b].push_back(i); } vis[1] = 1; dfs(1); for(int i=1; i<=m; ++i) { int ans = dist[nod[i].x]+dist[nod[i].y]-2*dist[nod[i].zx]; cout<<ans<<endl; } } return 0;}
阅读全文
0 0
- LCA Tarjan模板 HDU2586
- HDU2586 离线tarjan LCA
- tarjan 离线 lca hdu2586
- LCA离线模板(Tarjan)倍增模板 hdu2586
- hdu2586 LCA模板
- hdu2586 倍增lca模板
- LCA离线+在线+hdu2586(模板)
- hdu2586 How far away ?--tarjan & LCA
- hdu2586 How far away ? 【图论-Tarjan-Lca】
- Tarjan-LCA HDU2586 How far away ?
- HDU2586 How far away ?(LCA模板题)
- tarjan&&LCA模板
- 【模板】LCA Tarjan
- LCA之tarjan模板
- lca 离线tarjan模板
- LCA模板 Tarjan
- HDU2586 How far away ?【最近公共祖先】【Tarjan-LCA算法】
- hdu2586-LCA(tarjan法)-How far away ?
- Java-对象未初始化打印属性、类对象
- caffe: window8+cuda7.5+cudnn7.5
- 对CommonJs和AMD,CMD的理解,AMD和CMD的区别,CommonJS与ES6的module的区别
- 创建maven项目但是 webcontent这个文件夹没有
- Aspen.Exchanger.Design.and.Rating.V8.4 (FiredHeater,PlateFin好用)
- LCA Tarjan模板 HDU2586
- 表单验证,正则表达式收藏
- poj 1273-小白算法练习 Drainage Ditches 网络流
- 最高的奖励
- Python实现KNN回归
- 删除标签
- python 错误解析 AttributeError: 'module' object has no attribute 'dumps'
- Tapestry
- 分享 机器学习 好博客