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

    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 6which will be the maximum sum in this example.

    这个和前几天的多校1特别像,都是枚举边,考虑边的贡献,sum += min(ver,2*k-ver).多校那题。

    2016 Multi-University Training Contest 1

    A.Abandoned country(HDU正维护,自己找原题吧)。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <map>
     6 #include <vector>
     7 using namespace std;
     8 typedef long long ll;
     9 const int maxn = 2e5+5;
    10 vector<int> g[maxn];
    11 int n,k;
    12 int un[maxn];
    13 int vis[maxn];
    14 int in[maxn];
    15 ll sum = 0;
    16 int dfs(int x)
    17 {
    18     vis[x] = 1;
    19     int num = 0;
    20     if(un[x]) num = 1;
    21     for(int i=0;i<g[x].size();i++)
    22     {
    23         int v = g[x][i];
    24         if(vis[v]) continue;
    25         vis[v] = 1;
    26         if(in[v]==1)
    27         {
    28             if(un[v])
    29             {
    30                 sum += 1;
    31                 num++;
    32             }
    33             continue;
    34         }
    35         int ver = dfs(v);
    36         sum += min(ver,2*k-ver);
    37         num += ver;
    38     }
    39     return num;
    40 }
    41 int main()
    42 {
    43     cin>>n>>k;
    44     for(int i=1;i<=2*k;i++)
    45     {
    46         int flag = 0;
    47          scanf("%d",&flag);
    48          un[flag] = 1;
    49     }
    50     for(int i=1;i<=n-1;i++)
    51     {
    52         int x,y;
    53         scanf("%d %d",&x,&y);
    54         g[x].push_back(y);
    55         g[y].push_back(x);
    56         in[x]++;
    57         in[y]++;
    58     }
    59     dfs(1);
    60     printf("%I64d
    ",sum);
    61     return 0;
    62 }

  • 相关阅读:
    rails路由
    RRD.so文件 rrdruby
    windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
    d3js把circle和rect连接在一起
    ubuntu 12.04 rails server 时候报错 execjs
    mysql2
    rails rake 版本问题
    rails下mysql出错问题mysql_api,blog/text
    ubuntu安装mysql
    vmware下ubuntu不能上网 => 恢复默认虚拟网络
  • 原文地址:https://www.cnblogs.com/littlepear/p/5816441.html
Copyright © 2011-2022 走看看