给定一个n个点m条边的无向图,求图上的两点的所有的简单路径之间的最小边。

蓝链

$ n,m,q \leq 100000, w_i \leq 10 ^7$

Solution

考虑建立用缩点双来建立广义圆方树,然后方点的值是当前点双内最小的点 ,这样就直接维护树上的最小值就可以了。 但如果更新的是根节点。那么他的每个方儿子都会被更新。所以特别处理一下根节点即可。记方点权值为除根节点外的点。特别处理一下即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
const int BUF_SIZE = (int)1e6 + 10;
struct fastIO {
char buf[BUF_SIZE], buf1[BUF_SIZE];
int cur, cur1;
FILE *in, *out;
fastIO() {
cur = BUF_SIZE, in = stdin, out = stdout;
cur1 = 0;
}
inline char getchar() {
if(cur == BUF_SIZE) fread(buf, BUF_SIZE, 1, in), cur = 0;
return *(buf + (cur++));
}
inline void putchar(char ch) {
*(buf1 + (cur1++)) = ch;
if (cur1 == BUF_SIZE) fwrite(buf1, BUF_SIZE, 1, out), cur1 = 0;
}
inline int flush() {
if (cur1 > 0) fwrite(buf1, cur1, 1, out);
return cur1 = 0;
}
}IO;
#define getchar IO.getchar
#define putchar IO.putchar
int read() {
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
void putString(char s[], char EndChar = '\n') {
rep(i, 0, strlen(s) - 1) putchar(*(s + i));
if(~EndChar) putchar(EndChar);
} #define Maxn 300009
struct edge {
int to, nxt;
}g[Maxn << 1], g1[Maxn << 1];
int n, m, head[Maxn], e, e1, head1[Maxn];
multiset<int> S[Maxn];
int val[Maxn];
int dfn[Maxn], low[Maxn], _clk, cnt_Squ;
stack <int> s;
int top[Maxn], dep[Maxn], size[Maxn], son[Maxn];
int fa[Maxn], efn[Maxn], _index;
namespace INIT {
void add(int u, int v) {
g[++e] = (edge){v, head[u]}, head[u] = e;
}
void add1(int u, int v) {
g1[++e1] = (edge){v, head1[u]}, head1[u] = e1;
}
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++_clk;
s.push(u);
for(int i = head[u]; ~i; i = g[i].nxt) {
int v = g[i].to;
if(v != fa)
if(!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) {
add1(n + (++cnt_Squ), u), add1(u, n + cnt_Squ);
int a;
do {
a = s.top(); s.pop();
add1(n + cnt_Squ, a), add1(a, n + cnt_Squ);
}while(a != v);
}
}else low[u] = min(low[u], dfn[v]);
}
}
void dfs_init(int u, int f) {
size[u] = 1;
dep[u] = dep[f] + 1, fa[u] = f;
for(int i = head1[u]; ~i; i = g1[i].nxt) {
int v = g1[i].to;
if(v != f) {
dfs_init(v, u);
size[u] += size[v];
if(son[u] == -1 || size[son[u]] < size[v]) v = son[u];
}
}
}
void dfs_link(int u, int _top) {
top[u] = _top;
dfn[u] = ++_index, efn[_index] = u;
if(~son[u]) dfs_link(son[u], _top);
for(int i = head1[u]; ~i; i = g1[i].nxt) {
int v = g1[i].to;
if(v ^ fa[u] && v ^ son[u]) dfs_link(v, v);
}
}
void Main() {
clar(head, -1);
clar(head1, -1);
n = read(), m = read();
rep(i, 1, n) val[i] = read();
rep(i, 1, m) {
int u = read(), v = read();
add(u, v), add(v, u);
}
tarjan(1, 0);
clar(dfn, 0), _clk = 0;
clar(son, -1);
dfs_init(1, 0);
dfs_link(1, 1);
rep(i, 2, n) S[fa[i] - n].insert(val[i]);
}
}
namespace SGMT_tree {
int tree[Maxn << 2];
#define lc(x) ((x) << 1)
#define rc(x) ((x) << 1 | 1)
#define ls rt << 1, l, mid
#define rs rt << 1 | 1, mid + 1, r
void pushup(int root) {
tree[root] = min(tree[lc(root)], tree[rc(root)]);
}
void build(int rt, int l, int r) {
if(l == r) {
tree[rt] = (efn[l] <= n) ? (val[efn[l]]) : (*S[efn[l] - n].begin());
return ;
}
int mid = (l + r) >> 1;
build(ls), build(rs);
pushup(rt);
}
void modify(int rt, int l, int r, int pos) {
if(l == r) {
tree[rt] = (efn[l] <= n) ? (val[efn[l]]) : (*S[efn[l] - n].begin());
return;
}
int mid = (l + r) >> 1;
(pos <= mid) ? modify(ls, pos) : modify(rs, pos);
pushup(rt);
}
int query(int rt, int l, int r, int x, int y) {
if(x <= l && r <= y) return tree[rt];
int mid = (l + r) >> 1;
if(mid >= y) return query(ls, x, y);
if(mid + 1 <= x) return query(rs, x, y);
return min(query(ls, x, y), query(rs, x, y));
}
#undef lc
#undef rc
#undef ls
#undef rs
}
namespace SOLVE {
void Main() {
int amt = cnt_Squ + n;
SGMT_tree :: build(1, 1, amt);
rep(i, 1, read()) {
char s = getchar();
int x = read(), y = read();
if(s == 'C') {
if(x > 1) {
S[fa[x] - n].erase(S[fa[x] - n].lower_bound(val[x]));
S[fa[x] - n].insert(y);
SGMT_tree :: modify(1, 1, amt, dfn[fa[x]]);
}
val[x] = y;
SGMT_tree :: modify(1, 1, amt, dfn[x]);
}
if(s == 'Q') {
int Tmp = x, res = INT_MAX;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res = min(res, SGMT_tree :: query(1, 1, amt, dfn[top[x]], dfn[x]));
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
res = min(res, SGMT_tree :: query(1, 1, amt, dfn[x], dfn[y]));
if(x > n) res = min(res, val[fa[x]]);
assert(res != INT_MAX);
write(val[Tmp] - res), putchar('\n');
}
}
}
}
int main() {
INIT :: Main();
SOLVE :: Main();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return IO.flush();
}

[CF Round #278] Tourists的更多相关文章

  1. UOJ #30. [CF Round #278] Tourists

    UOJ #30. [CF Round #278] Tourists 题目大意 : 有一张 \(n\) 个点, \(m\) 条边的无向图,每一个点有一个点权 \(a_i\) ,你需要支持两种操作,第一种 ...

  2. 圆方树简介(UOJ30:CF Round #278 Tourists)

    我写这篇博客的原因 证明我也是学过圆方树的 顺便存存代码 前置技能 双联通分量:点双 然后就没辣 圆方树 建立 新建一个图 定义原图中的所有点为圆点 对于每个点双联通分量(只有两个点的也算) 建立一个 ...

  3. UOJ30——【CF Round #278】Tourists

    1.感谢taorunz老师 2.题目大意:就是给个带权无向图,然后有两种操作, 1是修改某个点的权值 2是询问,询问一个值,就是u到v之间经过点权的最小值(不可以经过重复的点) 操作数,点数,边数都不 ...

  4. UOJ #30. 【CF Round #278】Tourists

    Description Cyberland 有 n 座城市,编号从 1 到 n,有 m 条双向道路连接这些城市.第 j 条路连接城市 aj 和 bj.每天,都有成千上万的游客来到 Cyberland ...

  5. UOJ #30【CF Round #278】Tourists

    求从$ x$走到$ y$的路径上可能经过的最小点权,带修改  UOJ #30 $ Solution:$ 如果两个点经过了某个连通分量,一定可以走到这个连通分量的最小值 直接构建圆方树,圆点存原点的点权 ...

  6. 【题解】【CF Round #278】Tourists

    圆方树第二题…… 图中询问的是指定两点之间简单路径上点的最小权值.若我们建出圆方树,圆点的权值为自身权值,方点的权值为所连接的圆点的权值最小值(即点双连通分量中的最小权值).我们可以发现其实就是这两点 ...

  7. uoj30【CF Round #278】Tourists(圆方树+树链剖分+可删除堆)

    - 学习了一波圆方树 学习了一波点分治 学习了一波可删除堆(巧用 ? STL) 传送门: Icefox_zhx 注意看代码看怎么构建圆方树的. tips:tips:tips:圆方树内存记得开两倍 CO ...

  8. CF Round #551 (Div. 2) D

    CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...

  9. CF Round #510 (Div. 2)

    前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...

随机推荐

  1. Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported

    Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported 如果原有项目用的为JDK1.7,而自己的是低版本JDK,比如1. ...

  2. 关于rman duplicate 一些比較重要的知识点--系列三

    FYI: http://docs.oracle.com/cd/E11882_01/backup.112/e10643/rcmsynta020.htm#RCMRF126 rman duplicate d ...

  3. MySQL多实例配置(一)

    MySQL数据库的集中化运维,能够通过在一台MySQL数据库服务器上,部署多个MySQL实例.该功能是通过mysqld_multi来实现.mysqld_multi用于管理多个mysqld的服务进程,这 ...

  4. Windows——cmd findstr 字符串查找增强使用说明

    在文件中寻找字符串. 复制代码代码如下: FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [ ...

  5. mysql查看所有存储过程,函数,视图,触发器,表,分页

    查询数据库中的存储过程和函数 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' ...

  6. 【iOS系列】-UIImageView帧动画相关属性介绍

    UIImageView帧动画相关属性介绍 1:相关属性: //An array of UIImage objects to use for an animation.存放UIImage对象,会按顺序显 ...

  7. ranlib

    1 ranlib的缩写 random access library 2 ranlib的作用 为静态库的符号建立索引,可以加速链接,因此称用ranlib处理过的library为random access ...

  8. jquery loop on Json data using $.each

    Hi I have the following JSON returned in a variable called data. THIS IS THE JSON THAT GETS RETURNED ...

  9. 聚合类新闻client初体验

    初体验的产品:今日头条(ios3.6).百度新闻(ios4.4.0).ZAKER(ios4.4.5).鲜果(ios3.8.7).中搜搜悦(ios4.0.1).Flipboard(ios2.3.9) 1 ...

  10. eclipse 重启/打开内置浏览器

    重启 Eclipse 重启选项允许用户重启 Eclipse. 我们可以通过点击 File 菜单选择 Restart 菜单项来重启 Eclipse. Eclipse 内置浏览器 Web 浏览器 Ecli ...