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 |
自己想了好久,终于AC了,一次dfs,不过200多ms。#include<cstdio> using namespace std; struct node { node* next; int num; node(){next=NULL;} }head[20001]; int Max(int a , int b) { if(a<b) return b; return a; } int n , index[20001]; bool tag[20001]; int dfs(int v) { tag[v] = false; node* tem = &head[v]; int tem1 , ret = 1; bool c = false; while(tem->next) { tem = tem->next; if(tag[tem->num]) { c = true; tem1 = dfs(tem->num); if(index[v]<tem1) index[v] = tem1; ret += tem1; } } if(!c) { index[v] = n - 1; return ret; } index[v] = Max(n - ret,index[v]); return ret; } int main() { int t , i , x , y , ans , l; node* pp[20001]; scanf("%d",&t); while(t--) { scanf("%d",&n); ans = 999999; for(i = 1 ; i <= n ; i++) { head[i].next = NULL; pp[i] = &head[i]; tag[i] = true; index[i] = 0; } for(i = 1 ; i < n ; i++) { scanf("%d%d",&x,&y); pp[x]->next = new node; pp[x] = pp[x]->next; pp[x]->num = y; pp[y]->next = new node; pp[y] = pp[y]->next; pp[y]->num = x; } dfs(1); for(i = 1 ; i <= n ; i++) { if(ans>index[i]) { ans = index[i]; l = i; } } printf("%d %d\n",l,index[l]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator