ZOJ 3195Design the city LCA
来源:互联网 发布:士兵突击吴哲 知乎 编辑:程序博客网 时间:2024/05/22 11:30
题目:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3195
题意:给定一个树,求三点之间的距离
思路:假定三点为u, v, w,那么(dist[u,v] + dist[v,w] + dist[u,w]) / 2就是答案
总结:wa时不要灰心,也许你离ac不远了
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 50010;struct edge1{ int to, cost, next;} g1[N*2];struct edge2{ int to, ind, next;} g2[N*10];int head1[N], head2[N], dist[N], res[N*10], par[N];int cnt1, cnt2;bool vis[N];int n, m;void init(){ for(int i = 0; i < n; i++) par[i] = i; memset(head1, -1, sizeof head1); memset(head2, -1, sizeof head2); memset(vis, 0, sizeof vis); cnt1 = cnt2 = 0;}void add_edge1(int v, int u, int c){ g1[cnt1].to = u; g1[cnt1].cost = c; g1[cnt1].next = head1[v]; head1[v] = cnt1++;}void add_edge2(int v, int u, int ind){ g2[cnt2].to = u; g2[cnt2].ind = ind; g2[cnt2].next = head2[v]; head2[v] = cnt2++;}int ser(int v){ int r = v, i = v, j; while(r != par[r]) r = par[r]; while(i != r) j = par[i], par[i] = r, i = j; return r;}void tarjan_lca(int v){ vis[v] = true; int u; for(int i = head1[v]; i != -1; i = g1[i].next) if(!vis[u=g1[i].to]) { dist[u] = dist[v] + g1[i].cost; tarjan_lca(u); par[u] = v; } for(int i = head2[v]; i != -1; i = g2[i].next) if(vis[u=g2[i].to]) res[g2[i].ind] = dist[v] + dist[u] - 2 * dist[ser(u)];}int main(){ int a, b, c, x = 0; while(~ scanf("%d", &n)) { init(); for(int i = 0; i < n - 1; i++) { scanf("%d%d%d", &a, &b, &c); add_edge1(a, b, c); add_edge1(b, a, c); } scanf("%d", &m); for(int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); add_edge2(a, b, i); add_edge2(b, a, i); add_edge2(b, c, i + m); add_edge2(c, b, i + m); add_edge2(a, c, i + 2 * m); add_edge2(c, a, i + 2 * m); } dist[0] = 0; tarjan_lca(0); if(x++) printf("\n"); for(int i = 0; i < m; i++) printf("%d\n", (res[i] + res[i+m] + res[i+2*m]) / 2); } return 0;}
0 0
- zoj 3195 Design the city LCA
- zoj 3195 Design the city (LCA)
- Design the city - ZOJ 3195 LCA
- zoj 3195 Design the city(LCA)
- ZOJ - 3195 Design the city(LCA)
- ZOJ 3195Design the city LCA
- ZOJ 3195 Design the city LCA转RMQ
- ZOJ 3195 Design the city (在线LCA,4级)
- ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】
- ZOJ 3195 - Design the city(LCA'离线算法Tarjan)
- ZOJ 3195 Design the city(LCA Tarjan)
- ZOJ 3195 Design the city(LCA变形)
- zoj 3195 Design the city 【LCA转RMQ】 【求三点最短距离】
- ZOJ 题目3195 Design the city(LCA+RMQ)
- ZOJ 3195 Design the city LCA转RMQ在线
- 3195 Design the city //LCA
- Zoj 3195 Design the city
- ZOJ 3195 Design the city
- 隐藏TabControl 标签但是选项卡可以显示出来的代码
- linux平台使用gnu工具链来开发stm32单片机 环境搭建,备忘录
- 这个笑话网:刘备是个钉子户
- TCPdump
- Unity3D用户手册
- ZOJ 3195Design the city LCA
- java写出大数据(千万级别)的excel探索(一)
- Redhat 7使用CentOS 7的Yum网络源
- 关于Servlet中Path的几个注意点
- iptables
- 这个笑话网:三国西游穿越版笑人冷料
- 如何用tpcc-mysql工具进行oltp压力测试
- 哈希表和红黑树的对比
- Sublime Text 3 汉化破解