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 求每个节点连接的联通块数量的更多相关文章

  1. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  2. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  3. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  4. 【UVA10765】Doves and bombs (BCC求割点后联通块数量)

    题目: 题意: 给了一个联通无向图,现在问去掉某个点,会让图变成几个联通块? 输出的按分出的从多到小,若相等,输出标号从小到大.输出M个. 分析: BCC求割点后联通块数量,Tarjan算法. 联通块 ...

  5. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  6. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

  7. POJ 3107 Godfather (树形dp)

    题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...

  8. POJ 1523 SPF (去掉割点能形成联通块的个数)

    思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一 ...

  9. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

随机推荐

  1. P2257 YY的GCD(莫比乌斯反演)

    第一次做莫比乌斯反演,推式子真是快乐的很啊(棒读) 前置 若函数\(F(n)\)和\(f(d)\)存在以下关系 \[ F(n)=\sum_{n|d}f(d) \] 则可以推出 \[ f(n)=\sum ...

  2. Dependency Injection2

    IoC容器和Dependency Injection 模式   使用 Service Locator 依赖注入的最大好处在于:它消除了MovieLister类对具体 MovieFinder实现类的依赖 ...

  3. jd面试之感

    一面问题:问题都回答的很好,顺利进入二面 1.单点登录的改造和原理 2.hashmap 3.jvm:堆.方法区.栈,本地方法栈,gc,gc的方式 4.spring的ioc.aop的实现方式cglib和 ...

  4. sublime text 中文显示异常

    本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...

  5. SAP-批量修改主数据(客户、供应商、物料)

    SAP-批量修改主数据(客户.供应商.物料) TCODE: MASS 对于批量修改主数据如客户,供应商等,可以试用一下Mass , 它所能修改的范围如下: 选定要修改的对象后,点击运行,会要求选择需要 ...

  6. springboot集成logback日志

    简介 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logback. 如 ...

  7. python 排序 由大到小

    import functools class Solution: # @param {integer[]} nums # @return {string} def largestNumber(self ...

  8. python (协程)生产者,消费者

    #coding=utf- import gevent from gevent.queue import Queue, Empty import time tasks = Queue(maxsize=) ...

  9. Linux简单入门

    1.目录切换命令 cd usr 切换到该目录下usr目录 cd ../ 切换到上一层目录 cd / 切换到系统根目录 cd ~ 切换到用户主目录 cd - 切换到上一个所在目录 2.目录的操作命令 1 ...

  10. oracle 存储过程给另一个用户的权限问题

    grant execute on (包名)存储过程名称 to 用户名; grant debug on  (包名)存储过程名称 to 用户名 grant select on  存储过程名称 to 用户名 ...