codeforces700B

CF700B Connecting Universities

 

Translation of the meaning of problems

Kingdom of tree is connected to a country town by the n n-1 bidirectional path between any two towns are Unicom.

In the Kingdom of Shu 2k 5,977 universities located in different towns.

Recently, President tree enactment of a bill to establish a high-speed information networks among universities. Minister of Education in his own way to understand the bill, he found a cable connecting the school is more than enough. Formally, the bill scheduled task indeed done! (Corrupt ...)

In order to get as much of the budget, the Minister intends to University paired off so that the cable needed to establish a connection among the longest school. In other words, k bigger the better for the sum of the distance between the university.

Minister help accomplish this task. Of course, each university can not be repeated in pairs inside. You can think of the length of each route are 1.

Input formats:

The first line of the input data comprises two integers n and k (2 <= n <= 200000,1 <= k <= n / 2), respectively, the number of half the number of university and town. You can think of towns are numbered from 1 to n.

The second row comprises 2k integers u1, u2, ..., u2k (1 <= ui <= n), i denotes the number of urban UNIVERSITY located.

The next row n-1 in each row comprises two integers xj, yj (1 <= xj, yj <= n), j represents the road connects two towns xj and yj. The road is about a two-way road. You can only use these roads to move.

Output formats:

K output from the sum of the largest universities.

Description:

The figure below shows the possible outcomes in a sample a. If you are connecting is located in the university town of No. 1 and No. 6 is located in the university town together, put together 2 is located in the town and the university is located in the university town of No. 5, then the sum of the distances of 6, in kind in one embodiment the sum of the maximum distance.

pic

 

min and the number of poor in the total number of university sub-tree for each side of the ball contribution, contribution to a point x to the edge is to the: sol

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
  ll s=0; bool f=0; char ch=' ';
  while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
  while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
  return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
  if(x<0) {putchar('-'); x=-x;}
  if(x<10) {putchar(x+'0'); return;}
  write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=200005,M=400005;
int n,m,sz[N];
ll ans=0;
int tot=0,Next[M],to[M],head[N];
inline void Link(int x,int y)
{
    Next[++tot]=head[x]; to[tot]=y; head[x]=tot;
}
inline void dfs(int x,int fat)
{
    int e;
    for(e=head[x];e;e=Next[e]) if(to[e]!=fat)
    {
        dfs(to[e],x); sz[x]+=sz[to[e]]; ans+=min(sz[to[e]],m-sz[to[e]]);
    }
}
int main()
{
//    freopen("codeforces700B.in","r",stdin);
    int i,x,y;
    R(n); m=read()<<1;
    for(i=1;i<=m;i++) sz[read()]=1;
    for(i=1;i<n;i++)
    {
        R(x); R(y); Link(x,y); Link(y,x);
    }
    dfs(1,0);
    Wl(ans);
  return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/gaojunonly1/p/11242096.html