【CF700B】Connecting Universities(贪心,树上最短路)
题意:给出一棵树上的2*k个节点,给他们配对,使得他们之间的距离和最大。
思路:一条边的两侧如果有一侧没有给定的节点就不会被经过……
如果有1个节点就会被经过1次……
如果两侧分别有x,y个给定节点就会被经过min(x,y)次
因为要使总路程最大就是让每一条路被走过最多的次数
肯定是两侧各取一个 剩下的只能在某侧内部解决
所以按此统计即可
答案很大 用INT64
var head,vet,next,a,b,c,dep,flag,f:array[..]of longint;
n,k,tot,i:longint;
ans:int64; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure dfs(u,fa:longint);
var e,v:longint;
begin
flag[u]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if (v<>fa)and(flag[v]=) then
begin
dep[v]:=dep[u]+;
dfs(v,u);
f[u]:=f[u]+f[v];
end;
e:=next[e];
end;
end; begin
read(n,k);
for i:= to *k do
begin
read(c[i]);
f[c[i]]:=;
end;
for i:= to n- do
begin
read(a[i],b[i]);
add(a[i],b[i]);
add(b[i],a[i]);
end;
dfs(,-);
for i:= to n- do
if dep[a[i]]>dep[b[i]] then ans:=ans+min(f[a[i]],*k-f[a[i]])
else ans:=ans+min(f[b[i]],*k-f[b[i]]);
writeln(ans); end.
【CF700B】Connecting Universities(贪心,树上最短路)的更多相关文章
- Codeforces 701E Connecting Universities 贪心
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...
- Codeforces 700B Connecting Universities - 贪心
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...
- codeforces 700B Connecting Universities 贪心dfs
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...
- Codeforces Round #364 (Div. 2) E. Connecting Universities
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Connecting Universities
Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...
- Codeforces Round #364 (Div. 2) E. Connecting Universities (DFS)
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- codeforces 701E E. Connecting Universities(树的重心)
题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...
- cf701E Connecting Universities
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...
- cf 700 B Connecting Universities
题意:现在给以一棵$n$个结点的树,并给你$2k$个结点,现在要求你把这些节点互相配对,使得互相配对的节点之间的距离(路径上经过边的数目)之和最大.数据范围$1 \leq n \leq 200000, ...
随机推荐
- PAT (Basic Level) Practise (中文)- 1012. 数字分类 (20)
http://www.patest.cn/contests/pat-b-practise/1012 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数 ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 你对CommonJS规范了解多少?
写在前面 为什么会出现CommonJS规范? 因为JavaScript本身并没有模块的概念,不支持封闭的作用域和依赖管理,传统的文件引入方式又会污染变量,甚至文件引入的先后顺序都会影响整个项目的运行. ...
- POJ-1426-Find the multiply
这题深搜广搜都可以做,深搜的做法就是把每个由1 和 0 组成的数字拓展10倍以及拓展10倍+1,然后压入队列. 这样可以走过所有由10组成的数字,且两个方向平行发展(*10 +0和+1). bfs ...
- 【Mysql】Mysql主从库搭建过程(爬完坑后整理所得)
Mysql主从数据库搭建流程 新手开始学习mysql主从库,遇到一些问题,总结后写出以下流程 下面以5.7.23版本为例介绍 第一步:去官网下载5.7.23版本的免安装压缩包形式的mysql文件,贴上 ...
- Java多线程之Deque与LinkedBlockingDeque深入分析
有大小的队列就叫有界队列 如 ArrayBlockingquue, 反之是无界队列 如 LinkedBlockingDeque. 单词写错了. 是的,LinkedBlockingDeque 永远满不 ...
- 04 Django模板
基本概念 作为Web框架,Django提供了模板,用于编写html代码,还可以嵌入模板代码更快更方便的完成页面开发,再通过在视图中渲染模板,将生成最终的html字符串返回给客户端浏览器 模版致力于表达 ...
- 搜索引擎elasticsearch + kibana + X-pack + IK安装部署
目录 准备安装环境 配置启动 启动elasticsearch 启动kibana 启用X-pack 安装使用IK 使用示例 官方Clients 准备安装环境 这次我们安装以下软件或插件: elastic ...
- App架构经验总结
作者:李纪钢,网名 Keegan小钢,博客地址:http://keeganlee.me.目前在广州日报新媒体有限公司,负责移动产品的研发工作. 关于:本文整理自CSDN架构主题月子活动金牌架构师微课堂 ...
- BZOJ 4244: 邮戳拉力赛
转化为括号序列DP 注意边界 #include<cstdio> #include<algorithm> #define rep(i,x,y) for (int i=x; i&l ...