cf701E Connecting Universities
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Print the maximum possible sum of distances in the division of universities into k pairs.
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
6
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
9
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
给定树上2k个特殊点,把它分成k对,使得k对之间的距离之和最大
画个图就知道,显然尽可能取跨度最大的点是更优的。
假如树上某个节点i下特殊点数是son[i],那么(i,fa[i])这条边应当被取到min(son[i],2k-son[i])次
因为要使得跨度尽量大,肯定要让i的儿子绕远路跟i子树外面的配对
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<){printf("-");a=-a;}
if (a>=)write(a/);
putchar(a%+'');
}
int n,k,cnt;
int dep[];
int fa[];
int son[];
int head[];
bool mrk[];
bool ask[];
LL f[];
struct edge{
int to,next;
}e[];
inline void ins(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v)
{
ins(u,v);
ins(v,u);
}
inline void dfs(int x)
{
if (mrk[x])return;
if (ask[x])son[x]=;
mrk[x]=;
for (int i=head[x];i;i=e[i].next)
{
if (!mrk[e[i].to])
{
dep[e[i].to]=dep[x]+;
fa[e[i].to]=x;
dfs(e[i].to);
son[x]+=son[e[i].to];
}
}
}
inline void dfs2(int x)
{
if (mrk[x])return;mrk[x]=;
if (*son[x]<=k)f[x]=son[x];else f[x]=k-son[x];
for (int i=head[x];i;i=e[i].next)
{
if (!mrk[e[i].to])
{
dfs2(e[i].to);
f[x]+=f[e[i].to];
}
}
}
int main()
{
n=read();k=read();k*=;
for(int i=;i<=k;i++)
{
int x=read();
ask[x]=;
}
for (int i=;i<n;i++)
{
int x=read(),y=read();
insert(x,y);
}
dfs();
memset(mrk,,sizeof(mrk));
dfs2();
printf("%lld\n",f[]);
}
cf701E
最新文章
- TAP/TUN(二)
- C#遍历XML文件动态加载菜单
- nginx文件目录权限设置
- 【工具相关】iOS-Reveal的使用
- nyoj 712 探 寻 宝 藏--最小费用最大流
- POJ 2337 输出欧拉路径
- 百度的hao123.com篡改浏览器首页,解决办法
- 自己动手系列——实现一个简单的LinkedList
- select中的文字垂直居中的问题
- 将项目(代码)从GitHub上克隆(下载)到本地仓库
- Java设计模式之策略模式与状态模式
- Android签名与权限的安全问题(3)
- golang 调用windows API 中文的处理
- vuex2中使用mapGetters/mapActions报错解决方法
- 【tmos】SpringBoot项目IDEA不识别依赖,但是能够运行时什么鬼?
- Redis缓存机制一为什么要用Redis
- Android开发(二十)——Fragment中的touch事件
- window策略设置
- .net环境下的缓存技术-转载!
- 数据结构与算法之排序(1)冒泡排序 ——in dart