[ZJOI2012][bzoj 2816] 网络 network [LCT]
题目:
http://www.lydsy.com/JudgeOnline/problem.php?id=2816
思路:
第一个条件看完暂时还没什么想法
看完第二个,发现每一个颜色都是一个森林
进而想到对于每一个颜色维护LCT
看数据范围,n<=10000, c<=10,可行
对于操作0,把每一个LCT上该店的权值都修改
对于操作1,先检测这条边是否存在,若不存在就No such edge
如果这条边存在,且原来的颜色不同于要修改的颜色的话,就先判断Error 1和Error 2
如果要改的边已经是目标颜色了,就特判跳过
上述判断都通过了就cut和link即可
对于操作2,每一个节点维护一个最大值,输出即可
Code:
/**************************************************************
Problem: 2816
User: dedicatus545
Language: C++
Result: Accepted
Time:8032 ms
Memory:6688 kb
****************************************************************/ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<unistd.h>
#define col(i,c) n*c+i
using namespace std;
inline int read(){
int re=,flag=;char ch=getchar();
while(ch>''||ch<''){
if(ch=='-') flag=-;
ch=getchar();
}
while(ch>=''&&ch<='') re=(re<<)+(re<<)+ch-'',ch=getchar();
return re*flag;
}
struct edge{
int to,next,w;
}e[];
int n,m,C,cnt=-,q,first[],color[][];
int fa[]={},ch[][]={},w[],a[];
bool rev[]={},rt[]={};
void _swap(int &l,int &r){l^=r;r^=l;l^=r;}
void update(int x){a[x]=max(a[ch[x][]],max(a[ch[x][]],w[x]));};
void pushrev(int x){
if(!x) return;
_swap(ch[x][],ch[x][]);
rev[x]^=;
}
void pushdown(int x){
if(!x) return;
if(rev[x]){
pushrev(ch[x][]);
pushrev(ch[x][]);
rev[x]=;
}
}
void push(int x){
//cout<<"push "<<x<<' '<<rt[x]<<'\n';
if(!x) sleep();
if(!rt[x]) push(fa[x]);
pushdown(x);
}
int get(int x){return ch[fa[x]][]==x;}
void rotate(int x){
//cout<<"rotate "<<x<<'\n';
int f=fa[x],ff=fa[f],son=get(x);
ch[f][son]=ch[x][son^];
if(ch[f][son]) fa[ch[f][son]]=f;
ch[x][son^]=f;fa[f]=x;
fa[x]=ff;
if(rt[f]) rt[x]=,rt[f]=;
else ch[ff][ch[ff][]==f]=x;
update(f);update(x);
}
void splay(int x){
//cout<<"splay "<<x<<'\n';
push(x);
for(int f;!rt[x];rotate(x))
if(!rt[f=fa[x]])
rotate((get(x)==get(f))?f:x);
update(x);
}
void access(int x){
//cout<<"access "<<x<<'\n';
int y=;
do{
splay(x);
rt[ch[x][]]=;
rt[ch[x][]=y]=;
x=fa[y=x];
update(x);
}while(x);
}
void makeroot(int x){
//cout<<"makeroot "<<x<<'\n';
access(x);splay(x);pushrev(x);
}
bool judge(int x,int y){
while(fa[x]) x=fa[x];
while(fa[y]) y=fa[y];
return x==y;
}
void init(int x,int y){
//cout<<"init "<<x<<' '<<y<<'\n';
makeroot(x);fa[x]=y;
}
int link(int x,int y){
if(judge(x,y)) return ;
makeroot(x);fa[x]=y;
return ;
}
int cut(int x,int y){
if(!judge(x,y)) return ;
makeroot(x);splay(y);
fa[ch[y][]]=fa[y];
rt[ch[y][]]=;
fa[y]=;ch[y][]=;
return ;
}
void add(int u,int v,int w){
e[++cnt]=(edge){v,first[u],w};first[u]=cnt;
e[++cnt]=(edge){u,first[v],w};first[v]=cnt;
}
int split(int u,int v){
if(!judge(u,v)) return -;
makeroot(u);access(v);splay(v);
return a[v];
}
int main(){
// freopen("networkzj.in","r",stdin);
// freopen("networkzj.out","w",stdout);
memset(first,-,sizeof(first));
int i,j,t1,t2,t3,t4;
n=read();m=read();C=read();q=read();
for(i=;i<=n;i++){
t1=read();
for(j=;j<C;j++) a[col(i,j)]=w[col(i,j)]=t1,rt[col(i,j)]=;
} for(i=;i<=m;i++){
t1=read();t2=read();t3=read();
init(col(t1,t3),col(t2,t3));
add(t1,t2,t3);
color[t1][t3]++;color[t2][t3]++;
}
//for(i=1;i<=n;i++){
//for(j=0;j<C;j++) cout<<color[i][j]<<' ';
//cout<<'\n';
//}
for(i=;i<=q;i++){
//cout<<"operation "<<i<<'\n';
t1=read();
if(t1==){
t2=read();t3=read();
for(j=;j<C;j++){
makeroot(col(t2,j));w[col(t2,j)]=t3;update(col(t2,j));
}
}
if(t1==){
t2=read();t3=read();t4=read();bool flag=,f=;
for(j=first[t2];~j;j=e[j].next)
if(e[j].to==t3){
flag=;
if(e[j].w==t4) f=;
}
if(flag){
printf("No such edge.\n");
continue;
}
if(f&&(color[t2][t4]==||color[t3][t4]==)){
printf("Error 1.\n");continue;
}
if(f&&(judge(col(t2,t4),col(t3,t4)))){
printf("Error 2.\n");continue;
}
for(j=first[t2];~j;j=e[j].next){
if(e[j].to==t3){
color[t2][e[j].w]--;color[t3][e[j].w]--;
cut(col(t2,e[j].w),col(t3,e[j].w));
e[j].w=e[j^].w=t4;
color[t2][t4]++;color[t3][t4]++;
link(col(t2,t4),col(t3,t4));
printf("Success.\n");break;
}
}
}
if(t1==){
t2=read();t3=read();t4=read();
printf("%d\n",split(col(t3,t2),col(t4,t2)));
}
}
}
[ZJOI2012][bzoj 2816] 网络 network [LCT]的更多相关文章
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- Docker 外部访问容器Pp、数据管理volume、网络network 介绍
Docker 外部访问容器Pp.数据管理volume.网络network 介绍 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来 指定端口映射. ...
- 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]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...
- 【刷题】BZOJ 2816 [ZJOI2012]网络
Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- bzoj 2816: [ZJOI2012]网络(splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...
- 洛谷 2173 BZOJ 2816 [ZJOI2012]网络
[题解] 明显的LCT模板题,c种颜色就开c棵LCT好了.. #include<cstdio> #include<algorithm> #define N 100010 #de ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
随机推荐
- 问题 B: Curriculum Vitae
问题 B: Curriculum Vitae 时间限制: 1 Sec 内存限制: 128 MB提交: 109 解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...
- mongdb 一些操作
一.命令操作数据库1.管理员身份打开cmd2.进到mongdb的mongo.exe文件所在路径3.show dbs 查看mongodb4.连接远程数据库:mongo ip:端口/数据库5.打开某个数据 ...
- 基于 Nginx && Lua 的简易CC防护方案
零.前言 1.CC攻击简述 CC攻击(Challenge Collapsar)是常见网站应用层攻击的一种,目的是消耗服务器资源,降低业务响应效率:极端情况会让站点无法正常提供服务: 2.本文要点 旨在 ...
- css代码
#footr { background: #3e434a } #header #blogTitle { background: url("http://images.cnblogs.com/ ...
- C#Aspose操作Word & Excel简版(后会研究补充更多功能)
利用Aspose操作Word & Excel首先要在项目中标引用Aspose.Words.dll和Aspose.Cells.dll. 首先说一说向Word中写入数据,目前做的是向Word中的标 ...
- 为啥国内互联网公司都用centos而不是ubuntu?
一直以来都很好奇ubuntu和centos有啥区别,上学时接触的都是ubuntu,自己每次装virtual box的时候都会下个ubuntu,但是公司的服务器上装的都是centos,今天查了下知乎网友 ...
- 【前端_js】ajax的应用
1.设置请求头部 function makeRequest() { alert("inside makeRequest()"); var settings = { type: &q ...
- 第2 章Python 语言基础
必背必记 1.转义字符 Python 中的字符串还支持转义字符.所谓转义字符是指使用反斜杠“\”对一些特殊字符进行转义. \ 续行符 \n 换行符 \0 空 \t 水平制表符,用于横向跳到下一制表 ...
- 三十五、MySQL 运算符
MySQL 运算符 本章节我们主要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下几种运算符: 算术运算符 比较运算符 逻辑运算符 位运算符 算术运算符 MySQL 支持的算术运算 ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之1、开篇
源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...