题目:

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)));
}
}
}

最新文章

  1. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
  2. freeradius 安装出错的解决办法
  3. Javascript/jQuery 获取地址栏URL参数的方法
  4. http://five-js.envylabs.com/
  5. JCreator的配置
  6. 第四章 函数(JavaScript:语言精粹)
  7. 云端持续集成——AppVeyor拥抱GitHub
  8. 【Python】Python XML 读写
  9. Python 番外 消息队列设计精要
  10. 每日算法之三十三:Trapping Rain Water
  11. 「技巧」如何将Sketch改为深色模式
  12. Day055--MySQL--外键的变种,表与表的关系,单表查询,多表查询, 内连接,左右连接,全外连接
  13. 毕业季,我的Linux求职之路
  14. cgi fast-cig php-fpm
  15. openCV 色彩空间
  16. HTML5上传文件显示进度
  17. 微信小程序笔记&lt;六&gt;模块化 —— module.exports
  18. 4-4-串的KMP匹配算法-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版
  19. Java设计模式(10)代理模式(Proxy模式)
  20. java I/O流类概述

热门文章

  1. 问题 F: 等比数列
  2. JS let和const关键字
  3. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第四节
  4. js当中mouseover和mouseout多次触发(非冒泡)
  5. 解决Jquery中使用each循环时,循环外的js依旧会执行
  6. Bootstrap 历练实例 - 按钮(Button)插件复选框
  7. sass安装更新及卸载方法
  8. SQLSERVER存储过程的基本语法实例
  9. 【转】【win网络编程】socket中的recv阻塞和select的用法
  10. django+xadmin在线教育平台(五)