【Luogu】P2173网络(LCT)
这次坑我的是与或的结合顺序……
开十个LCT记录一下即可。以上。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<map>
#define maxn 210050
#define maxc 12
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Splay{
struct Node{
int e[],fa,val,maxi,tag;
}tree[maxn];
inline int iden(int x){ return x==tree[tree[x].fa].e[]; }
inline void connect(int x,int fa,int how){ tree[x].fa=fa; tree[fa].e[how]=x; }
inline bool isroot(int x){ return x!=tree[tree[x].fa].e[]&&x!=tree[tree[x].fa].e[]; }
inline void update(int x){ tree[x].maxi=max(tree[x].val,max(tree[tree[x].e[]].maxi,tree[tree[x].e[]].maxi)); }
void reverse(int x){
swap(tree[x].e[],tree[x].e[]);
tree[x].tag^=;
}
void pushdown(int x){
if(tree[x].tag==) return;
tree[x].tag=;
if(tree[x].e[]) reverse(tree[x].e[]);
if(tree[x].e[]) reverse(tree[x].e[]);
}
void rotate(int x){
int y=tree[x].fa; int r=tree[y].fa;
int sony=iden(x); int sonr=iden(y);
tree[x].fa=r; if(!isroot(y)) tree[r].e[sonr]=x;
int b=tree[x].e[sony^];
connect(b,y,sony);
connect(y,x,sony^);
update(y);
}
inline void pushto(int x){
if(!isroot(x)) pushto(tree[x].fa);
pushdown(x);
return;
}
void splay(int x){
pushto(x);
while(!isroot(x)){
int fa=tree[x].fa;
if(!isroot(fa))
if(iden(fa)==iden(x)) rotate(fa);
else rotate(x);
rotate(x);
}
update(x);
}
inline void access(int x){
int last=;
while(x){
splay(x);
tree[x].e[]=last;
update(x);
last=x; x=tree[x].fa;
}
}
inline void makeroot(int x){
access(x);
splay(x);
reverse(x);
}
inline int findroot(int x){
access(x);
splay(x);
while(tree[x].e[]) x=tree[x].e[];
return x;
}
inline void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
inline void link(int x,int y){
split(x,y);
tree[x].fa=y;
}
inline void cut(int x,int y){
split(x,y);
if(tree[y].e[]!=x||tree[x].e[]) return;
tree[x].fa=tree[y].e[]=;
}
}s[maxc]; inline long long calc(long long x,long long y,long long n){ return x*n+y; }
map<long long,int>d; int old[maxn];
int sum[maxn][maxc]; int main(){
int n=read(),m=read(),c=read(),e=read();
for(int i=;i<=n;++i){
int x=read();
for(int j=;j<=c;++j) s[j].tree[i].val=s[j].tree[i].maxi=x;
}
for(int i=;i<=m;++i){
int from=read(),to=read(),col=read();
sum[from][col]++; sum[to][col]++;
old[i+n]=col;
if(from>to) swap(from,to);
d[calc(from,to,n)]=i+n;
s[col].link(from,i+n);
s[col].link(to,i+n);
}
for(int i=;i<=e;++i){
int opt=read();
if(opt==){
int x=read(),y=read();
for(int j=;j<=c;++j){
s[j].splay(x);
s[j].tree[x].val=y;
s[j].update(x);
}
}
else if(opt==){
int from=read(),to=read(),col=read();
if(from>to) swap(from,to);
if(d.count(calc(from,to,n))==){
printf("No such edge.\n");
continue;
}
int id=d[calc(from,to,n)];
if(col==old[id]){
printf("Success.\n");
continue;
}
if(sum[from][col]==||sum[to][col]==){
printf("Error 1.\n");
continue;
}
if(s[col].findroot(from)==s[col].findroot(to)){
printf("Error 2.\n");
continue;
}
sum[from][old[id]]--; sum[to][old[id]]--;
sum[from][col]++; sum[to][col]++;
s[old[id]].cut(from,id);
s[old[id]].cut(to,id);
old[id]=col;
s[old[id]].link(from,id);
s[old[id]].link(to,id);
printf("Success.\n");
}
else{
int col=read(),x=read(),y=read();
if(s[col].findroot(x)!=s[col].findroot(y)){
printf("-1\n");
continue;
}
s[col].split(x,y);
//printf("%d %d>>>\n",y,s[col].tree[y].val);
printf("%d\n",s[col].tree[y].maxi);
}
}
return ;
}
【Luogu】P2173网络(LCT)的更多相关文章
- Luogu 2173 [ZJOI2012]网络 - LCT
Solution $LCT$ 直接上$QuQ$ 注意$cut$ 完 需要 $d[u + c * N]--$ 再 $link$, 不然会输出Error 1的哦 Code #include<cs ...
- Luogu P2173 [ZJOI2012]网络
题意 写的比较清楚,我就不解释了. \(\texttt{Data Range:}n\leq 10^4,m\leq 10^5,c\leq 10,k\leq 10^5\) 题解 注意到 \(c\) 的范围 ...
- ZJOI2012 网络——LCT相关题目
有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你 ...
- luoguP2173 [ZJOI2012]网络 LCT
链接 luogu 思路 颜色很少,开10个lct分别维护 if (Hash.count(make_pair(u, v)) && Hash[make_pair(u, v)] == col ...
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...
- BZOJ.2816.[ZJOI2012]网络(LCT)
题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...
- BZOJ2816:[ZJOI2012]网络(LCT)
Description 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构 ...
- 【luogu P3376 网络最大流】 模板
题目链接:https://www.luogu.org/problemnew/show/P3376 #include <iostream> #include <cstdio> # ...
- Luogu P4299 首都 LCT
既然是中文题目,这里便不给题意. 分析: 这个题的做法据说是启发式合并? 但是我不会啊…… 进入正题,LCT是怎样做掉这道题的.记得在前面的一篇<大融合>的题解中,介绍过LCT维护子树信息 ...
随机推荐
- bzoj3242 [Noi2013]快餐店
Description 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. 快餐店的顾客分布在城 ...
- java设计模式——单例模式(一)
一. 定义与类型 定义:保证一个类仅有一个实例,并提供一个全局访问点 类型:创建型 二. 适用场景 想确保任何情况下都绝对只用一个实例 三. 优缺点 优点: 在内存里只有一个实例,减少了内存开销 可以 ...
- C#箴言之用属性来访问类的私有成员
在程序中,难免要访问某个对象的私有成员.那么以前实现这类功能的方法有两种,第一种方法最简单,就是把成员访问符从“private”改为“public”即可:而另一个就是提供公有的成员访问函数来进行访问. ...
- shell脚本,awk结合正则来打印文件里面的内容。
文件内容如下:key1abc d key2 1.想得到如下结果: abc d 2.想得到如下结果: key1key2
- 搭建mock服务器(微信小程序)
搭建mock服务器(微信小程序) 如何在微信小程序使用mock.js实在是个问题,为了完全模拟访问路由和数据,选择在搭建本地mock服务器是一个不错的选择. 以下示例了一个mock服务器的搭建过程以及 ...
- popen和pclose详解及实例
popen函数是标准c提供的一个管道创建函数,其内部操作主 要是创建一个管道,调用fork创建子进程,关闭不需用的文件描述符,调用exec函数族执行popen的第一个参数.然后等到关闭. 也就是说我们 ...
- 安装配置mysql图文步骤以及配置mysql的环境变量的步骤
MySQL下载地址:http://dev.mysql.com/downloads/installer/ 我的数据库是5.5.21这个版本的.其实可以一直点击next,直到出现第14张图,从这里开始要注 ...
- vmware 开机自动启动
vmware开机自动启动, 可以使用vmrun命令. 1. 首先在“我的电脑”-“属性”-“高级”-“环境变量”-“PATH”中添加vmware路径,如:C:\Program Files (x86)\ ...
- 经典dfs(depth-first search)
DFS主要在于参数的改变; 样例输入: n=4 //给定n个数字 a={1,2,4,7} //输入n个数据 k=15 //目标数字 样例输 ...
- MTCNN学习进展
20190618 截止今日,学习了MTCNN预测部分的内容,包括三个网络输入输出之类的东西. 之后需要进一步学习的,NMS原理鞋机,MTCNN训练过程细节,损失函数细节