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 评 ...
随机推荐
- git,npm,bower设置代理地址
我們先假設代理伺服器的位址為: http://10.0.0.1:8080 設定 Git 使用代理伺服器 輸入兩行指令即可設定完畢: git config --global https.proxy ht ...
- centos type.h 编译错误问题
# ifndef __int8_t_defined # define __int8_t_defined __intN_t (, __QI__); __intN_t (, __HI__); __intN ...
- hihocoder#1148 : 2月29日 计算闰年的个数
计算到某年为止的闰年数,其实很简单.设要计算的年为A,则到A年为止(含A年)的闰年数为: 闰年数=INT(A/)-INT(A/)+INT(A/) 这里:INT为取整数函数 #include <c ...
- HDU - 2294: Pendant(矩阵优化DP&前缀和)
On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...
- POJ - 2187:Beauty Contest (最简单的旋转卡壳,求最远距离)
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ti ...
- 无状态服务 VS 有状态服务
无状态服务 VS 有状态服务 https://blog.csdn.net/mysee1989/article/details/51381435 对服务器程序来说,究竟是有状态服务,还是无状态服务,其判 ...
- net Core 2.1新功能Generic Host(通用主机)
net Core 2.1新功能Generic Host(通用主机) http://doc.okbase.net/CoderAyu/archive/301859.html 什么是Generic Host ...
- BZOJ1280: Emmy卖猪pigs
BZOJ1280: Emmy卖猪pigs https://lydsy.com/JudgeOnline/problem.php?id=1280 分析: 这题感觉还好,因为是有时间顺序,所以拆点做最大流即 ...
- LeetCode Beautiful Arrangement
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...
- COGS1516. 棋盘上的车
[题目描述] 在n*n(n≤20)的方格棋盘上放置n 个车,求使它们不能互相攻击的方案总数. [输入格式] 一行一个正整数n. [输出格式] 一行一个正整数,即方案总数. [样例输入] 3 [样例输出 ...