POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655
题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同,取编号小 的
思路:树的重心的模板题
首先要知道什么是树的重心,树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡.
所以寻找重心,即是最小化重心
的最大子树。子树大小的计算分为两部分
的下面的子树大小,
即可计算出
的上面的子树大小,则为
(n为总结点的个数,d即是
的所有子树大小,包含
)
,最后取
里面的最小值,即为该树的重心位置
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 20005;
//邻接表存图
struct node
{
int to;
int next;
};
node edges[maxn << 1];
int head[maxn];
int d[maxn];//树的深度
int dp[maxn];
int tot;
int n;
int ans;
void add_edges(int u, int v)
{
edges[++tot].to = v;
edges[tot].next = head[u];
head[u] = tot;
}
void dfs(int root, int fa)
{
int mx = 0;
for(int i = head[root]; i; i = edges[i].next)
{
int v = edges[i].to;
if(v == fa)
continue;
else
{
dfs(v, root);
d[root] += d[v];
mx = max(d[v], mx);
}
}
dp[root] = max(n - d[root], mx);
if(dp[ans] > dp[root])
{
ans = root;
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
tot = 0;
for(int i = 0; i <= n; i++)
head[i] = 0;
for(int i = 0; i <= n; i++)
d[i] = 1;
for(int i = 1; i < n; i++)
{
int a, b;
scanf("%d %d", &a, &b);
add_edges(a, b);
add_edges(b, a);
}
ans = 0;
dp[ans] = 0x3f3f3f3f;
dfs(1, 0);
cout << ans << " " << dp[ans] << endl;
}
}
POJ 1655 Balancing Act【树的重心模板题】的更多相关文章
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- POJ 1655 - Balancing Act 树型DP
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act【树的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- poj 1655 Balancing Act(找树的重心)
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...
- POJ 1655 Balancing Act (求树的重心)
求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- 070-PHP数组相加
<?php $arr1=array('a','b','c'); //定义一个数组 echo '数组$arr1的信息:<br />'; print_r($arr1); //输出数组信息 ...
- EUI库 - 9 - 数据集合 - 列表
List 和DataGroup的区别 1 选中一项 会触发 eui.ItemEvent.ITEM_TAP 事件, 2 有选中项的概念,可以设置 List 里的默认选中项 selectedIn ...
- C++编程学习(一) 概述
从一年前开通博客,陆陆续续写了一些总结类的东西,但是一直没push到网上.现在发现,分享自己的笔记也是自身学习.与他人交流的好方式.从今天开始,我会经常的push一些学习笔记到博客中,先从C++开始吧 ...
- cf 444C.
听说这是线段树的裸题??(看来我也就能搞个求和什么的了2333) #include<bits/stdc++.h> #define INF 0x7fffffff #define LL lon ...
- ubuntu安装opencv3.2
把master分支git下来: git clone git@github.com:opencv/opencv.git 查看可用的版本: git tag 选择自己想要的版本号: git reset -- ...
- Python操作APP -- Appium-Python-Client
Appium连接模拟器 pip install Appium-Python-Client 使用Appium定位或者使用辅助定位工具 SDK安装目录/tools/bin,双击此辅助定位工具 from a ...
- HZNU-ACM寒假集训Day4小结 最短路
最短路 1.Floy 复杂度O(N3) 适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...
- 配置自己的sublime
我配置的sublime的是这样的,就是在input里输入数据,然后在output里可以得到数据,这样比较方便,看到有的大神还配置背景和其他的,有时间搞一下: 首先把编译器g++配置到环境变量,可以从d ...
- python脚本下载 Google Driver 文件
使用python脚本下载 Google Driver 文件 import yaml import sys import requests import os import re import tarf ...
- POJ 1731:Orders
Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9940 Accepted: 6048 Descriptio ...