POJ1655 Balancing Act (树的重心)】的更多相关文章

题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i) #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int INF = 1 << 30; const int N = 100000 + 10; int H[N…
题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4993 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of on…
树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可以通过树形DP来求解树的重心. 设\(Max_i\)代表删去i节点后树中剩下子树中节点最多的一个子树的节点数.由于删去节点i至少将原树分为两部分,所以满足\(\ \frac{1}{2}n \leq Max_i\),我们要求的就是一个\(i\),使得\(Max_i\)最小. 对于Max数组,我们可以列…
Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the…
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a f…
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 222222 struct Edge{ int u,v,next; }edge[MAXN<<]; int NE,head[MAXN]; void addEdge(int u,int…
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting…
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 const int INF=0x7f7f7f7f; 6 const int N=20005; 7 int head[N],to[N*2],nxt[N*2],f[N],size[N]; 8 int tot,n,T,…
Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m…
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 最大结点dp[i]=max(max(s[j]),n-s[i])  其中的s[i]为以i为根节点的子树的结点总数,j为i的孩子. //思路和代码都是比较好理解的 代码: #include<iostream> #include<cstdio> #include<algorithm&g…