http://www.lydsy.com/JudgeOnline/problem.php?id=2816

每种颜色搞一个LCT

判断u v之间有边直接相连:

如果u和v之间有边相连,那么他们的深度相差1

所以

make_root(u);

access(v);

splay(v);

判断u的父亲是不是v 以及 u是不是没有右儿子

#include<cstdio>
#include<iostream> using namespace std; #define N 10001 void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} struct LCT
{
int ch[N][],fa[N];
int key[N],mx[N];
int d[N]; bool rev[N]; int st[N],top; void update(int x)
{
mx[x]=key[x];
mx[x]=max(mx[x],mx[ch[x][]]);
mx[x]=max(mx[x],mx[ch[x][]]);
} void down(int x)
{
if(rev[x])
{
rev[x]^=;
swap(ch[x][],ch[x][]);
rev[ch[x][]]^=;
rev[ch[x][]]^=;
}
} bool getson(int x)
{
return ch[fa[x]][]==x;
} bool isroot(int x)
{
return ch[fa[x]][]!=x && ch[fa[x]][]!=x;
} void rotate(int x)
{
int y=fa[x],z=fa[y];
bool k=ch[y][]==x;
if(!isroot(y)) ch[z][ch[z][]==y]=x;
ch[y][k]=ch[x][k^]; ch[x][k^]=y;
fa[y]=x; fa[x]=z; fa[ch[y][k]]=y;
update(y);
} void splay(int x)
{
st[top=]=x;
for(int i=x;!isroot(i);i=fa[i])
st[++top]=fa[i];
for(int i=top;i;--i) down(st[i]);
int y;
while(!isroot(x))
{
y=fa[x];
if(!isroot(y)) rotate(getson(x)==getson(y) ? y : x);
rotate(x);
}
update(x);
} void access(int x)
{
int t=;
while(x)
{
splay(x);
ch[x][]=t;
update(x);
t=x; x=fa[x];
}
} void make_root(int x)
{
access(x);
splay(x);
rev[x]^=;
} void link(int x,int y)
{
make_root(x);
fa[x]=y;
d[x]++; d[y]++;
splay(x);
} void cut(int x,int y)
{
make_root(x);
access(y);
splay(y);
ch[y][]=fa[x]=;
update(y);
d[x]--; d[y]--;
} int findroot(int x)
{
access(x);
splay(x);
while(ch[x][]) x=ch[x][];
return x;
} bool query(int x,int y)
{
int a=findroot(x);
int b=findroot(y);
return a==b;
} bool query_edge(int u,int v)
{
make_root(u);
access(v);
splay(v);
return fa[u]==v && !ch[u][];
} }Lct[]; int main()
{
freopen("networkzj.in","r",stdin);
freopen("networkzj.out","w",stdout);
int n,m,c,q;
read(n); read(m); read(c); read(q);
int u,v,w,k;
for(int i=;i<=n;++i)
{
read(w);
for(int j=;j<c;++j) Lct[j].key[i]=Lct[j].key[i]=w;
}
while(m--)
{
read(u); read(v); read(w);
Lct[w].link(u,v);
}
while(q--)
{
read(k);
if(!k)
{
read(u); read(w);
for(int i=;i<c;++i)
{
Lct[i].make_root(u);
Lct[i].key[u]=w;
Lct[i].update(u);
}
}
else if(k==)
{
read(u); read(v); read(w);
int i;
for(i=;i<c;++i)
if(Lct[i].query_edge(u,v) ) break;
if(i==c) { puts("No such edge."); continue; }
if(i==w) { puts("Success."); continue; }
if(Lct[w].d[u]== || Lct[w].d[v]==) { puts("Error 1."); continue; }
if(Lct[w].query(u,v)) { puts("Error 2."); continue; }
Lct[i].cut(u,v);
Lct[w].link(u,v);
puts("Success.");
}
else
{
read(w); read(u); read(v);
if(!Lct[w].query(u,v)) { puts("-1"); continue; }
Lct[w].make_root(u);
Lct[w].access(v);
Lct[w].splay(v);
cout<<Lct[w].mx[v]<<'\n';
}
}
}

bzoj千题计划223:bzoj2816: [ZJOI2012]网络的更多相关文章

  1. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  2. bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量

    http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...

  3. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  4. bzoj千题计划192:bzoj1569: [JSOI2008]Blue Mary的职员分配

    http://www.lydsy.com/JudgeOnline/problem.php?id=1569 dp[i][j][a][b] 表示i个职员,发广告状态为j,已有金钱a,声誉b的最少天数 j= ...

  5. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  6. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  7. bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...

  8. bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...

  9. bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹

    http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...

随机推荐

  1. BLCR技术初探

    BLCR技术到底是什么技术?我没空和你乱扯,自己去看该官方网站的介绍:http://crd.lbl.gov/groups-depts/ftg/projects/current-projects/BLC ...

  2. SpringBoot日记——错误页处理的配置篇

    在我们访问页面的时候经常会遇到各种问题,比如404,400,500,502等等,可返回的错误页对用户来讲,并不太亲民,所以要定制一下自己的错误页. 我们先访问一个错误页面,看下效果:(虽然给我们提供了 ...

  3. 使用node-webkit(v0.35.5)和innosetup(3.6.1)打包将web程序打包为桌面客户端(安装包)

    这边主要是有一个客户,需要在电视机上安装一个客户端,含有视频直播功能:刚开始我们采用的webapp打包成apk安装在电视机上,发现摄像头监控画面根本无法播放(apk在手机上可以正常播放视频):排除一些 ...

  4. GitHub 新手教程 三,Git Bash

    1,通过 开始菜单 启动 Git Bash,或者 在 cmd 下执行以下命令: D:\SoftWare\Git\git-bash.exe --cd-to-home (D:\SoftWare\Git 是 ...

  5. 机器学习初入门03 - Matplotlib

    这一部分很简单,所以以代码的形式给出,在实际学习开发中,Matplotlib最好只把它当成一个画图的工具来用,没有必要深究其实现原理是什么. 一.折线图的绘制 import pandas as pd ...

  6. Linux DDos防御

    今天要说的就是一款能够自动屏蔽DDOS攻击者IP的软件:DDoS deflate. DDoS deflate介绍 DDoS deflate是一款免费的用来防御和减轻DDoS攻击的脚本.它通过netst ...

  7. YQCB冲刺第二周第五天

    今天的任务为实现由用户设置每月初始额度的功能. 昨天的任务为实现精准查账的功能. 遇到的问题为界面的布局以及精准查账按什么标准查找,最后决定按分类查账与时间查账相结合. 站立会议 任务面板

  8. C#获取每月最后一天或者最末一天的方法

    /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="datetime">要取得 ...

  9. 第十一周(11.24-12.01)----ptim测试程序运行速度

    我在dos下用ptime指令对分数运算(http://www.cnblogs.com/YangXiaomoo/p/6095583.html)的运行时间进行了测试.测试结果一般都在0.212-0.217 ...

  10. App phonegap

    云端打包 https://build.phonegap.com/apps phonegap PC端下载 https://www.phonegap.com/getstarted/ 移动端下载 https ...