题意:给出一棵树上的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(贪心,树上最短路)的更多相关文章

  1. Codeforces 701E Connecting Universities 贪心

    链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...

  2. 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 ...

  3. codeforces 700B Connecting Universities 贪心dfs

    分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...

  4. 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 ...

  5. Connecting Universities

    Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...

  6. 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 ...

  7. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  8. 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 ...

  9. cf 700 B Connecting Universities

    题意:现在给以一棵$n$个结点的树,并给你$2k$个结点,现在要求你把这些节点互相配对,使得互相配对的节点之间的距离(路径上经过边的数目)之和最大.数据范围$1 \leq n \leq 200000, ...

随机推荐

  1. LINQ结合正则表达式查询文件系统

    string startFolder = @"D:\Program Files (x86)\Microsoft Visual Studio 12.0\"; IEnumerable& ...

  2. 12_1_Annotation注解

    12_1_Annotation注解 1. 什么是注解 Annotation是从JDK5.0开始引入的新技术. Annotation的作用: 不是程序本身,可以对程序作出解释.可以被其他程序(比如,编译 ...

  3. 深入理解ES6箭头函数的this以及各类this面试题总结

    ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取this的特性,俘获了大批粉丝儿 它也可能是面试中的宠儿, 我们关键要搞清楚 箭头函数和普通函数中的this 一针见血式总结: 普通函数中的 ...

  4. NOIP模拟赛 篮球比赛1

    篮球比赛1(basketball1.*) Czhou为了提高机房里各种神牛的身体素质,决定在每次训练后举行篮球比赛.为了保持比赛公平,Czhou要将神牛们分成两队.首先神牛们赛前都要排成固定的队伍:然 ...

  5. NodeJS基础-Buffer

    Buffer用于处理二进制数据流 实例类似于整数数组,大小固定 C++代码在V8堆外分配物理内存 // 创建一个长度为10,且用0填充的Buffer const buf1 = Buffer.alloc ...

  6. redis主从+哨兵模式

    主从模式配置分为手动和配置文件两种方式进行配置,我现在有192.168.238.128(CentOS1).192.168.238.131(CentOS3).192.168.238.132(CentOS ...

  7. 关于使用Java实现的简单网络爬虫Demo

    什么是网络爬虫? 网络爬虫又叫蜘蛛,网络蜘蛛是通过网页的链接地址来寻找网页,从网站某一个页面(通常是首页)开始,读取网页的内容,找到在网页中的其它链接地址,然后通过这些链接地址寻找下一个网页,这样一直 ...

  8. C++中重载、覆盖和隐藏的区别,以及适用场景

    一.重载.覆盖和隐藏的区别 二.适用场景 1.重载: 适用于不同的数据类型都需要使用到的功能函数.以数据相加的函数为例,可以在同一个文件内提供以下的重载函数以支持同样的功能: int add(int, ...

  9. mysql中的存储引擎

    MySQL中常用的几种存储引擎:innoDB.bdb.myisam.memory以及这几个引擎的讲解: InnoDB存储引擎: (1) innodb存储引擎该mysql表提供了事务,回滚以及系统崩溃修 ...

  10. LOFTER 迁移

    title: LOFTER 迁移 date: 2018-09-01 16:41:02 updated: tags: [其他] description: keywords: comments: imag ...