Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大水题~~~#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator