zoukankan      html  css  js  c++  java
  • 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.

    Input

    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.

    Output

    Print the maximum possible sum of distances in the division of universities into k pairs.

    Examples
    Input
    7 2
    1 5 6 2
    1 3
    3 2
    4 5
    3 7
    4 3
    4 6
    Output
    6
    Input
    9 3
    3 2 1 6 5 9
    8 9
    3 2
    2 7
    3 4
    7 6
    4 5
    2 1
    2 8
    Output
    9
    Note

    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子树外面的配对

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<deque>
     9 #include<set>
    10 #include<map>
    11 #include<ctime>
    12 #define LL long long
    13 #define inf 0x7ffffff
    14 #define pa pair<int,int>
    15 using namespace std;
    16 inline LL read()
    17 {
    18     LL x=0,f=1;char ch=getchar();
    19     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    20     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    21     return x*f;
    22 }
    23 inline void write(LL a)
    24 {
    25     if (a<0){printf("-");a=-a;}
    26     if (a>=10)write(a/10);
    27     putchar(a%10+'0');
    28 }
    29 int n,k,cnt;
    30 int dep[200010];
    31 int fa[200010];
    32 int son[200010];
    33 int head[200010];
    34 bool mrk[200010];
    35 bool ask[200010];
    36 LL f[200010];
    37 struct edge{
    38     int to,next;
    39 }e[400010];
    40 inline void ins(int u,int v)
    41 {
    42     e[++cnt].to=v;
    43     e[cnt].next=head[u];
    44     head[u]=cnt;
    45 }
    46 inline void insert(int u,int v)
    47 {
    48     ins(u,v);
    49     ins(v,u);
    50 }
    51 inline void dfs(int x)
    52 {
    53     if (mrk[x])return;
    54     if (ask[x])son[x]=1;
    55     mrk[x]=1;
    56     for (int i=head[x];i;i=e[i].next)
    57     {
    58         if (!mrk[e[i].to])
    59         {
    60             dep[e[i].to]=dep[x]+1;
    61             fa[e[i].to]=x;
    62             dfs(e[i].to);
    63             son[x]+=son[e[i].to];
    64         }
    65     }
    66 }
    67 inline void dfs2(int x)
    68 {
    69     if (mrk[x])return;mrk[x]=1;
    70     if (2*son[x]<=k)f[x]=son[x];else f[x]=k-son[x];
    71     for (int i=head[x];i;i=e[i].next)
    72     {
    73         if (!mrk[e[i].to])
    74         {
    75             dfs2(e[i].to);
    76             f[x]+=f[e[i].to];
    77         }
    78     }
    79 }
    80 int main()
    81 {
    82     n=read();k=read();k*=2;
    83     for(int i=1;i<=k;i++)
    84     {
    85         int x=read();
    86         ask[x]=1;
    87     }
    88     for (int i=1;i<n;i++)
    89     {
    90         int x=read(),y=read();
    91         insert(x,y);
    92     }
    93     dfs(1);
    94     memset(mrk,0,sizeof(mrk));
    95     dfs2(1);
    96     printf("%lld
    ",f[1]);
    97 }
    cf701E
  • 相关阅读:
    1052: 最大报销额
    1036: 小希的数表
    1050: 找出直系亲属
    1048: 导弹防御系统
    1051: 魔咒词典
    以大数据眼光欣赏唐人文墨(一)
    Java 内部类详解
    那些“不务正业”的IT培训公司
    Brackets 前端编辑器试用
    Emmet 快速编写html代码
  • 原文地址:https://www.cnblogs.com/zhber/p/7163748.html
Copyright © 2011-2022 走看看