POJ-3107 Godfather 求每个节点连接的联通块数量
dp[n][2],维护儿子的联通块数量和父亲的联通块数量。
第一遍dfs求儿子,第二遍dfs求爸爸。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include <string>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include <iomanip>
#define LL long long
#define INF 20000000
#define N 50500
using namespace std;
int n, m, sum, cnt, flag;
int deg[N];
int head[N];
struct Node
{
int v, next;
}; Node edge[N << ];
LL dis[N];
LL dp[N][];//0 fat,1 son
LL mx[N];
void dfs1(int now, int pre)
{
dp[now][] = ;
for (int i = head[now]; i != -; i = edge[i].next)
{
int e = edge[i].v;
if (e == pre) continue;
dfs1(e, now);
dp[now][] += dp[e][];
}
}
void dfs2(int now, int pre)
{
if (pre == -)
{
dp[now][] = mx[now] = ;
}
else
{
dp[now][] = dp[pre][] + dp[pre][] - dp[now][];
mx[now] = dp[pre][] + dp[pre][] - dp[now][];
}
for (int i = head[now]; i != -; i = edge[i].next)
{
int e = edge[i].v;
if (e == pre) continue;
dfs2(e, now);
mx[now] = max(mx[now], dp[e][]);
}
}
void ini()
{
for (int i = ; i <= n; i++)
head[i] = -, flag = ;
cnt = , sum = n;
}
void add(int u, int v)
{
deg[v]++;
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int main()
{
//cin.sync_with_stdio(false);
while (scanf("%d",&n)!=EOF)
{
ini();
for (int i = ; i <= n; i++)
{
LL a, b;
//cin >> a >> b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
memset(dp, , sizeof(dp));
LL ans = INF;
dfs1(, -);
dfs2(, -);
for (int i = ; i <= n; i++)
ans = min(ans, mx[i]); set<int> ss;
for (int i = ; i <= n; i++)
if (mx[i] == ans) ss.insert(i);
for (set<int>::iterator i = ss.begin(); i != ss.end();)
{
//cout << *i;
printf("%d", *i);
i++;
if (i == ss.end())printf("\n");
else printf(" ");
}
}
return ;
}
POJ-3107 Godfather 求每个节点连接的联通块数量的更多相关文章
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- 【UVA10765】Doves and bombs (BCC求割点后联通块数量)
题目: 题意: 给了一个联通无向图,现在问去掉某个点,会让图变成几个联通块? 输出的按分出的从多到小,若相等,输出标号从小到大.输出M个. 分析: BCC求割点后联通块数量,Tarjan算法. 联通块 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ 1655 Balancing Act && POJ 3107 Godfather
题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...
- POJ 3107 Godfather (树形dp)
题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
- POJ 1523 SPF (去掉割点能形成联通块的个数)
思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一 ...
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
随机推荐
- 解决Tax discount configure 报出异常
If your tax calculation is based on a problematic configuration, the following warnings appear: Warn ...
- JUnit Parametrized Tests
Junit allows us to create parametrized tests. Parametrized test class has to be annotated with the @ ...
- debian下如何源码安装tmux
一.源码安装ncurses库 1.1 获取源码 wget https://invisible-island.net/datafiles/release/ncurses.tar.gz tar xvf n ...
- vba编程基础2
安装office2010的时候, 最好是 完全安装/完整安装 , 这样可以查阅 excel的 "帮助文档" 帮助文档中包含了更多的/更详细的 参考信息. 普通模块无事件, 只有 子 ...
- (转) The care and maintenance of your adviser
The care and maintenance of your adviser Ever since the advent of graduate school, students have com ...
- Face Aging with Conditional Generative Adversarial Network 论文笔记
Face Aging with Conditional Generative Adversarial Network 论文笔记 2017.02.28 Motivation: 本文是要根据最新的条件产 ...
- HDU 6061 RXD and functions(NTT)
题意 给定一个\(n\) 次的 \(f\) 函数,向右移动 \(m\) 次得到 \(g\) 函数,第 \(i\) 次移动长度是 \(a_i\) ,求 \(g\) 函数解析式的各项系数,对 ...
- Kubernetes命令
kubectl applykubectl getkubectl set image deployment/xxx -n ns echoservice=xxxkubectl deletekubectl ...
- Xilinx_ISE 14.7 Win10 闪退
打开D:\Xilinx\14.7\ISE_DS\ISE\lib\nt64 将libPortabilityNOSH.dll 重命名为libPortability.dll,替换原libPortabilit ...
- 8、nginx和tengine简介
练习: 使用nginx反向代理(rr调度)用户请求至两个以上的后端LAMP(按标准路径部署的有pma,wd),不管用户请求是什么内容都反向代理至后端服务器去,但是如果用户请求的是图片或者是html,就 ...