博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P2173 [ZJOI2012]网络
阅读量:4521 次
发布时间:2019-06-08

本文共 2772 字,大约阅读时间需要 9 分钟。

如果只有一种颜色,显然 $LCT$

多种颜色,发现颜色不多,所以对每一种颜色建 $LCT$

编号 $c$ 的颜色的第 $i$ 个节点在 $LCT$ 中编号 $c*n+i$

改颜色的时候有一堆细节,具体来讲

用 $map$ 来判断两点之间是否有边并记录边的颜色,注意边 $(x,y)$ 和 $(y,x)$ 是等价的

记录 $cnt[i][c]$ 维护节点 $i$ 颜色 $c$ 的边数

用 $findroot$ 判断两点之间是否已经有路径

注意改颜色的时候颜色可能不变!一定要特判!

注意边的颜色从 $0$ 开始

 

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f;}const int N=1e6+7;int c[N][2],fa[N],val[N],mx[N];bool rev[N];inline void pushup(int x) { mx[x]=max(mx[c[x][0]], max(val[x],mx[c[x][1]]) ); }inline void pushdown(int x){ if(!rev[x]||!x) return; int &lc=c[x][0],&rc=c[x][1]; swap(lc,rc); rev[x]=0; if(lc) rev[lc]^=1; if(rc) rev[rc]^=1;}inline void rever(int x) { rev[x]=1; pushdown(x); }inline bool noroot(int x) { return (c[fa[x]][0]==x)|(c[fa[x]][1]==x); }inline void rotate(int x){ int y=fa[x],z=fa[y],d=(c[y][1]==x); if(noroot(y)) c[z][c[z][1]==y]=x; fa[x]=z; fa[y]=x; fa[c[x][d^1]]=y; c[y][d]=c[x][d^1]; c[x][d^1]=y; pushup(y); pushup(x);}void push_tag(int x){ if(noroot(x)) push_tag(fa[x]); else pushdown(x); pushdown(c[x][0]); pushdown(c[x][1]);}inline void splay(int x){ push_tag(x); while(noroot(x)) { int y=fa[x],z=fa[y]; if(noroot(y)) { if(c[y][0]==x ^ c[z][0]==y) rotate(x); else rotate(y); } rotate(x); }}inline void access(int x){ for(int y=0;x;y=x,x=fa[x]) splay(x),c[x][1]=y,pushup(x);}inline void makeroot(int x) { access(x); splay(x); rever(x); }inline int findroot(int x){ access(x); splay(x); pushdown(x); while(c[x][0]) x=c[x][0],pushdown(x); splay(x); return x;}inline int split(int x,int y) { makeroot(x); access(y); splay(y); return mx[y]; }//返回最大值inline void link(int x,int y) { makeroot(x); if(findroot(y)!=x) fa[x]=y; }inline void cut(int x,int y){ makeroot(x); if(findroot(y)!=x||fa[y]!=x||c[y][0]) return; fa[y]=c[x][1]=0; pushup(x);}int n,m,C,K,cnt[N][11];struct edge{ int x,y; inline bool operator < (const edge &tmp) const { return x!=tmp.x ? x
mp;int main(){ int a,b,c,opt; n=read(),m=read(),C=read(),K=read(); for(int i=1;i<=n;i++) { a=read(); for(int j=0;j
1||cnt[b][c]>1)) { printf("Error 1.\n"); continue; } if(findroot(c*n+a)==findroot(c*n+b)) { printf("Error 2.\n"); continue; } cnt[a][c]++,cnt[b][c]++; cnt[a][p]--,cnt[b][p]--; mp[(edge){a,b}]=c+1;//更新一堆东西 cut(p*n+a,p*n+b); link(c*n+a,c*n+b); printf("Success.\n"); } if(opt==2) { c=read(); if(findroot(a*n+b)!=findroot(a*n+c)) { printf("-1\n"); continue; } printf("%d\n",split(a*n+b,a*n+c)); } } return 0;}

 

转载于:https://www.cnblogs.com/LLTYYC/p/11175327.html

你可能感兴趣的文章
Spring MVC 学习总结(五)——校验与文件上传
查看>>
160505、oracle 修改字符集 修改为ZHS16GBK
查看>>
Spring 4 官方文档学习 Spring与Java EE技术的集成
查看>>
cocos+kbe问题记录
查看>>
自动化测试框架selenium+java+TestNG——配置篇
查看>>
测量标准体重
查看>>
(转)关于Android中为什么主线程不会因为Looper.loop()里的死循环卡死?引发的思考,事实可能不是一个 epoll 那么 简单。...
查看>>
SQL*Plus 系统变量之32 - NEWP[AGE]
查看>>
Spring配置文件总结
查看>>
4.三角形面积
查看>>
基础-事务
查看>>
MAC下安装与配置MySQL [转]
查看>>
ERROR: ld.so: object '/usr/lib64/libtcmalloc.so.4' from LD_PRELOAD cannot be preloaded: ignored
查看>>
爬虫入门【10】Pyspider框架简介及安装说明
查看>>
android面试(4)---文件存储
查看>>
(转载) 标准C中的字符串操作函数
查看>>
如何提高android串口kernel log等级
查看>>
Docker快速配置指南
查看>>
Python基础---OS模块 (二)
查看>>
【JS点滴】substring和substr以及slice和splice的用法和区别。
查看>>