同,只要求输出一个数值最小的点。
刚开始的答案值修改的num[]记录的,罪过罪过。。。
code:
#include<cstdio> #include<cstring> #define Max(a, b) a>b?a:b using namespace std ; const int MAX = 20001 ; int k, n, Min, anspoint, ansnum ; int vis[MAX], head[MAX], num[MAX] ; struct Edge{ int v, next ; }edge[ 2*MAX] ; void addedge( int a, int b){ edge[k].v = b ; edge[k].next = head[a] ; head[a] = k ++ ; } void dfs( int x){ int i, v, min = - 1 ; num[x] = 1 ; vis[x] = 1 ; for(i=head[x]; i; i=edge[i].next){ v = edge[i].v ; if(vis[v]) continue ; dfs(v) ; num[x] += num[v] ; min = Max(min, num[v]) ; } min = Max(min, n-num[x]) ; if(min<Min){ anspoint = x ; Min = min ; ansnum = min ; } else if(min==Min&&anspoint>x){ anspoint = x ; ansnum = min ; } } int main(){ int t, x, y ; scanf( " %d ", &t) ; while(t--){ scanf( " %d ", &n) ; k = 1 ; memset(head, 0, sizeof(head)) ; for( int i= 1; i<n; i++){ scanf( " %d%d ", &x, &y) ; addedge(x, y) ; addedge(y, x) ; } memset(vis, 0, sizeof(vis)) ; memset(num, 0, sizeof(num)) ; Min = MAX ; dfs( 1) ; printf( " %d %d\n ", anspoint, ansnum) ; } return 0 ; }