[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了. ---- ...
随机推荐
- cout对象一些常用方法的总结
cout.precision(n); 这个方法的功能是,设置精度为n,返还值是上一次的设置精度. #include <iostream> using namespace std; int ...
- install ipython-notebook
http://it.010lm.com/os/LINUX/182036.html ipython[notebook]安装(Linux平台) 1. 环境 操作系统:ubuntukylin 2. 操作步骤 ...
- JQuery模拟点击页面上的所有a标签,触发onclick事件
注意: 这种方法需要给所有的a标签加上id属性 页面加载完成模拟点击所有的a标签: <script> $(function () { // 模拟点击页面上的所有a标签,触发onclick事 ...
- 【计数】cf223C. Partial Sums
考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...
- Python_三级目录
程序要求: 1. 使用字典存储 1. 可以一层一层的进入到所有层2. 可以在每层返回上一层3. 可以在任意层退出 三级目录写了两个版本,第一个版本是刚看完字典写出来的,代码很多冗余,很多重复. men ...
- HTML5页面元素中的文本最快速替换replace()方法
$.ajax({ type:"get", url:spanUrl, dataType:'jsonp', jsonpCallback:'jsonp',//jsonp数据,需要数据库提 ...
- 【PHP】$_SERVER整理
PHP变成中经常需要用到服务器的一些资料,我在这里整理一下,方便查找.第一部分为比较常用的$_SERVER $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERV ...
- 微信小程序 onLoad 函数
小程序注册完成后,加载页面,触发onLoad方法. 页面载入后触发onShow方法,显示页面. 首次显示页面,会触发onReady方法,渲染页面元素和样式,一个页面只会调用一次. 当小程序后台运行或跳 ...
- 数据分析处理库Pandas——概述
导入Pandas库 创建DataFrame结构 读取.csv文件 titanic_train.csv文件:https://files.cnblogs.com/files/gloria-zhang/ti ...
- SoapUI(一)之webservice测试
webservice测试需要具备的条件: 1.了解业务需求:如从客户端发送一个post请求给服务器,服务器将响应传给客户端. 2.需要一个明确的wsdl地址: 如天气预报的接口链接:http://ww ...