http://poj.org/problem?id=2378
树状DP
1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #include <set> 5 6 #define N 10010 7 8 using namespace std; 9 10 vector<int> a[N]; 11 set<int> set1; 12 set<int>::iterator it; 13 14 int n, father[N] = {0}; 15 16 int dfs(int x) 17 { 18 int i, j, flag = 0, sum = 1, temp; 19 //printf("%d: ( ", x); 20 for(i=0; i<a[x].size(); i++) 21 { 22 j = a[x][i]; 23 if(!father[j]) 24 { 25 father[j] = x; 26 temp = dfs(j); 27 sum += temp; 28 if(temp > n/2) 29 { 30 flag = 1; 31 } 32 } 33 } 34 if(n - sum > n/2) 35 { 36 flag = 1; 37 } 38 if(flag == 0) 39 { 40 set1.insert(x); 41 } 42 //printf(" %d) ", dp[x]); 43 return sum; 44 } 45 46 int main() 47 { 48 int i, x, y; 49 scanf("%d", &n); 50 for(i=1; i<=n-1; i++) 51 { 52 scanf("%d%d", &x, &y); 53 a[x].push_back(y); 54 a[y].push_back(x); 55 } 56 father[1] = 1; 57 dfs(1); 58 for(it=set1.begin(); it!=set1.end(); it++) 59 { 60 printf("%d\n", *it); 61 } 62 return 0; 63 }