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 ...
随机推荐
- BottomNavigationBar 底部导航控件
BottomNavigationBar 底部导航控件 属性 说明BottomNavigationBarItem 多个 item,iconSize icon大小currentIndex 默认选中第几个o ...
- 【控制分片分配】控制Elasticsearch分片和副本的分配
ES集群中索引可能由多个分片构成,并且每个分片可以拥有多个副本.通过将一个单独的索引分为多个分片,我们可以处理不能在一个单一的服务器上面运行的大型索引,简单的说就是索引的大小过大,导致效率问题.不能运 ...
- 题解——洛谷P3812【模板】线性基
学了下线性基 使用好像并不复杂 打了板子 但是要注意位运算优先级 #include <cstdio> #include <algorithm> #include <cst ...
- Codeforces 808G Anthem of Berland(KMP+基础DP)
题意 给定一个字符串 \(s\) ,一个字符串 \(t\) ,其中 \(s\) 包含小写字母和 "?" ,\(t\) 只包含小写字母,现在把 \(s\) 中的问号替换成任意的小写字 ...
- 【C#】委托中的匿名函数与lambda
将方法作为方法的参数 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使 ...
- 原生JS取代一些JQuery方法的简单实现
原生JS取代一些JQuery方法的简单实现 下面小编就为大家带来一篇原生JS取代一些JQuery方法的简单实现.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1.选 ...
- javaScript 内置对象-Array数组
Array 对象方法 方法 描述 concat() 连接两个或更多的数组,并返回结果. join() 把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. pop() 删除并返回数组的最后一 ...
- 【Mysql】外键
MYSQL数据表建立外键 MySQL创建关联表可以理解为是两个表之间有个外键关系,但这两个表必须满足三个条件 1.两个表必须是InnoDB数据引擎 2.使用在外键关系的域必须为索引型(Index) 3 ...
- 【Selenium2】【Shell】
E:\test_object>Python all_test.py >> report/log.txt 2>&1
- win10上安装keras
下载Anaconda https://www.anaconda.com/ 点击进入下载界面 选择Windows版本64位,python3.7 下载完成后 ,双击安装 等待安装完成! 安装MinGW包, ...