洛谷 2173 BZOJ 2816 [ZJOI2012]网络
【题解】
明显的LCT模板题,c种颜色就开c棵LCT好了。。
- #include<cstdio>
- #include<algorithm>
- #define N 100010
- #define C 11
- #define rg register
- #define ls (son[c][u][0])
- #define rs (son[c][u][1])
- using namespace std;
- int n,m,c,k,opt,x,y,w,top;
- int cnt[C][N],fa[C][N],son[C][N][],val[N],mx[C][N],rev[C][N],st[N],to[C][N][];
- inline int read(){
- int k=,f=; char c=getchar();
- while(c<''||c>'')c=='-'&&(f=-),c=getchar();
- while(''<=c&&c<='')k=k*+c-'',c=getchar();
- return k*f;
- }
- inline bool isroot(int c,int u){
- return son[c][fa[c][u]][]!=u&&son[c][fa[c][u]][]!=u;
- }
- inline bool which(int c,int u){
- return son[c][fa[c][u]][]==u;
- }
- inline void pushup(int c,int u){
- mx[c][u]=max(max(mx[c][ls],mx[c][rs]),val[u]);
- }
- inline void pushdown(int c,int u){
- rev[c][ls]^=; rev[c][rs]^=; rev[c][u]=; swap(ls,rs);
- }
- void rotate(int c,int u){
- int f=fa[c][u],gf=fa[c][f],wh=which(c,u);
- if(!isroot(c,f)) son[c][gf][which(c,f)]=u;
- fa[c][u]=fa[c][f]; fa[c][f]=u; fa[c][son[c][u][wh^]]=f;
- son[c][f][wh]=son[c][u][wh^]; son[c][u][wh^]=f;
- pushup(c,f); pushup(c,u);
- }
- inline void splay(int c,int u){
- st[top=]=u;
- for(rg int i=u;!isroot(c,i);i=fa[c][i]) st[++top]=fa[c][i];
- for(rg int i=top;i;i--) if(rev[c][st[i]]) pushdown(c,st[i]);
- while(!isroot(c,u)){
- if(!isroot(c,fa[c][u])) rotate(c,which(c,u)==which(c,fa[c][u])?fa[c][u]:u);
- rotate(c,u);
- }
- }
- inline void access(int c,int u){
- for(rg int s=;u;s=u,u=fa[c][u]) splay(c,u),son[c][u][]=s,pushup(c,u);
- }
- inline void makeroot(int c,int u){
- access(c,u); splay(c,u); rev[c][u]^=;
- }
- inline int find(int c,int u){
- access(c,u); splay(c,u);
- while(ls) u=ls; return u;
- }
- inline void split(int c,int x,int y){
- makeroot(c,x); access(c,y); splay(c,y);
- }
- inline void link(int c,int x,int y){
- cnt[c][x]++; cnt[c][y]++;
- if(to[c][x][]==-) to[c][x][]=y; else to[c][x][]=y;
- if(to[c][y][]==-) to[c][y][]=x; else to[c][y][]=x;
- makeroot(c,x); fa[c][x]=y;
- }
- inline void cut(int c,int x,int y){
- split(c,x,y); int t=son[c][y][];
- if(!son[c][t][]&&t==x){
- son[c][y][]=,fa[c][x]=,cnt[c][x]--,cnt[c][y]--;
- if(to[c][x][]==y) to[c][x][]=-; else to[c][x][]=-;
- if(to[c][y][]==x) to[c][y][]=-; else to[c][y][]=-;
- }
- else{
- while(son[c][t][]) t=son[c][t][];
- if(t==x){
- son[c][fa[c][t]][]=,fa[c][x]=,cnt[c][x]--,cnt[c][y]--;
- if(to[c][x][]==y) to[c][x][]=-; else to[c][x][]=-;
- if(to[c][y][]==x) to[c][y][]=-; else to[c][y][]=-;
- }
- }
- }
- inline bool haveedge(int c,int x,int y){
- split(c,x,y); int t=son[c][y][];
- if(!son[c][t][]&&t==x) return ;
- else{
- while(son[c][t][]) t=son[c][t][];
- if(t==x) return ;
- }
- return ;
- }
- int main(){
- n=read(); m=read(); c=read()-; k=read();
- for(rg int i=;i<=n;i++){
- val[i]=read();
- for(rg int j=;j<=c;j++) mx[j][i]=val[i];
- }
- for(rg int i=;i<=m;i++){
- x=read(); y=read(); w=read();
- link(w,x,y);
- }
- while(k--){
- if((opt=read())==){
- x=read(),y=read();
- for(rg int i=;i<=c;i++) access(i,x),splay(i,x);
- val[x]=y;
- for(rg int i=;i<=c;i++) pushup(i,x);
- }
- if(opt==){
- x=read(),y=read(),w=read(); bool linked=; int lastcol=;
- for(rg int i=;i<=c;i++)if(haveedge(i,x,y)){
- linked=; lastcol=i; break;
- }
- // for(rg int i=0;i<=c;i++)if(to[i][x][0]==y||to[i][x][1]==y){
- // linked=1; lastcol=i; break;
- // }
- if(lastcol==w&&linked){
- puts("Success."); continue;
- }
- if(!linked){
- puts("No such edge."); continue;
- }
- if(cnt[w][x]>=||cnt[w][y]>=){
- puts("Error 1."); continue;
- }
- if(find(w,x)==find(w,y)){
- puts("Error 2."); continue;
- }
- cut(lastcol,x,y); link(w,x,y); puts("Success.");
- }
- if(opt==){
- w=read(); x=read(); y=read();
- if(find(w,x)!=find(w,y)){
- puts("-1"); continue;
- }
- split(w,x,y); printf("%d\n",mx[w][y]);
- }
- }
- }
洛谷 2173 BZOJ 2816 [ZJOI2012]网络的更多相关文章
- BZOJ.2816.[ZJOI2012]网络(LCT)
题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...
- bzoj 2816: [ZJOI2012]网络(splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...
- 【刷题】BZOJ 2816 [ZJOI2012]网络
Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...
- 洛谷 P3307: bzoj 3202: [SDOI2013] 项链
题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...
- 洛谷 4106 / bzoj 3614 [HEOI2014]逻辑翻译——思路+类似FWT
题目:https://www.luogu.org/problemnew/show/P4106 https://www.lydsy.com/JudgeOnline/problem.php?id=3614 ...
- 洛谷 P3332 BZOJ 3110 [ZJOI2013]K大数查询
题目链接 洛谷 bzoj 题解 整体二分 Code #include<bits/stdc++.h> #define LL long long #define RG register usi ...
- 洛谷 P2486 BZOJ 2243 [SDOI2011]染色
题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221” ...
- 洛谷 P2827 BZOJ 4721 UOJ #264 蚯蚓
题目描述 本题中,我们将用符号表示对c向下取整,例如:. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现在共有n只蚯蚓(n为正整数).每只 ...
随机推荐
- bzoj 1574: [Usaco2009 Jan]地震损坏Damage【dfs】
和March的那道不一样,只是非常单纯的带着贪心的dfs 首先一个点被隔断,与它相邻的所有点也会被隔断,打上删除标记,从1dfs即可 #include<iostream> #include ...
- bzoj1528 sam-Toy Cars(贪心,优先队列)
「BZOJ1528」[POI2005] sam – Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Ja ...
- web界面bug-临时
一.登录页面 二.首页 三.项目 四.项目池 五.专家管理 六.审批 七.日/周报 八.设置
- 实数类型c++
数据类型 定义标识符 数值范围 占字节数 有效位数 单精度浮点数 float -3.4E+38-3.4E+38 4(32位) 6-7位 双精度浮点数 double -1.7E+308-1.7E+308 ...
- EditText(4)常用属性详解
常用的属性: 显示密码 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod( ...
- Visual C++ Windows 桌面应用程序样例(摘抄)
//================================== //Windows应用程序框架结构(例子) //参考:<Visual C++宝典>陈国建等编著 //======= ...
- [转]MySQL存储过程
转自:http://www.cnblogs.com/exmyth/p/3303470.html 14.1.1 创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE ...
- 继承static的注意点
继承static的注意点 singleton模式会使用 <?php class Auth { protected static $_instance = null; /** * 单用例入口 * ...
- 冒泡 [Python]
冒泡Python class BubbleSort: def __init__(self): self.initArr() def initArr(self): self.arrInfo = [60, ...
- C++(extern关键字的理解和作用深入)
extern关键字的理解和作用深入 extern是一个关键字,它告诉编译器存在着一个变量或者一个函数,如果在当前编译语句的前面中没有找到相应的变量或者函数, 也会在当前文件的后面或者其它文件中定义 引 ...