uva 11354 - Bond(树链拆分)
题目大意:给定一张图。每次询问两个节点路径上进过边的危急值的最大值的最小值。
解题思路:首先建立最小生成数,然后依据这棵树做树链剖分。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 50005;
const int INF = 0x3f3f3f3f;
struct Edge {
int u, v, w;
Edge (int u = 0, int v = 0, int w = 0) { set(u, v, w); }
void set(int u, int v, int w) {
this->u = u;
this->v = v;
this->w = w;
}
friend bool operator < (const Edge& a, const Edge& b) {
return a.w < b.w;
}
}ed[maxn * 2];
int N, M, Q, ne, f[maxn], first[maxn], jump[maxn * 2], val[maxn];
int id, idx[maxn], top[maxn], far[maxn], son[maxn], dep[maxn], cnt[maxn];
vector<Edge> vec;
inline int getfar(int x) {
return x == f[x] ? x : f[x] = getfar(f[x]);
}
inline void add_Edge (int u, int v, int w) {
ed[ne].set(u, v, w);
jump[ne] = first[u];
first[u] = ne++;
}
void dfs (int u, int pre, int d) {
far[u] = pre;
dep[u] = d;
son[u] = 0;
cnt[u] = 1;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == pre)
continue;
dfs(v, u, d + 1);
cnt[u] += cnt[v];
if (cnt[son[u]] < cnt[v])
son[u] = v;
}
}
void dfs (int u, int rot) {
top[u] = rot;
idx[u] = ++id;
if (son[u])
dfs(son[u], rot);
for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == far[u] || v == son[u])
continue;
dfs(v, v);
}
}
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], s[maxn << 2];
inline void pushup(int u) {
s[u] = max(s[lson(u)], s[rson(u)]);
}
void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
if (l == r) {
s[u] = val[l];
return;
}
int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
}
int query(int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return s[u];
int mid = (lc[u] + rc[u]) / 2, ret = 0;
if (l <= mid)
ret = max(ret, query(lson(u), l, r));
if (r > mid)
ret = max(ret, query(rson(u), l, r));
return ret;
}
void init () {
int u, v, w;
ne = id = 0;
vec.clear();
memset(first, -1, sizeof(first));
for (int i = 1; i <= N; i++)
f[i] = i;
for (int i = 0; i < M; i++) {
scanf("%d%d%d", &u, &v, &w);
vec.push_back(Edge(u, v, w));
}
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
int p = getfar(vec[i].u);
int q = getfar(vec[i].v);
if (p == q)
continue;
add_Edge(vec[i].u, vec[i].v, vec[i].w);
add_Edge(vec[i].v, vec[i].u, vec[i].w);
f[p] = q;
}
dfs(1, 0, 0);
dfs(1, 1);
for (int i = 0; i < N - 1; i++) {
int t = i * 2;
u = (dep[ed[t].u] < dep[ed[t].v] ? ed[t].v : ed[t].u);
val[idx[u]] = ed[t].w;
}
build(1, 1, N);
}
int solve (int u, int v) {
int p = top[u], q = top[v], ret = 0;
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
ret = max(ret, query(1, idx[p], idx[u]));
u = far[p];
p = top[u];
}
if (u == v)
return ret;
if (dep[u] > dep[v])
swap(u, v);
ret = max(ret, query(1, idx[son[u]], idx[v]));
return ret;
}
int main () {
int cas = 0;
while (scanf("%d%d", &N, &M) == 2) {
if (cas++)
printf("\n");
init();
int u, v;
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &u, &v);
printf("%d\n", solve(u, v));
}
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
uva 11354 - Bond(树链拆分)的更多相关文章
- hdu5044 Tree 树链拆分,点细分,刚,非递归版本
hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...
- UVA 11354 - Bond (最小生成树 + 树链剖分)
题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- Codeforces 191 C Fools and Roads (树链拆分)
主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...
- HYSBZ 2243 染色 (树链拆分)
主题链接~~> 做题情绪:这题思路好想.调试代码调试了好久.第一次写线段树区间合并. 解题思路: 树链剖分 + 线段树区间合并 线段树的端点记录左右区间的颜色.颜色数目.合并的时候就用区间合并的 ...
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- hdu 4912 Paths on the tree(树链拆分+贪婪)
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...
- BZOJ 3589 动态树 树链拆分+纳入和排除定理
标题效果:鉴于一棵树.每个节点有一个右值,所有节点正确启动值他们是0.有两种操作模式,0 x y代表x右所有点的子树的根值添加y. 1 k a1 b1 a2 b2 --ak bk代表质疑. 共同拥有者 ...
- POJ 3237 Tree (树链拆分)
主题链接~~> 做题情绪:了. 解题思路: 主要注意如何区间更新就ok了 . 树链剖分就是树上的线段树. 代码: #include<iostream> #include<sst ...
随机推荐
- c++11多线程简介
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...
- Everything中文绿色版在Win7/8/10用不了问题的图文教程,只显示盘符
打开Everything后点击菜单上的“工具”——“选项” 勾选 Everything Service 后,点应用 需要用到管理者权限
- eclipse在maven项目交付svn忽略简介
文章来源:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明. 不时因为它将有关鲍恩梳理,它会做出相应的内容不变.文. ecl ...
- ios崩溃日志1
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not loa ...
- Android 应用程序启动过程源代码分析
本文转自:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程序的Activity的启动过程.在And ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
- SUPPORTDIR引用的文件的加入
上一篇转载了SUPPORTDIR的理论解释,如今截图说明实际操作: 选择依赖的语言,在右面的files框里右键点击,选择Insert Files插入文件,编译.由于安装得时候,压缩包会解压成一个暂时文 ...
- Nginx + IIS 配置,实现负载均衡
当你的Web应用程序访问量大的时候,一台服务器可能会因为压力过大而无法处理所有的请求.此时,可以增加服务器,采用负载均衡来分担所有的请求.关于Nginx的作用,自行百度了解.总之,在Windows平台 ...
- Debian/Ubuntu 已安装gcc/g++ 4.8.1
gcc 4.8.1 是第一个全然支持C++11(C++14非常可能在gcc 4.9.0開始支持.)的编译器,Windows上能够安装mingw版的.在sourceforge 上有下载.安装也比較方便. ...
- Event Sourcing
Event Sourcing - ENode(二) 接上篇文章继续 http://www.cnblogs.com/dopeter/p/4899721.html 分布式系统 前篇谈到了我们为何要使用分布 ...