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. topcoder srm 693 div1 -3

    1.给出一个$n$个顶点的无向带权图.其中顶点$i,i+1$之间存在边,$i,i+2$之间存在边.而且仅有这些边.现在删掉其中的一些边,剩下的边满足图仍然是2联通的情况下使得权值和最小? 思路:其实就 ...

  2. Spring 学习——Spring IOC概念

    Spring IOC 接口及面向接口编程 接口 定义及理解:接口是一个类的抽象声明,用于由内部操作分离出外部沟通的方式,使其内部进行修改而不影响其外部连接沟通的一种交互方式.不对外公开逻辑处理,只是返 ...

  3. Bootstrap3基础 img-rounded 图片的四个角改成圆角

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  4. P2147 [SDOI2008]洞穴勘测

    P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...

  5. P3159 [CQOI2012]交换棋子

    思路 相当神奇的费用流拆点模型 最开始我想到把交换黑色棋子看成一个流流动的过程,流从一个节点流向另一个节点就是交换两个节点,然后把一个位置拆成两个点限制流量,然后就有了这样的建图方法 S向所有初始是黑 ...

  6. (转载)用C#实现MySQL建库及建表

    最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...

  7. Kylin——CDH

    CDH:Cloudera‘s Distribution,including Apache Hadoop. Hadoop众多分支中的一种,可直接用于成产环境 CM:Cloudera Manager

  8. windows下的安装及使用 python

    出处 https://www.cnblogs.com/daysme/ - 2017-12-30 本文只讲在 vscode 中如何运行起 python - 2017-12-30 ## windows下的 ...

  9. Python学习 day01打卡

    1.Python : 是一门解释型 弱类型 高级开发编程语言. 2.第一个Python程序的编写: print ("hell,world") 3.变量:把程序运行过程中的值储存起来 ...

  10. c# 通过反射输出成员变量以及成员变量的值

    /*** @Author rexzhao* 工具类 仅限于* public variable*/using System.Collections;using System.Collections.Ge ...