【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, ...
随机推荐
- Bootstrap历练实例:大小Well
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 抓取oracle建表语句及获取建表ddl语句
抓取oracle建表语句及获取建表ddl语句 1.抓取代码如下: 1.1.产生表的语法资料 DECLARE-- v_notPartTable VARCHAR2(1000):= '&2'; -- ...
- MySQL索引类型及优化
索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...
- 02_5if switch分支与循环语句
02_5if switch分支与循环语句 1.语句 1.1条件语句-根据不同条件,执行不同语句. if if ... else if ... else if if ... else if ... el ...
- 【dp】饥饿的牛
普通dp题 题目描述 牛在饲料槽前排好了队.饲料槽依次用1到n(1 ≤ n ≤ 2000)编号.每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料. 约翰提供b个区间的清单.一个区间是一对整数 ...
- vue + axios---封装一个http请求
在使用vue开发时,官方推荐使用axios来请求接口 // axios官方地址 https://github.com/axios/axios 但是axios并不像 vue-resource 一样拥有i ...
- React和Vue组件间数据传递demo
一.React (一)父组件向子组件传数据 简单的向下传递参数 /* Parent */ class App extends Component { render() { return ( <d ...
- destoon 列表页面增加手动选择排序方式
在mobile/include/mall.inc.php 行60 $order = $MOD['order']; 之前增加 排序方式判断 如果有order参数则$order接受参数,没有就用默认 ...
- 图解Disruptor框架(二):核心概念
图解Disruptor框架(二):核心概念 概述 上一个章节简单的介绍了了下Disruptor,这节就是要好好的理清楚Disruptor中的核心的概念.并且会给出个HelloWorld的小例子. 在正 ...
- Python 建模步骤
#%% #载入数据 .查看相关信息 import pandas as pd import numpy as np from sklearn.preprocessing import LabelEnco ...