标签:hat nes put deque print .com ide style poi
Input
Output
Sample Input
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
树形DP,注意考虑n-sum[u]这个搜索方向的联通点集
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 200099 #define L 31 #define INF 1000000009 #define eps 0.00000001 #define sf(a) scanf("%d",&a) /* dp[i] 记录i点除去偶的最大点集点数 sum[i] 记录所有子节点数目 */ struct edge { int to, next; }E[MAXN]; int sum[MAXN], dp[MAXN], head[MAXN]; int t, n, cnt; void addedge(int f,int t) { E[cnt].to = t; E[cnt].next = head[f]; head[f] = cnt++; } void init() { memset(sum, 0, sizeof(sum)); memset(dp, 0, sizeof(dp)); memset(head, -1, sizeof(head)); cnt = 0; } void dfs(int u, int pre) { sum[u] = dp[u] = 1; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].to; if (v == pre) continue; dfs(v, u); sum[u] += sum[v]; dp[u] = max(dp[u], sum[v]); } dp[u] = max(dp[u], n - sum[u]); } int main() { sf(t); while (t--) { init(); sf(n); for (int i = 0; i < n - 1; i++) { int a, b; sf(a), sf(b); addedge(a, b); addedge(b, a); } dfs(1, -1); int ans = INF, k = -1; for (int i = 1; i <= n; i++) { if (dp[i] < ans) { k = i, ans = dp[i]; } } printf("%d %d\n", k, ans); } }
标签:hat nes put deque print .com ide style poi
原文地址:http://www.cnblogs.com/joeylee97/p/7464819.html