zoukankan      html  css  js  c++  java
  • Connecting Universities

    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.

    分析:每条边对答案的贡献是端点两处及外部大学个数较少的一个(待证);dfs回溯求解点个数;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=2e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m;
    ll ans;
    bool check[maxn],vis[maxn];
    vi a[maxn];
    int dfs(int now)
    {
        int cnt=0;
        vis[now]=true;
        if(check[now])cnt++;
        for(int x:a[now])
        {
            int son_cnt=0;
            if(!vis[x])
            {
                son_cnt+=dfs(x);
            }
            ans+=min(son_cnt,2*m-son_cnt);
            cnt+=son_cnt;
        }
        return cnt;
    }
    int main()
    {
        int i,j,k,t;
        scanf("%d%d",&n,&m);
        rep(i,1,2*m)scanf("%d",&k),check[k]=true;
        rep(i,1,n-1)
        {
            scanf("%d%d",&j,&k);
            a[j].pb(k),a[k].pb(j);
        }
        dfs(1);
        printf("%lld
    ",ans);
        //system ("pause");
        return 0;
    }
  • 相关阅读:
    关于*和&的数组操作运算对比(一维数组)
    GCC内联汇编
    输出10进制、16进制
    异常退出时的出栈
    字典
    List简单增删改查
    数组的增删改查
    Excel的简单导入导出
    文件流
    Lambda 的简单入门
  • 原文地址:https://www.cnblogs.com/dyzll/p/5700632.html
Copyright © 2011-2022 走看看