$ \rightarrow $ 戳我进CF原题

E. Tourists


time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output


 
There are $ n $ cities in Cyberland, numbered from $ 1 $ to $ n $ , connected by m bidirectional roads.
The $ j $ -th road connects city $ a_j $ and $ b_j $ .
 
For tourists, souvenirs are sold in every city of Cyberland. In particular, city $ i $ sell it at a price of $ w_i $ .
 
Now there are $ q $ queries for you to handle. There are two types of queries:
 

  • " C $ a w $ ": The price in city $ a $ is changed to $ w $.

  • " A $ a b $ ": Now a tourist will travel from city $ a $ to $ b $ .
    He will choose a route, he also doesn't want to visit a city twice.
    He will buy souvenirs at the city where the souvenirs are the cheapest (possibly exactly at city $ a $ or $ b $ ).
    You should output the minimum possible price that he can buy the souvenirs during his travel.
     

More formally, we can define routes as follow:
 

  • A route is a sequence of cities $ [x_1, x_2, ..., x_k] $ , where $ k $ is a certain positive integer.

  • For any $ 1 ≤ i < j ≤ k, xi ≠ xj $ .

  • For any $ 1 ≤ i < k $ , there is a road connecting $ x_i $ and $ x_{i + 1} $ .

  • The minimum price of the route is $ min(w_{x_1}, w_{x_2}, ..., w_{x_k}) $ .

  • The required answer is the minimum value of the minimum prices of all valid routes from $ a $ to $ b $ .
     

Input

The first line of input contains three integers $ n, m, q (1 ≤ n, m, q ≤ 10^5) $ , separated by a single space.
 
Next $ n $ lines contain integers $ w_i (1 ≤ w_i ≤ 10^9) $ .
 
Next $ m $ lines contain pairs of space-separated integers $ a_j $ and $ b_j (1 ≤ a_j, b_j ≤ n, a_j ≠ b_j) $ .
 
It is guaranteed that there is at most one road connecting the same pair of cities.
There is always at least one valid route between any two cities.
 
Next $ q $ lines each describe a query. The format is " C $ a w $ " or " A $ a b $ " $ (1 ≤ a, b ≤ n, 1 ≤ w ≤ 10^9) $ .
 

Output

For each query of type "A", output the corresponding answer.
 

Examples

input1

 3 3 31231 22 31 3
A 2 3
C 1 5
A 2 3

output1

 12

input2

 7 9 412345671 22 51 52 33 42 45 66 75 7
A 2 3
A 6 4
A 6 7
A 3 3

output2

 2153

 

Note

For the second sample, an optimal routes are:
 
From $ 2 $ to $ 3 $ it is $ [2, 3] $ .
 
From $ 6 $ to $ 4 $ it is $ [6, 5, 1, 2, 4] $ .
 
From $ 6 $ to $ 7 $ it is $ [6, 5, 7] $ .
 
From $ 3 $ to $ 3 $ it is $ [3] $ .

 

题目大意

  • $ n $ 个点 $ m $ 条边的无向图,每个点的纪念品都有一个价格,执行 $ q $ 次操作,分为两类

  • 改变一个点的纪念品价格

  • 询问 $ x $ 到 $ y $ 的任意简单路径上最便宜的纪念品

  • $ n,m,q \le 100000

 

题解

结论:一个点数大于等于3的点双连通分量中对于任意不同的三点 $ a,b,c $ ,
必定存在一条简单路径从 $ a $ 走到 $ b $ 经过 $ c $ 。
 

  • 一个 $ v-DCC $ 中的点肯定能通过简单路径互相到达

  • 把点双连通分量缩点,形成一棵树,树上包括“割点”和“缩点后的新点”

 

-对于每个缩成的点,用 $ set $ 维护对应 $ v-DCC $ 中除了“最高割点”之外的纪念品的最小价格

  • 换言之,割点纪念品的价格只在树上“割点”本身和它父亲对应的 $ v-DCC $ 中维护

 

  • 最后再加上动态树或树链剖分即可解决

 

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
using namespace std;
#define N 200005
multiset<int>s[N];
multiset<int>::iterator it;
vector<int>e[N],G[N];
stack<int>st;
int n,m,q,dfn[N],low[N],tim,cnt,w[N],bel[N];
bool vis[N];
void tarjan(int u,int fa){dfn[u]=low[u]=++tim; st.push(u); vis[u]=1;for(int i=0;i<e[u].size();++i){int v=e[u][i];if(v==fa) continue;if(!dfn[v]){tarjan(v,u);low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]){++cnt; int tmp;G[u].push_back(cnt);do{tmp=st.top(); st.pop();G[cnt].push_back(tmp);s[cnt].insert(w[tmp]);bel[tmp]=cnt;}while(tmp!=v);w[cnt]=*(s[cnt].begin());}} else if(vis[v])low[u]=min(low[u],dfn[v]);}
}
int siz[N],f[N],dep[N],son[N],top[N],id[N],wt[N];
void dfs1(int u){siz[u]=1; for(int i=0;i<G[u].size();++i){int v=G[u][i];dep[v]=dep[u]+1; f[v]=u;dfs1(v);siz[u]+=siz[v];if(siz[v]>siz[son[u]]) son[u]=v; }
}
void dfs2(int u,int topf){top[u]=topf;wt[id[u]=++tim]=u;if(son[u]) dfs2(son[u],topf);for(int i=0;i<G[u].size();++i){int v=G[u][i];if(v==son[u]) continue;dfs2(v,v);}
}
int sum[N<<2];
void build(int o,int l,int r){if(l==r){sum[o]=w[wt[l]];return;}int mid=l+r>>1;build(o<<1,l,mid); build(o<<1|1,mid+1,r);sum[o]=min(sum[o<<1],sum[o<<1|1]);
}
void updata(int o,int l,int r,int u,int val){if(l==r){sum[o]=val;return;}int mid=l+r>>1;if(u<=mid) updata(o<<1,l,mid,u,val);else updata(o<<1|1,mid+1,r,u,val);sum[o]=min(sum[o<<1],sum[o<<1|1]);
}
int check(int o,int l,int r,int u,int v){if(u<=l&&r<=v){return sum[o];}int res=1e9+7,mid=l+r>>1;if(u<=mid) res=min(res,check(o<<1,l,mid,u,v));if(v>mid) res=min(res,check(o<<1|1,mid+1,r,u,v));return res;
}
void modify(int u,int val){if(bel[u]){it=s[bel[u]].find(w[u]);s[bel[u]].erase(it);}w[u]=val;updata(1,1,cnt,id[u],val);if(bel[u]){s[bel[u]].insert(w[u]);w[bel[u]]=*(s[bel[u]].begin());updata(1,1,cnt,id[bel[u]],w[bel[u]]);}
}
int query(int u,int v){int res=1e9+7;while(top[u]!=top[v]){if(dep[top[u]]<dep[top[v]]) swap(u,v);res=min(res,check(1,1,cnt,id[top[u]],id[u]));u=f[top[u]];}if(dep[u]>dep[v]) swap(u,v);res=min(res,check(1,1,cnt,id[u],id[v]));if(u>n&&f[u]) res=min(res,w[f[u]]);return res;
}
int main(){scanf("%d %d %d",&n,&m,&q);cnt=n;for(int i=1;i<=n;++i) scanf("%d",&w[i]);for(int i=1;i<=m;++i){int u,v;scanf("%d %d",&u,&v);e[u].push_back(v);e[v].push_back(u);}tarjan(1,0);dfs1(1);tim=0;dfs2(1,0);build(1,1,cnt);while(q--){char opt[1]; int x,y;scanf("%s %d %d",opt,&x,&y);if(opt[0]=='C') modify(x,y);else printf("%d\n",query(x,y));}return 0;
}
/*
#         42550351
When      2018-09-06 14:42:23
Who       PotremZ
Problem   E - Tourists
Lang      GNU C++11
Verdict   Accepted
Time      421 ms
Memory    41800 KB
*/

转载于:https://www.cnblogs.com/PotremZ/p/9600411.html

codeforces CF487E Tourists 边双连通分量 树链剖分相关推荐

  1. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  2. AcWing 397. 逃不掉的路(边双连通分量缩点成树 + 树链剖分乱搞)

    整理的算法模板合集: ACM模板 我们知道在同一个边双连通分量中的点没有必经边(因为至少有两条分离的路径). 所以我们直接tarjan求出桥后缩点,然后求一下树上两点间的距离即可. 那么如何求树上两点 ...

  3. 广义圆方树+树链剖分+set(Codeforces Round #278 (Div. 1): E. Tourists)

    前置:双联通分量.圆方树.树链剖分 什是是广义圆方树 圆方树是针对于仙人掌建树,而广义圆方树是针对无向图建树,对于一个无向图 无向图中的所有点 → 广义圆方树中的所有圆点 无向图中的一个双联通分量 → ...

  4. CF487E Tourists(圆方树+树链剖分)

    洛谷题目传送门 解题思路 不会圆方树的可以看我的博客圆方树学习记录及例题 首先Tarjan寻找点双连通分量,然后建立圆方树,每个方点存这个点双内的最小点权 将圆方树树链剖分之后,对于修改操作,将这个点 ...

  5. 【CF487E】Tourists【圆方树】【树链剖分】【multiset】

    题意:给一张 nnn 点 mmm 边的连通无向图,点帯权,qqq 次操作: 修改一个点的权值. 询问两点间所有简单路的最小权值的最小值. n,m,q≤105n,m,q\leq 10^5n,m,q≤10 ...

  6. CodeForces - 160D Edges in MST(思维+tarjan/树链剖分+线段树)

    题目链接:点击查看 题目大意:给出一张 n 个点 m 条边组成的带权无向图,现在对于每条边来说,确定一下其分类: 一定是最小生成树上的边 可能是最小生成树上的边 一定不是最小生成树的边 题目分析:两种 ...

  7. CodeForces - 609E Minimum spanning tree for each edge(最小生成树+树链剖分+线段树/树上倍增)

    题目链接:点击查看 题目大意:给出一张 n 个点和 m 条边组成的无向图,现在询问包含每一条边的最小生成树 题目分析:考虑求解次小生成树的思路: 求出最小生成树 ans 枚举每一条非树边 ( u , ...

  8. CodeForces - 343D Water Tree(树链剖分+线段树)

    题目链接: 题目大意:给出一棵由n个点组成的树,初始时每个点的权值为0,接下来有m个操作,每个操作分为以下三种: 1 x:将包括节点x在内的所有子孙节点的权值都改为1 2 x:将包括节点x在内的所有父 ...

  9. D. Best Edge Weight(最小生成树 + 树链剖分)(Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals))

    D. Best Edge Weight 给定一个有nnn个点mmm条边的无向连通图,有mmm次询问,每次询问第iii条边的权值最大为多少,这张图的所有最小生成树的方案中,一定包含第iii条边. 先跑一 ...

  10. [JZOJ5909]【NOIP2018模拟10.16】跑商【圆方树】【树链剖分】

    Description 基三的地图可以看做 n 个城市,m 条边的无向图,尊者神高达会从某个点出发并在起点购买货物,在旅途中任意一点卖出并最终到达终点,尊者神高达的时间很宝贵,所以他不会重复经过同一个 ...

最新文章

  1. IBM超越谷歌抵达量子计算里程碑:研制出50量子位计算机
  2. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)...
  3. python基础知识思维导图-总结 Python 知识点思维导图
  4. mac ox 10.9 安装eclipse cpp launch failed binary not found
  5. 浅谈常见的七种加密算法及实现(附代码)
  6. 用python内置函数算复杂度吗_番外篇: Python 面试感受
  7. .NET2.0学习资料
  8. 大剑无锋之已知后续遍历bfegcda,中序badefcg,前序是?【面试推荐】
  9. 宾馆管理系统mysql_宾馆管理系统(含源码和数据库文件)
  10. HTML+CSS+JS实现 ❤️从亮到暗图片滤镜特效❤️
  11. 大理大学日常作业计算机基础知识,大理学院成人高等教育大学计算机基础课程作业.doc...
  12. php值传递和引用传递
  13. db2 springboot 整合_Spring boot Mybatis 整合(完整版)
  14. overflow-x和文字超出...显示
  15. 网站微信扫码登录实现步骤
  16. 华为网络技术大赛2017 考后感
  17. Python3.GrADS的二进制码数据
  18. 清除手机图案解锁(执行adb命令工具类)
  19. NTKO 文件在线编辑并保存 目前平台板plus支持 word excel ppt
  20. Qt Quick实现的文件传输工具(TCP传输篇)

热门文章

  1. 为什么有斯坦福计算机科学博士学位的你找不到工作?
  2. 用计算机表白我不喜欢你了,绝对看不懂的表白公式(用古文暗示我喜欢你的方式)...
  3. 一步到位Composer直接打开SOLIDWORKS贴图
  4. 二进制拆弹(20181023-20181026)
  5. 历年软考网络规划师考点总结
  6. Scrapy 入门教程
  7. 小米pro15拆机_小米笔记本Pro 15增强版值得买吗 小米笔记本Pro 15增强版拆解+评测...
  8. IBM笔记本电池保养细则
  9. coffeescript html5,CoffeeScript入门
  10. CodeForces 669A Little Artem and Presents