给定一个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. [教程]Delphi 中三种回调函数形式解析

    Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...

  2. curl 发送post请求

    curl 发送post请求 curl -X POST "http://localhost:8080/usr3?id=1&name=3&departmentId=2" ...

  3. Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>

    这是因为架包没导对或者关联的项目不是在同一个工作空间.

  4. Python 基础语法(和Java相比)

    Python变量和数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ...

  5. AFNetworking配合Swift3.0请求数据

    首先用桥接或pods将AFNetworking导入项目,在这不再赘述,然后创建一个单例NetWorkTools.swift 继承:AFHTTPSessionManager import UIKit i ...

  6. 在linux命令行中编译和运行java文件

    同时加载编译多个jar包和java文件 在个人平常使用或者当我们把代码部署到linux服务器上的时候,我们经常需要通过命令行编译和运行java文件,网上关于这个的方法大多是通过 javac -cp f ...

  7. Python不同功能的函数

    •函数作为参数 import math def add(x,y,f): return f(x) + f(y)print add(25,36,math.sqrt) •map()函数 map()是 Pyt ...

  8. C++问题记录

    问题idx: 1) 怎么在VS2010下新建一个像VC6.0 中那样的控制台C++程序. cdate: 2014-4-24 A1: VC6.0 对标准C++集的支持不是太好, VS2010也有一些吧, ...

  9. 关于集成支付宝SDK的开发

    下载 首先,你要想找到这个SDK,都得费点功夫.如今的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 "请点此下载集成开发包" Baidu和Googlep排在前面的支付宝开 ...

  10. ASP.NET MVC 提供与访问 Web Api

    ASP.NET MVC 提供与访问 Web Api 一.提供一个 Web Api 新建一个项目,类型就选 "Web Api".我用的是MVC5,结果生成的项目一大堆东西,还编译不过 ...