POJ 1655 Balancing Act【树的重心】
来源:互联网 发布:windows一键重装 编辑:程序博客网 时间:2024/05/16 01:58
这个题也很裸了……不多说。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define N 20010int s[N], f[N], n, root;vector<int> g[N];void getroot(int now, int fa) { int u; s[now] = 1; f[now] = 0; for (int i=0; i<g[now].size(); i++) if ((u=g[now][i]) != fa) { getroot(u, now); s[now] += s[u]; f[now] = max(f[now], s[u]); } f[now] = max(f[now], n-s[now]); if (f[now] < f[root]) root = now;}int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif int T, a, b; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i=1; i<=n; i++) g[i].clear(); for (int i=1; i<n; i++) { scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } f[0] = n; getroot(1, root=0); int cnt = 0; for (int i=1; i<=n; i++) if (f[i] == f[root]) { root = i; break; } printf("%d %d\n", root, f[root]); } return 0;}
- POJ 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act 【树的重心】
- POJ-1655 Balancing Act 树的重心
- 【POJ】1655 Balancing Act 树的重心
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- 【POJ 1655】Balancing Act 【树的重心】
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act(树的重心)
- POJ 1655 Balancing Act (树的重心)
- poj 1655 Balancing Act 【树的重心】
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act(求树的重心)
- poj 1655 Balancing Act(求树的重心)
- 树的重心学习小记 Poj 1655 Balancing Act
- POJ 1655 Balancing Act(求树的重心)
- 求割点详解 以poj1523为例
- CI8.8--八皇后问题
- Eclipse 在开发中使用到的快捷键很实用
- poj 3067(树状数组)
- MFC中CenterWindow()函数的模拟,对话框居中显示
- POJ 1655 Balancing Act【树的重心】
- 有多少程序员具有写技术博客的习惯
- 【iPod转换精灵V10.2 官方最新版】强大的视频转换功能
- C#泛型类之LIST<T>
- 网卡驱动想要成功入住Ubuntu中要这样操作【老牌系统】
- N2CMS 实践之路 Part.1 前期准备
- Spring的事物管理
- 汉诺塔(栈的应用)
- 二叉树层序遍历(队列)