并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道……

先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉。

维护两个并查集,一个用来缩点,另一个用来维护生成树的相关信息

直接把每一条树链合并到lca处,最后再把两个lca合并,因为最后要把两个lca合并,所以求lca拆开跳链的做法比较优秀。

链剖求lca还真的比倍增常数小

感觉get了很多。

Code:

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. const int N = 1e5 + ;
  7. const int Lg = ;
  8.  
  9. int n, m, tot = , head[N], ufs[N];
  10. int fa[N][Lg], same[N], dep[N], siz[N];
  11. ll cost[N];
  12.  
  13. struct Edge {
  14. int to, nxt;
  15. } e[N << ];
  16.  
  17. inline void add(int from, int to) {
  18. e[++tot].to = to;
  19. e[tot].nxt = head[from];
  20. head[from] = tot;
  21. }
  22.  
  23. struct Lineway {
  24. int u1, v1, u2, v2;
  25. ll cost;
  26. } a[N];
  27.  
  28. bool cmp(const Lineway &x, const Lineway &y) {
  29. return x.cost < y.cost;
  30. }
  31.  
  32. template <typename T>
  33. inline void read(T &X) {
  34. X = ;
  35. char ch = ;
  36. T op = ;
  37. for(; ch > ''|| ch < ''; ch = getchar())
  38. if(ch == '-') op = -;
  39. for(; ch >= '' && ch <= ''; ch = getchar())
  40. X = (X << ) + (X << ) + ch - ;
  41. X *= op;
  42. }
  43.  
  44. inline void swap(int &x, int &y) {
  45. int t = x;
  46. x = y;
  47. y = t;
  48. }
  49.  
  50. void dfs(int x, int fat, int depth) {
  51. fa[x][] = fat, dep[x] = depth;
  52. for(int i = ; i <= ; i++)
  53. fa[x][i] = fa[fa[x][i - ]][i - ];
  54. for(int i = head[x]; i; i = e[i].nxt) {
  55. int y = e[i].to;
  56. if(y == fat) continue;
  57. dfs(y, x, depth + );
  58. }
  59. }
  60.  
  61. inline int getLca(int x, int y) {
  62. if(dep[x] < dep[y]) swap(x, y);
  63. for(int i = ; i >= ; i--)
  64. if(dep[fa[x][i]] >= dep[y])
  65. x = fa[x][i];
  66. if(x == y) return x;
  67. for(int i = ; i >= ; i--)
  68. if(fa[x][i] != fa[y][i])
  69. x = fa[x][i], y = fa[y][i];
  70. return fa[x][];
  71. }
  72.  
  73. inline void init() {
  74. for(int i = ; i <= n; i++)
  75. same[i] = i, ufs[i] = i, siz[i] = , cost[i] = 0LL;
  76. }
  77.  
  78. int find(int x) {
  79. return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
  80. }
  81.  
  82. int findSame(int x) {
  83. return same[x] == x ? x : same[x] = findSame(same[x]);
  84. }
  85.  
  86. inline void merge(int x, int y, ll c) {
  87. int fx = find(x), fy = find(y);
  88. if(fx == fy) return;
  89. ufs[fx] = fy;
  90. siz[fy] += siz[fx];
  91. cost[fy] += cost[fx] + c;
  92. }
  93.  
  94. inline void go(int x, int y, ll c) {
  95. for(; ; ) {
  96. x = findSame(x);
  97. if(dep[x] <= dep[y]) return;
  98. merge(x, fa[x][], c);
  99. same[x] = fa[x][];
  100. }
  101. }
  102.  
  103. inline void chain(int x, int y, ll c) {
  104. int z = getLca(x, y);
  105. go(x, z, c), go(y, z, c);
  106. }
  107.  
  108. int main() {
  109. read(n), read(m);
  110. for(int x, y, i = ; i < n; i++) {
  111. read(x), read(y);
  112. add(x, y), add(y, x);
  113. }
  114. dfs(, , );
  115.  
  116. for(int i = ; i <= m; i++)
  117. read(a[i].u1), read(a[i].v1), read(a[i].u2), read(a[i].v2), read(a[i].cost);
  118. sort(a + , a + + m, cmp);
  119.  
  120. init();
  121. for(int i = ; i <= m; i++) {
  122. chain(a[i].u1, a[i].v1, a[i].cost);
  123. chain(a[i].u2, a[i].v2, a[i].cost);
  124. merge(a[i].u1, a[i].u2, a[i].cost);
  125. }
  126.  
  127. int ans = find();
  128. printf("%d %lld\n", siz[ans], cost[ans]);
  129. return ;
  130. }

WOJ 43 电话邀请的更多相关文章

  1. .net程序员工作两年总结

    (2015年9月) 最近换了工作,面试了很多家公司想总结下,以便以后回顾知道自己是怎么走过来的. 入行背景: 我是半路转行做软件开发的,2011年7月大学专科毕业,大学专业是:机械制造及其自动化:20 ...

  2. 100个直接可以拿来用的JavaScript实用功能代码片段(转载)

    把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaS ...

  3. 小议 - 来自《XX时代XX公司》的笔试编程题目

    经过几天的雾霾,魔都终于放晴了.哥投了几天的简历,希望找到一份.NET开发方面的岗位.也收到了几个面试邀请.这不应Ge老师的要求,选了个良辰吉日,带着身份证,学位证怀揣着2B青年的梦想来这个XX公司面 ...

  4. NLP interview

    2019-08-26 17:19:58 1)聊实习项目 2)代码题,二维数组中的查找某个target 3)讲一些最能体现创新能力的工作,而不是一些工程上的实现 4)讲论文可以从哪些方面做创新点,文本生 ...

  5. Windows Phone开发(43):推送通知第一集——Toast推送

    原文:Windows Phone开发(43):推送通知第一集--Toast推送 好像有好几天没更新了,抱歉抱歉,最近"光荣"地失业,先是忙于寻找新去处,唉,暂时没有下文.而后又有一 ...

  6. 【腾讯Bugly干货分享】QQ电话适配iOS10 Callkit框架

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58009392302e4725036142fc Dev Club 是一个交流移动 ...

  7. Android实战--电话拨号器

    今天跟着黑马视频建立一个android app--电话拨号器 首先新建一个android项目 activity_main_xml中的代码如下: <RelativeLayout xmlns:and ...

  8. IOS开发之—— 客服QQ(调用qq网页聊天),客服热线(拨打电话)

    @property (nonatomic,strong) UIButton *but;@property (nonatomic,strong) UIButton *but1;@property (st ...

  9. IOS中调用系统的电话、短信、邮件、浏览功能

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...

随机推荐

  1. @angular/cli项目构建--interceptor

    JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...

  2. Django上传文件的两种方式

    基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...

  3. python导入图片

    一.导入图片资源 方法1:直接从源图片中导(图片位于images文件夹内) self.label1=QLabel(self)self.label1.setPixmap(QPixmap(r"i ...

  4. Sortable

    d_(:з」∠)_ import React, {Component} from 'react'; import "./app.css"; import Sortable from ...

  5. CodeForces - 156C:Cipher (不错的DP)

    Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But t ...

  6. [独孤九剑]Oracle知识点梳理(三)导入、导出

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...

  7. 【LeetCode】008. String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  8. 洛谷【P1886】滑动窗口

    浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.luogu.org/problemnew/show/P1886 ...

  9. poj 3463 Sightseeing——次短路计数

    题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...

  10. 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable)

    本文转载自:http://uule.iteye.com/blog/2034097 解决方法: 在C:\Documents and Settings\Administrator\.m2中放入settin ...