1. 程式人生 > >Codeforces Round #364 (Div. 2) E DFS

Codeforces Round #364 (Div. 2) E DFS

連結:戳這裡

E. Connecting Universities time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard 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 6 which will be the maximum sum in this example.


題意:

給出一棵樹,然後給出2*k對節點,這2*k個節點上都存在一個價值。

要求將2*k個價值分成k對,使得所有pair之間距離之和最大。輸出最大值!

思路:

root為1的樹,算k對點對距離和最大,樹上不存在最短路一說,兩點之間的距離是定死的。

那麼怎麼組呢?不需要考慮這個,樹上的每條邊肯定是一些點對的必經之路。

我們只需要列舉一條邊的上下兩個節點,上部分有多少價值點,下部分有多少價值點,取min。

算作每條邊的貢獻

程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
/// 算每條邊的貢獻
struct edge{
    int v,next;
}e[400100];
int n,k;
int head[200100],tot=0,vis[200100];
int sz[200100];
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
void DFS(int u,int fa){
    sz[u]=vis[u];
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        DFS(v,u);
        sz[u]+=sz[v];
    }
}
ll ans=0;
void DFS2(int u,int fa){
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        DFS2(v,u);
        ans+=min(sz[v],2*k-sz[v]);
    }
}
int main(){
    mst(head,-1);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=2*k;i++){
        int x;
        scanf("%d",&x);
        vis[x]=1;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    DFS(1,0);
    ///for(int i=1;i<=n;i++) cout<<sz[i]<<" ";cout<<endl;
    DFS2(1,0);
    printf("%I64d\n",ans);
    return 0;
}