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是不是没有右儿子

  1. #include<cstdio>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. #define N 10001
  7.  
  8. void read(int &x)
  9. {
  10. x=; char c=getchar();
  11. while(!isdigit(c)) c=getchar();
  12. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  13. }
  14.  
  15. struct LCT
  16. {
  17. int ch[N][],fa[N];
  18. int key[N],mx[N];
  19. int d[N];
  20.  
  21. bool rev[N];
  22.  
  23. int st[N],top;
  24.  
  25. void update(int x)
  26. {
  27. mx[x]=key[x];
  28. mx[x]=max(mx[x],mx[ch[x][]]);
  29. mx[x]=max(mx[x],mx[ch[x][]]);
  30. }
  31.  
  32. void down(int x)
  33. {
  34. if(rev[x])
  35. {
  36. rev[x]^=;
  37. swap(ch[x][],ch[x][]);
  38. rev[ch[x][]]^=;
  39. rev[ch[x][]]^=;
  40. }
  41. }
  42.  
  43. bool getson(int x)
  44. {
  45. return ch[fa[x]][]==x;
  46. }
  47.  
  48. bool isroot(int x)
  49. {
  50. return ch[fa[x]][]!=x && ch[fa[x]][]!=x;
  51. }
  52.  
  53. void rotate(int x)
  54. {
  55. int y=fa[x],z=fa[y];
  56. bool k=ch[y][]==x;
  57. if(!isroot(y)) ch[z][ch[z][]==y]=x;
  58. ch[y][k]=ch[x][k^]; ch[x][k^]=y;
  59. fa[y]=x; fa[x]=z; fa[ch[y][k]]=y;
  60. update(y);
  61. }
  62.  
  63. void splay(int x)
  64. {
  65. st[top=]=x;
  66. for(int i=x;!isroot(i);i=fa[i])
  67. st[++top]=fa[i];
  68. for(int i=top;i;--i) down(st[i]);
  69. int y;
  70. while(!isroot(x))
  71. {
  72. y=fa[x];
  73. if(!isroot(y)) rotate(getson(x)==getson(y) ? y : x);
  74. rotate(x);
  75. }
  76. update(x);
  77. }
  78.  
  79. void access(int x)
  80. {
  81. int t=;
  82. while(x)
  83. {
  84. splay(x);
  85. ch[x][]=t;
  86. update(x);
  87. t=x; x=fa[x];
  88. }
  89. }
  90.  
  91. void make_root(int x)
  92. {
  93. access(x);
  94. splay(x);
  95. rev[x]^=;
  96. }
  97.  
  98. void link(int x,int y)
  99. {
  100. make_root(x);
  101. fa[x]=y;
  102. d[x]++; d[y]++;
  103. splay(x);
  104. }
  105.  
  106. void cut(int x,int y)
  107. {
  108. make_root(x);
  109. access(y);
  110. splay(y);
  111. ch[y][]=fa[x]=;
  112. update(y);
  113. d[x]--; d[y]--;
  114. }
  115.  
  116. int findroot(int x)
  117. {
  118. access(x);
  119. splay(x);
  120. while(ch[x][]) x=ch[x][];
  121. return x;
  122. }
  123.  
  124. bool query(int x,int y)
  125. {
  126. int a=findroot(x);
  127. int b=findroot(y);
  128. return a==b;
  129. }
  130.  
  131. bool query_edge(int u,int v)
  132. {
  133. make_root(u);
  134. access(v);
  135. splay(v);
  136. return fa[u]==v && !ch[u][];
  137. }
  138.  
  139. }Lct[];
  140.  
  141. int main()
  142. {
  143. freopen("networkzj.in","r",stdin);
  144. freopen("networkzj.out","w",stdout);
  145. int n,m,c,q;
  146. read(n); read(m); read(c); read(q);
  147. int u,v,w,k;
  148. for(int i=;i<=n;++i)
  149. {
  150. read(w);
  151. for(int j=;j<c;++j) Lct[j].key[i]=Lct[j].key[i]=w;
  152. }
  153. while(m--)
  154. {
  155. read(u); read(v); read(w);
  156. Lct[w].link(u,v);
  157. }
  158. while(q--)
  159. {
  160. read(k);
  161. if(!k)
  162. {
  163. read(u); read(w);
  164. for(int i=;i<c;++i)
  165. {
  166. Lct[i].make_root(u);
  167. Lct[i].key[u]=w;
  168. Lct[i].update(u);
  169. }
  170. }
  171. else if(k==)
  172. {
  173. read(u); read(v); read(w);
  174. int i;
  175. for(i=;i<c;++i)
  176. if(Lct[i].query_edge(u,v) ) break;
  177. if(i==c) { puts("No such edge."); continue; }
  178. if(i==w) { puts("Success."); continue; }
  179. if(Lct[w].d[u]== || Lct[w].d[v]==) { puts("Error 1."); continue; }
  180. if(Lct[w].query(u,v)) { puts("Error 2."); continue; }
  181. Lct[i].cut(u,v);
  182. Lct[w].link(u,v);
  183. puts("Success.");
  184. }
  185. else
  186. {
  187. read(w); read(u); read(v);
  188. if(!Lct[w].query(u,v)) { puts("-1"); continue; }
  189. Lct[w].make_root(u);
  190. Lct[w].access(v);
  191. Lct[w].splay(v);
  192. cout<<Lct[w].mx[v]<<'\n';
  193. }
  194. }
  195. }

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. 6、Docker图形化管理(Portainer)

    一.Portainer简介 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控 ...

  2. 设计模式 笔记 命令模式 Command

    //---------------------------15/04/25---------------------------- //Conmmand  命令模式----对象行为型模式 /* 1:意 ...

  3. 代理神器allproxy

    背景 allproxy意为all as proxy,即是说所有设备均可以成为一个网络代理,唯一的要求就是有网络访问权限. 一般的代理软件要求宿主机必须有公网地址,然后才能把网络代理出去,但在实际情况下 ...

  4. 【读书笔记】Linux内核设计与实现(第五章)

    5.1 内核通信 系统调用在用户空间和硬件设备之间添加了一个中间层. 该层主要作用: 1.为用户空间提供了一种硬件的抽象接口. 2.保证了系统的稳定和安全. 3.每个进程都运行在虚拟系统中. 在Lin ...

  5. 20135337朱荟潼 Linux第七周学习总结——可执行程序的装载

    朱荟潼 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 第七周 Linu ...

  6. Alpha 冲刺报告模板

    Alpha 冲刺报告模板 Deadline: 十分钟左右站立会议,控制好时间,不要在此会议上讨论细节问题. 每组一份博客,组内共享,每人都需提交. 模板 队名:xxx 组员1(组长) 今天完成了哪些任 ...

  7. [2017BUAA软工]个人阅读作业+总结

    阅读作业 没有银弹 No Silver Bullet - Essence and Accidents of Software Engineering - Brooks 在这篇论文中,作者阐述了软件的四 ...

  8. OneZero第二周第五次站立会议(2016.4.1)

    会议时间:2016年4月1日 会议成员:冉华,张敏,夏一鸣.(王请假). 会议目的:汇报前一天工作,会议成员评论. 会议内容: 1.前端,由夏,张负责汇报,完成前端功能,待命. 2.数据逻辑控制,由王 ...

  9. 通过动态包含和Ajax机制抽取Web应用的公共页面

    在Java Web应用开发中,经常遇到的一种情况是,许多的页面中都包含着“公共页面”,这部分动态页面的特征是:访问量大,会带来较大的性能压力.功能设计上会动态地改变自身的元素.比如在登录前和登录后所展 ...

  10. bzoj5301[CQOI2018]异或序列

    题意 已知一个长度为 n 的整数数列 a[1],a[2],-,a[n] ,给定查询参数 l.r ,问在 [l,r] 区间内,有多少连续子 序列满足异或和等于 k . 也就是说,对于所有的 x,y (l ...