WOJ 43 电话邀请
并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道……
先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉。
维护两个并查集,一个用来缩点,另一个用来维护生成树的相关信息
直接把每一条树链合并到lca处,最后再把两个lca合并,因为最后要把两个lca合并,所以求lca拆开跳链的做法比较优秀。
链剖求lca还真的比倍增常数小
感觉get了很多。
Code:
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- const int N = 1e5 + ;
- const int Lg = ;
- int n, m, tot = , head[N], ufs[N];
- int fa[N][Lg], same[N], dep[N], siz[N];
- ll cost[N];
- struct Edge {
- int to, nxt;
- } e[N << ];
- inline void add(int from, int to) {
- e[++tot].to = to;
- e[tot].nxt = head[from];
- head[from] = tot;
- }
- struct Lineway {
- int u1, v1, u2, v2;
- ll cost;
- } a[N];
- bool cmp(const Lineway &x, const Lineway &y) {
- return x.cost < y.cost;
- }
- template <typename T>
- inline void read(T &X) {
- X = ;
- char ch = ;
- T op = ;
- for(; ch > ''|| ch < ''; ch = getchar())
- if(ch == '-') op = -;
- for(; ch >= '' && ch <= ''; ch = getchar())
- X = (X << ) + (X << ) + ch - ;
- X *= op;
- }
- inline void swap(int &x, int &y) {
- int t = x;
- x = y;
- y = t;
- }
- void dfs(int x, int fat, int depth) {
- fa[x][] = fat, dep[x] = depth;
- for(int i = ; i <= ; i++)
- fa[x][i] = fa[fa[x][i - ]][i - ];
- for(int i = head[x]; i; i = e[i].nxt) {
- int y = e[i].to;
- if(y == fat) continue;
- dfs(y, x, depth + );
- }
- }
- inline int getLca(int x, int y) {
- if(dep[x] < dep[y]) swap(x, y);
- for(int i = ; i >= ; i--)
- if(dep[fa[x][i]] >= dep[y])
- x = fa[x][i];
- if(x == y) return x;
- for(int i = ; i >= ; i--)
- if(fa[x][i] != fa[y][i])
- x = fa[x][i], y = fa[y][i];
- return fa[x][];
- }
- inline void init() {
- for(int i = ; i <= n; i++)
- same[i] = i, ufs[i] = i, siz[i] = , cost[i] = 0LL;
- }
- int find(int x) {
- return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
- }
- int findSame(int x) {
- return same[x] == x ? x : same[x] = findSame(same[x]);
- }
- inline void merge(int x, int y, ll c) {
- int fx = find(x), fy = find(y);
- if(fx == fy) return;
- ufs[fx] = fy;
- siz[fy] += siz[fx];
- cost[fy] += cost[fx] + c;
- }
- inline void go(int x, int y, ll c) {
- for(; ; ) {
- x = findSame(x);
- if(dep[x] <= dep[y]) return;
- merge(x, fa[x][], c);
- same[x] = fa[x][];
- }
- }
- inline void chain(int x, int y, ll c) {
- int z = getLca(x, y);
- go(x, z, c), go(y, z, c);
- }
- int main() {
- read(n), read(m);
- for(int x, y, i = ; i < n; i++) {
- read(x), read(y);
- add(x, y), add(y, x);
- }
- dfs(, , );
- for(int i = ; i <= m; i++)
- read(a[i].u1), read(a[i].v1), read(a[i].u2), read(a[i].v2), read(a[i].cost);
- sort(a + , a + + m, cmp);
- init();
- for(int i = ; i <= m; i++) {
- chain(a[i].u1, a[i].v1, a[i].cost);
- chain(a[i].u2, a[i].v2, a[i].cost);
- merge(a[i].u1, a[i].u2, a[i].cost);
- }
- int ans = find();
- printf("%d %lld\n", siz[ans], cost[ans]);
- return ;
- }
WOJ 43 电话邀请的更多相关文章
- .net程序员工作两年总结
(2015年9月) 最近换了工作,面试了很多家公司想总结下,以便以后回顾知道自己是怎么走过来的. 入行背景: 我是半路转行做软件开发的,2011年7月大学专科毕业,大学专业是:机械制造及其自动化:20 ...
- 100个直接可以拿来用的JavaScript实用功能代码片段(转载)
把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaS ...
- 小议 - 来自《XX时代XX公司》的笔试编程题目
经过几天的雾霾,魔都终于放晴了.哥投了几天的简历,希望找到一份.NET开发方面的岗位.也收到了几个面试邀请.这不应Ge老师的要求,选了个良辰吉日,带着身份证,学位证怀揣着2B青年的梦想来这个XX公司面 ...
- NLP interview
2019-08-26 17:19:58 1)聊实习项目 2)代码题,二维数组中的查找某个target 3)讲一些最能体现创新能力的工作,而不是一些工程上的实现 4)讲论文可以从哪些方面做创新点,文本生 ...
- Windows Phone开发(43):推送通知第一集——Toast推送
原文:Windows Phone开发(43):推送通知第一集--Toast推送 好像有好几天没更新了,抱歉抱歉,最近"光荣"地失业,先是忙于寻找新去处,唉,暂时没有下文.而后又有一 ...
- 【腾讯Bugly干货分享】QQ电话适配iOS10 Callkit框架
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58009392302e4725036142fc Dev Club 是一个交流移动 ...
- Android实战--电话拨号器
今天跟着黑马视频建立一个android app--电话拨号器 首先新建一个android项目 activity_main_xml中的代码如下: <RelativeLayout xmlns:and ...
- IOS开发之—— 客服QQ(调用qq网页聊天),客服热线(拨打电话)
@property (nonatomic,strong) UIButton *but;@property (nonatomic,strong) UIButton *but1;@property (st ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
随机推荐
- @angular/cli项目构建--interceptor
JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...
- Django上传文件的两种方式
基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...
- python导入图片
一.导入图片资源 方法1:直接从源图片中导(图片位于images文件夹内) self.label1=QLabel(self)self.label1.setPixmap(QPixmap(r"i ...
- Sortable
d_(:з」∠)_ import React, {Component} from 'react'; import "./app.css"; import Sortable from ...
- CodeForces - 156C:Cipher (不错的DP)
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But t ...
- [独孤九剑]Oracle知识点梳理(三)导入、导出
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 洛谷【P1886】滑动窗口
浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.luogu.org/problemnew/show/P1886 ...
- poj 3463 Sightseeing——次短路计数
题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...
- 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable)
本文转载自:http://uule.iteye.com/blog/2034097 解决方法: 在C:\Documents and Settings\Administrator\.m2中放入settin ...