【Codeforces 700B】Connecting Universities

题目传送门:http://codeforces.com/problemset/problem/700/B
中文体面:http://paste.ubuntu.com/23084330/

好题啊!做了以后心旷神怡!巧啊!妙啊!
题解的话,让我们来召唤官方题解:http://codeforces.com/blog/entry/46283

#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;

const int N = 400000+9;

int n,k,tot,head[N],to[N],nxt[N],tag[N],f[N];
LL vout;

inline void Add_Edge(int a, int b){
	static int T = 0;
	nxt[++T] = head[a]; to[T] = b; head[a] = T;
	nxt[++T] = head[b]; to[T] = a; head[b] = T;
}

inline int read(){
	char c=getchar(); int ret=0;
	while (c<'0'||c>'9') c=getchar();
	while (c<='9'&&c>='0') ret=ret*10+c-'0', c=getchar();
	return ret;
}

void DFS(int w, int fa) {
	f[w] = tot; if (tag[w]) tot++;
	for (int i=head[w];i;i=nxt[i]) 
		if (to[i] != fa) DFS(to[i], w);
	f[w] = tot - f[w];
}

int main(){
	n = read(); k = read();
	for (int i=1,lim=k*2,a,b;i<=lim;i++) tag[read()] = 1;
	for (int i=1;i<n;i++) Add_Edge(read(),read());
	DFS(1,1); for (int i=2;i<=n;i++) vout += min(f[i],2*k-f[i]);
	printf("%I64d\n",vout); 
	return 0;
} 

2 thoughts to “【Codeforces 700B】Connecting Universities”

  1. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  2. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

Leave a Reply to Patricia Vinup Cancel reply

Your email address will not be published. Required fields are marked *