【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, ...
随机推荐
- maven,gradle本地缓存位置
gradle: 配置系统环境变量GRADLE_USER_HOME即可,值为缓存位置. maven: 修改settings文件:maven的home路径下的conf文件夹下的settings.xml 对 ...
- webservice基础
一.webservice概念 webservice用于异构平台之间的交互,我用Java写的程序,可以用php..net.pythod等其它语言的程序来访问我的接口.webservice有很多框架帮我们 ...
- .Net Core依赖注入中TryAddEnumerable 和TryAddTransient方法的区别
.Net Core依赖注入添加的每个服务,最终都会转换为一个ServiceDescriptor的实例,ServiceDescriptor包含以下属性: Lifetime:服务的生命周期(Singlet ...
- Linux异常体系之stubs_offset
转自 http://www.xuebuyuan.com/2208550.html 在ARM V4及V4T以后的大部分处理器中,中断向量表的位置可以有两个位置:一个是0x00000000,另一个是0xf ...
- 学习pwn的一些指导
使用ret2libc攻击方法绕过数据执行保护 http://blog.csdn.net/linyt/article/details/43643499 格式化字符串利用小结 http://www.cnb ...
- 3224: Tyvj 1728 普通平衡树(新板子)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17048 Solved: 7429[Submit][St ...
- HDU 5044 Tree LCA
题意: 给出一棵\(n(1 \leq n \leq 10^5)\)个节点的树,每条边和每个点都有一个权值,初始所有权值为0. 有两种操作: \(ADD1 \, u \, v \, k\):将路径\(u ...
- excludeFromRecents标签
Android:excludeFromRecents控制在不在recent列表中显示. true时不显示:false显示,默认. 运行如下activity后,不会显示在recent列表中. <a ...
- activity-alias
activity-alias标签,它有一个属性叫android:targetActivity,这个属性就是用来为该标签设置目标Activity的,或者说它就是这个目标Activity的别名.至此我们已 ...
- luogu1578 奶牛浴场 枚举点最大子矩阵
建议看看王知昆dalao的论文,讲得很好 #include <algorithm> #include <iostream> #include <cstring> # ...