Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

大水题~~~

Posted by yinjian at 2017-08-29 15:29:49 on Problem 1655
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;


int T;
int N;

const int maxn = 20005;
std::vector<int> g[maxn];


int d[maxn];
int sz[maxn];

void dfs(int u,int fa) {
	sz[u] = 1;
    d[u] = 0;
	for(int i=0; i < (int)g[u].size(); i++) {
		int v = g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		sz[u] +=sz[v];
		d[u] = max(d[u],sz[v]);
	}
	d[u] = max(d[u],N-sz[u]);

}

int main(int argc, char const *argv[]) {

    //freopen("in.txt","r",stdin);
	scanf("%d",&T);

	while(T--) {
		scanf("%d",&N);

		for(int i = 0 ; i <= N; i++)
			g[i].clear();
		memset(d,0,sizeof(d));
		memset(sz,0,sizeof(sz));

		for(int i=1; i < N; i++) {
			int u,v;
			scanf("%d%d",&u,&v);
			g[u].push_back(v);
			g[v].push_back(u);
		}

		dfs(1,0);

		int k = N+1;
		int ans = 0;
		for(int i = 1; i <= N; i++) {
			if(d[i]<k) {
                ans = i;
                k = d[i];
			}
		}

		printf("%d %d\n", ans,k);
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator