【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维护子树信息 ...
随机推荐
- Tarjan在图论中的应用(二)——用Tarjan来求割点与割边
前言:\(Tarjan\) 求割点和割边建立在 \(Tarjan\)算法的基础之上,因此建议在看这篇博客之前先去学一学\(Tarjan\). 回顾\(Tarjan\)中各个数组的定义 首先,我们来回顾 ...
- file - 确定文件类型
总览 file [ -bcnsvzL ] [ -f 命名文件 ] [ -m 幻数文件 ] file ... 描述 本手册页说明了3.27版本 file 命令的使用. File 命令试图检查每个参数以判 ...
- 标准输入输出 stdio 流缓冲 buffering in standard streams
From : http://www.pixelbeat.org/programming/stdio_buffering/ 译者:李秋豪 我发现找出标准流用的是什么缓冲是一件困难的事. 例如下面这个使用 ...
- linux apache 不解析php文件显示源码
首先检查是否安装PHP 没有的话就先安装 如果安装过 在/etc/httpd/conf/httpd.conf文件中 在<IfModule mime_module>里面 AddType ap ...
- java Html&JavaScript面试题:HTML 的 form 提交之前如何验证数值文本框的内容全部为数字? 否则的话提示用户并终止提交?
提交的验证方法(通过单个字符比较): <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- FILE对象线程安全
根据apue讲述: 标准的IO例程可能从它们各自的内部数据结构的角度出发,是以线程安全的方式实现的!但在线程中,如果标准 IO例程都获取它们各自的锁,那么在做一次一个字符的IO时就会出现严重的性能下降 ...
- pthread_cancel函数注意事项
/************************************************** 相关函数: #include <pthread.h> int pthread_can ...
- cf519C. A and B and Team Training(找规律)
题意 $a$个学生,$b$个教练 可以两个学生和一个教练一组,也可以两个教练和一个学生一组,问最多组成多少组 Sol 发题解的目的是为了纪念一下自己的错误思路 刚开始想的是:贪心的选,让少的跟多的分在 ...
- 洛谷P1111修复公路并查集改
看了他们的题解感觉很震惊,为什么要用kruskal,这题要用到最小生成树吗??? 38行短短的程序就可以了,我觉得学习不是一种套用,套自己学的,而且题解很大一部分都是kruskal. 个人认为自己的程 ...
- 为啥国内互联网公司都用centos而不是ubuntu?
一直以来都很好奇ubuntu和centos有啥区别,上学时接触的都是ubuntu,自己每次装virtual box的时候都会下个ubuntu,但是公司的服务器上装的都是centos,今天查了下知乎网友 ...