【HDU 2586】测板
来源:互联网 发布:淘宝关闭订单规则 编辑:程序博客网 时间:2024/04/26 21:58
tarjan - LCA
O(n+q)
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <string.h> #include <stdio.h> #include <vector> using namespace std; const int N = 50005; vector<int> v[N],w[N],query[N],num[N]; int pre[N],dist[N],ans[N]; bool vis[N]; int n; void Init() { for(int i=1; i<=n; i++) { v[i].clear(); w[i].clear(); query[i].clear(); num[i].clear(); pre[i] = i; dist[i] = 0; vis[i] = false; } } int Find(int x) { if(pre[x] != x) pre[x] = Find(pre[x]); return pre[x]; } void Union(int x,int y) { x = Find(x); y = Find(y); if(x == y) return; pre[y] = x; } void Tarjan(int cur,int val) { vis[cur] = true; dist[cur] = val; int size = v[cur].size(); for(int i=0;i<size;i++) { int tmp = v[cur][i]; if(vis[tmp]) continue; Tarjan(tmp,val + w[cur][i]); Union(cur,tmp); } int Size = query[cur].size(); for(int i=0;i<Size;i++) { int tmp = query[cur][i]; if(!vis[tmp]) continue; ans[num[cur][i]] = dist[cur] + dist[tmp] - 2*dist[Find(tmp)]; } } int main() { int T,Q,x,y,z; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&Q); Init(); for(int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); v[x].push_back(y); w[x].push_back(z); v[y].push_back(x); w[y].push_back(z); } for(int i=0;i<Q;i++) { scanf("%d%d",&x,&y); query[x].push_back(y); query[y].push_back(x); num[x].push_back(i); num[y].push_back(i); } Tarjan(1,0); for(int i=0;i<Q;i++) printf("%d\n",ans[i]); } return 0; }
1 0
- 【HDU 2586】测板
- HDU 2586
- HDU 2586
- HDU 2586
- HDU 2586
- HDU 2586
- hdu 2586
- HDU 2586
- HDU 2586
- hdu 2586
- 周赛 HDU 2874 HDU 2586 LCA
- HDU 5266 HDU 2586 LCA问题
- hdu 2586 Tarjan离线
- hdu 2586的技巧
- HDU 2586 LCA
- HDU - 2586 LCA
- HDU 2586 LCA
- 【HDU 2586】LCA模板
- 一道笔试题,引发我的基础问题的反思
- 程序员的“第一天”
- Java多线程(2):创建与启动
- linux 不能上网
- Ubuntu中的vi
- 【HDU 2586】测板
- 工厂方法(Factory Method)
- unity3d camera.culling mask
- 异常处理
- 模式识别作业2
- HiHo #1032 : 最长回文子串 【Manacher算法】
- Leetcode||9. Palindrome Number
- scanf遇到空格继续输入
- 函数、指针