2816

思路:

  多个LCT;

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 10005
  4. #define ll long long
  5. int val[maxn];
  6. struct LinkCutTreeType {
  7. int f[maxn],Max[maxn],ch[maxn][],rev[maxn],sta[maxn],top,cnt[maxn];
  8. void updata(int now)
  9. {
  10. Max[now]=val[now];
  11. if(ch[now][]) Max[now]=max(Max[now],Max[ch[now][]]);
  12. if(ch[now][]) Max[now]=max(Max[now],Max[ch[now][]]);
  13. }
  14. void downdata(int now)
  15. {
  16. if(rev[now])
  17. {
  18. rev[now]^=,swap(ch[now][],ch[now][]);
  19. if(ch[now][]) rev[ch[now][]]^=;
  20. if(ch[now][]) rev[ch[now][]]^=;
  21. }
  22. }
  23. bool isroot(int now)
  24. {
  25. return (ch[f[now]][]!=now)&&(ch[f[now]][]!=now);
  26. }
  27. void rotate(int now)
  28. {
  29. int fa=f[now],ffa=f[fa],l=(ch[fa][]==now),r=l^;
  30. if(!isroot(fa)) ch[ffa][ch[ffa][]==fa]=now;
  31. f[now]=ffa,f[fa]=now,ch[fa][l]=ch[now][r],ch[now][r]=fa;
  32. if(ch[fa][l]) f[ch[fa][l]]=fa;updata(fa);
  33. }
  34. void splay(int now)
  35. {
  36. top=,sta[]=now;int fa,ffa;
  37. for(int i=now;!isroot(i);i=f[i]) sta[++top]=f[i];
  38. while(top) downdata(sta[top--]);
  39. while(!isroot(now))
  40. {
  41. fa=f[now],ffa=f[fa];
  42. if(!isroot(fa)) rotate(((ch[ffa][]==fa)^(ch[fa][]==now))?now:fa);
  43. rotate(now);
  44. }
  45. updata(now);
  46. }
  47. void access(int now)
  48. {
  49. for(int i=;now;i=now,now=f[now]) splay(now),ch[now][]=i;
  50. }
  51. void makeroot(int now)
  52. {
  53. access(now),splay(now),rev[now]^=;
  54. }
  55. void cut(int x,int y)
  56. {
  57. makeroot(x),access(y),splay(y);
  58. f[x]=,ch[y][]=,cnt[x]--,cnt[y]--;
  59. }
  60. void link(int x,int y)
  61. {
  62. makeroot(x),f[x]=y,splay(x),cnt[x]++,cnt[y]++;
  63. }
  64. bool iscon(int x,int y)
  65. {
  66. while(f[x]) x=f[x];
  67. while(f[y]) y=f[y];
  68. return x==y;
  69. }
  70. int query(int u,int v)
  71. {
  72. makeroot(u),access(v),splay(v);
  73. return Max[v];
  74. }
  75. };
  76. struct LinkCutTreeType lct[];
  77. int n,m,c,k;
  78. map<ll,int>Map;
  79. inline void in(int &now)
  80. {
  81. char Cget=getchar();now=;
  82. while(Cget>''||Cget<'')Cget=getchar();
  83. while(Cget>=''&&Cget<='')
  84. {
  85. now=now*+Cget-'';
  86. Cget=getchar();
  87. }
  88. }
  89. ll Mapped(int u,int v)
  90. {
  91. if(u>v) swap(u,v);
  92. return (ll)u*n*n+v;
  93. }
  94. int main()
  95. {
  96. //freopen("data.txt","r",stdin);
  97. freopen("networkzj.in","r",stdin);
  98. freopen("networkzj.out","w",stdout);
  99. in(n),in(m),in(c),in(k);int u,v,w,op;ll pos,id;
  100. for(int i=;i<=n;i++) in(val[i]);
  101. while(m--) in(u),in(v),in(w),Map[Mapped(u,v)]=w+,lct[w].link(u,v);
  102. while(k--)
  103. {
  104. in(op);
  105. if(op==)
  106. {
  107. in(u),in(val[u]);
  108. for(int i=;i<c;i++) lct[i].splay(u);
  109. }
  110. if(op==)
  111. {
  112. in(u),in(v),in(w),id=Mapped(u,v),pos=Map[id]-;
  113. if(pos<)
  114. {
  115. puts("No such edge.");
  116. continue;
  117. }
  118. if(pos==w)
  119. {
  120. puts("Success.");
  121. continue;
  122. }
  123. if(lct[w].cnt[u]>=||lct[w].cnt[v]>=)
  124. {
  125. puts("Error 1.");
  126. continue;
  127. }
  128. if(lct[w].iscon(u,v))
  129. {
  130. puts("Error 2.");
  131. continue;
  132. }
  133. lct[pos].cut(u,v),lct[w].link(u,v),Map[id]=w+;
  134. puts("Success.");
  135. }
  136. if(op==)
  137. {
  138. in(w),in(u),in(v);
  139. if(!lct[w].iscon(u,v))
  140. {
  141. puts("-1");
  142. continue;
  143. }
  144. printf("%d\n",lct[w].query(u,v));
  145. }
  146. }
  147. return ;
  148. }

AC日记——[ZJOI2012]网络 bzoj 2816的更多相关文章

  1. AC日记——无线网络发射器选址 洛谷 P2038

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  2. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  3. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  4. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  5. AC日记——[HNOI2014]世界树 bzoj 3572

    3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...

  6. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  7. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  8. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  9. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

随机推荐

  1. sql 建表以及查询---复杂查询之成绩排名

    废话不说,直接建表 1.表Player USE T4st -- 设置当前数据库为T4st,以便访问sysobjects IF EXISTS(SELECT * FROM sysobjects WHERE ...

  2. linux上抓包

    使用tcpdump命令. 使用tcpdump -help查看其用法. -i eth0:在第一块网卡上进行抓包. -w filename.cap:将抓的保存到当前目录下的filename.cap文件中, ...

  3. hdu 3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  4. 【bzoj1774-过路费】floyd+排序

    题意:n个点,m条双向边,每个点有权值c[i],每条边有权值a[i].d,一条路径的费用=每条边的权值和+各个点的权值的最大值,即sigma(a[i].d)+max(c[i]).q个询问,问x到y的最 ...

  5. 【BZOJ1598】牛跑步 [A*搜索]

    牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description BESSIE准备用从牛棚跑到池塘的方 ...

  6. 【NOIP】提高组2014 解方程

    [题意]已知n次方程(n<=100)及其所有系数(|ai|<=10^10000),求[1,m]中整数解的个数(m<=10^6). [算法]数论 [题解]如果f(x)=0,则有f(x) ...

  7. 解决vue代码缩进报错问题 关闭ESlint

    前言 使用vue-cli来构建单页SPA应用,提示代码缩进报错 原因分析 通过查看package.json文件我们可以发现,在文件中默认安装了eslint-loader模块,eslint-loader ...

  8. jqgrid 翻页记录选中行

    简单的jqgrid列表 $("#list").jqGrid({ url:contextPath + "/getList", postData: data, da ...

  9. promise 如何知道所有的回调都执行完了?

    var fs = require('fs'); /** * @return {object} Promise */ function doThing(fileName) { // ... // con ...

  10. DirectX介绍(转)

    原文转自 https://baike.baidu.com/item/Direct3D/910353