跟去年NOIP某题基本一样。

最小生成树之后,就变成了询问连接两点的路径上的权值最大的边。

倍增LCA、链剖什么的随便搞。

块状树其实也是很简单的,只不过每个点的点权要记录成“连接其与其父节点的边的权值”,然后暴力LCA时不要用LCA的值更新答案了。

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. #define maxn 15001
  7. int Res,Num;char C,CH[];
  8. inline int Ge()
  9. {
  10. Res=;C='*';
  11. while(C<''||C>'')C=getchar();
  12. while(C>=''&&C<=''){Res=Res*+(C-'');C=getchar();}
  13. return Res;
  14. }
  15. inline void P(int x)
  16. {
  17. Num=;if(!x){putchar('');puts("");return;}
  18. while(x>)CH[++Num]=x%,x/=;
  19. while(Num)putchar(CH[Num--]+);
  20. putchar('\n');
  21. }
  22. struct Edge{int u,v,w;void Read(){u=Ge();v=Ge();w=Ge();}};
  23. bool cmp(const Edge &a,const Edge &b){return a.w<b.w;}
  24. Edge edges[maxn<<];
  25. struct Graph
  26. {
  27. int v[maxn<<],first[maxn<<],next[maxn<<],w[maxn<<],en;
  28. void AddEdge(const int &a,const int &b)
  29. {v[++en]=b;next[en]=first[a];first[a]=en;}
  30. void AddEdge(const int &a,const int &b,const int &c)
  31. {v[++en]=b;w[en]=c;next[en]=first[a];first[a]=en;}
  32. };
  33. Graph G[];
  34. int fa[maxn],dep[maxn],top[maxn],siz[maxn],sz,maxv[maxn],W[maxn];
  35. int n,m,q,x,y;
  36. void makeblock(int cur)
  37. {
  38. for(int i=G[].first[cur];i;i=G[].next[i])
  39. if(G[].v[i]!=fa[cur])
  40. {
  41. dep[G[].v[i]]=dep[cur]+;
  42. W[G[].v[i]]=G[].w[i];
  43. fa[G[].v[i]]=cur;
  44. if(siz[top[cur]]<sz)
  45. {
  46. siz[top[cur]]++;
  47. top[G[].v[i]]=top[cur];
  48. G[].AddEdge(cur,G[].v[i]);
  49. }
  50. makeblock(G[].v[i]);
  51. }
  52. }
  53. int rank[maxn],father[maxn];
  54. void init(){for(int i=;i<=n;i++) father[i]=i;}
  55. int findroot(int x)
  56. {
  57. if(father[x]==x) return x;
  58. int rt=findroot(father[x]);
  59. father[x]=rt;
  60. return rt;
  61. }
  62. void Union(int U,int V)
  63. {
  64. if(rank[U]<rank[V]) father[U]=V;
  65. else
  66. {
  67. father[V]=U;
  68. if(rank[U]==rank[V]) rank[U]++;
  69. }
  70. }
  71. void dfs(int cur,int Maxnow)
  72. {
  73. maxv[cur]=Maxnow;
  74. for(int i=G[].first[cur];i;i=G[].next[i])
  75. dfs(G[].v[i],max(Maxnow,W[G[].v[i]]));
  76. }
  77. int Query_max(int u,int v)
  78. {
  79. int res=-;
  80. while(u!=v)
  81. {
  82. if(top[u]==top[v])
  83. {
  84. if(dep[u]<dep[v]) swap(u,v);
  85. res=max(res,W[u]);
  86. u=fa[u];
  87. }
  88. else
  89. {
  90. if(dep[top[u]]<dep[top[v]]) swap(u,v);
  91. res=max(res,maxv[u]);
  92. u=fa[top[u]];
  93. }
  94. }
  95. return res;
  96. }
  97. int main()
  98. {
  99. n=Ge();m=Ge();q=Ge();
  100. for(int i=;i<=m;i++) edges[i].Read();
  101. sort(edges+,edges+m+,cmp);
  102. init();
  103. int cnt=;
  104. for(int i=;i<=m;i++)
  105. {
  106. int f1=findroot(edges[i].u),f2=findroot(edges[i].v);
  107. if(f1!=f2)
  108. {
  109. Union(f1,f2);
  110. G[].AddEdge(edges[i].u,edges[i].v,edges[i].w);
  111. G[].AddEdge(edges[i].v,edges[i].u,edges[i].w);
  112. cnt++;
  113. if(cnt==n-) break;
  114. }
  115. }
  116. sz=sqrt(n);
  117. for(int i=;i<=n;i++)
  118. {
  119. top[i]=i;
  120. siz[i]=;
  121. }
  122. makeblock();
  123. for(int i=;i<=n;i++) if(top[i]==i) dfs(i,W[i]);
  124. for(int i=;i<=q;i++) {x=Ge();y=Ge();P(Query_max(x,y));}
  125. return ;
  126. }

【kruscal】【最小生成树】【块状树】bzoj3732 Network的更多相关文章

  1. CF891C Envy 最小生成树/虚树

    正解:最小生成树/虚树 解题报告: 传送门! sd如我就只想到了最暴力的想法,一点儿优化都麻油想到,,,真的菜到爆炸了QAQ 然后就分别港下两个正解QAQ 法一,最小生成树 这个主要是要想到关于最小生 ...

  2. bzoj 3720: Gty的妹子树 块状树

    3720: Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 153[Submit][Status] Descr ...

  3. 【块状树】bzoj3731 Gty的超级妹子树

    带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...

  4. 【块状树】bzoj3720 Gty的妹子树

    块状树.教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/将树按一定大小分块,分成许多子树,在每个子树的根节点记 ...

  5. 【块状树】【树链剖分】bzoj1036 [ZJOI2008]树的统计Count

    很早之前用树链剖分写过,但是代码太长太难写,省选现场就写错了. #include<cstdio> #include<algorithm> #include<cstring ...

  6. 【块状树】【博弈论】bzoj3729 Gty的游戏

    块状树,每个块的根记录一下当前块内距块根为奇数距离的异或和和偶数距离的异或和,询问的时候讨论一下即可. 总的节点数可能超过50000. #include<cstdio> #include& ...

  7. 【块状树】【树链剖分】【线段树】bzoj3531 [Sdoi2014]旅行

    离线后以宗教为第一关键字,操作时间为第二关键字排序. <法一>块状树,线下ac,线上tle…… #include<cstdio> #include<cmath> # ...

  8. 【最近公共祖先】【块状树】CODEVS 1036 商务旅行

    在线块状树LCA模板. #include<cstdio> #include<vector> #include<algorithm> #include<cmat ...

  9. bzoj3732: Network(最小生成树+LCA)

    3732: Network 题目:传送门 题解: 第一眼就看到最大边最小,直接一波最小生成树. 一开始还担心会错,问了一波肉大佬,任意两点在最小生成树上的路径最大边一定是最小的. 那么事情就变得简单起 ...

随机推荐

  1. POJ2516:Minimum Cost(最小费用最大流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19088   Accepted: 6740 题目链 ...

  2. Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider

    Backgroup: I have an AWS Managed Active Directory(domain.com). I created a DHCP options set  to my d ...

  3. maven 压缩、合并 js, css

    转载自:http://blog.csdn.net/fangxing80/article/details/17639607 我们知道在 Web 应用开发中为了提高客户端响应速度,需要将页面使用的资源最小 ...

  4. php模式-数据映射模式

    概念:简言之,数据映射模式就是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 深入理解:数据映射,是在持久化数据存储层(一般是关系型数据库)和驻于内存的数据表现层之间进行双向数据 ...

  5. Switf与OC混合开发流程

    看着身边越来越多的小伙伴转入Swift,本人也跟随潮流,转战Swift了~下面是初步写入的一个Swift项目框架. 1.创建项目,这个应该不用说了,语言swift 2.CocoaPods 导入第三方 ...

  6. 【洛谷 P4289】[HAOI2008]移动玩具(搜索)

    其实这题可以不用状压.. 提供一种新思路. 我们在读入目标棋盘的时候,把当前位置的数和当前棋盘进行比较,如果不一样,如果当前是\(1\),目标是\(0\),那么我们就把当前位置加入\(needmove ...

  7. CF 200 div.1 A

    2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...

  8. [FZU2261]浪里个浪

    TonyY是一个喜欢到处浪的男人,他的梦想是带着兰兰姐姐浪遍天朝的各个角落,不过在此之前,他需要做好规划. 现在他的手上有一份天朝地图,上面有n个城市,m条交通路径,每条交通路径都是单行道.他已经预先 ...

  9. 转:Android 调试桥(adb)是多种用途的工具

    转自:http://my.oschina.net/xuwa/blog/1574 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. 可以通过下列几种方法加入 ...

  10. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...