POJ 3107 Godfather (树形dp)
虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久。 还有据说这个题用vector会超时,看了以后还是用邻接吧。
题意:
给一颗树,保证是一颗树,求去掉一个点以后的联通块里节点的数目的 最大值最小,求这样的点,并按照递增顺序输出。
分析:
d[father] = max(n-sum, d[son]); sum代表这个节点以下的全部节点总数, 去掉一个节点的联通块的最大的节点数 等于 整个树里的节点数减去这个节点下的总数 和 子树的数目的
最大值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define LL __int64
const int maxn = +;
const int INF = <<;
using namespace std;
int head[maxn], vis[maxn], t, d[maxn];
int mi, n;
struct node
{
int u, v, ne;
}e[*maxn]; void add(int u, int v)
{
e[t].u = u;
e[t].v = v;
e[t].ne = head[u];
head[u] = t++;
}
int dfs(int son, int fa)
{
int i, tmp, sum = , x; //sum是以son为根节点的子树的全部的节点数
for(i = head[son]; i != -; i = e[i].ne)
{
tmp = e[i].v;
if(tmp == fa) continue; //避免回去。
x = dfs(tmp, son);
sum += x;
d[son] = max(d[son], x);
}
d[son] = max(d[son], n-sum);
if(d[son]<mi)
mi = d[son];
return sum;
}
int main()
{
int i, f;
while(~scanf("%d", &n))
{
memset(e, , sizeof(e));
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(d, , sizeof(d));
t = ;
mi = INF; for(i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -); //把给的树看成以1为根节点。 f = ;
for(i = ; i <= n; i++)
{
if(d[i]==mi)
{
if(f)
printf(" %d", i);
else
printf("%d", i);
f = ;
}
}
printf("\n");
}
return ;
}
避免回去的时候也可以用vis[]来标记
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define LL __int64
const int maxn = +;
const int INF = <<;
using namespace std;
int head[maxn], vis[maxn], t, d[maxn];
int mi, n;
struct node
{
int u, v, ne;
}e[*maxn]; void add(int u, int v)
{
e[t].u = u;
e[t].v = v;
e[t].ne = head[u];
head[u] = t++;
}
int dfs(int son)
{
int i, tmp, sum = , x;
vis[son] = ;
for(i = head[son]; i != -; i = e[i].ne)
{
tmp = e[i].v;
if(vis[tmp]) continue;
x = dfs(tmp);
sum += x;
d[son] = max(d[son], x);
}
d[son] = max(d[son], n-sum);
if(d[son]<mi)
mi = d[son];
return sum;
}
int main()
{
int i, f;
while(~scanf("%d", &n))
{
memset(e, , sizeof(e));
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(d, , sizeof(d));
t = ;
mi = INF; for(i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(); f = ;
for(i = ; i <= n; i++)
{
if(d[i]==mi)
{
if(f)
printf(" %d", i);
else
printf("%d", i);
f = ;
}
}
printf("\n");
}
return ;
}
POJ 3107 Godfather (树形dp)的更多相关文章
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- 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. 链式前 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- UML部署图(转载)
概述: 部署图用于可视化的软件组件部署的系统中的物理组件的拓扑结构. 因此,部署图是用来描述一个系统的静态部署视图.部署图由节点和它们之间的关系. 目的: 部署名称本身描述的原理图的目的.部署图用于描 ...
- 动态调用WCF服务
动态调用WCF服务,只需要提供*.svc地址, 1:命名空间: using System.ServiceModel.Channels;using System.ServiceModel; 2:创建访问 ...
- 【ASP.Net MVC】在AspNet Mvc使用Ajax
目录 一.使用System.Web.Mvc.Ajax 1.1 System.Web.Mvc.Ajax.BeginForm 1.2 System.Web.Mvc.Ajax.ActionLink 二.手工 ...
- 用HAProxy和KeepAlived构建高可用的反向代理
用HAProxy和KeepAlived构建高可用的反向代理 用HAProxy和KeepAlived构建高可用的反向代理 前言对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求 ...
- 【linux】文字提取
提取IP地址: 方法①: ifconfig eth3|grep Bcast|cut -d ":" -f2|cut -d " " -f1 ifconfig: 显示 ...
- POJ 1274
#include<iostream> #include<stdio.h> #include <string.h> #include <vector> # ...
- ***Xcode Interface Builder或Storyboard中可建立那两种连接?
在Xcode Interface Builder或Storyboard中,可建立到输出口(IBOutlet)和操作(方法,IBAction)的连接. IBOutlet are for output C ...
- Linux网络编程5——使用UDP协议实现群聊
引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...
- http://blog.csdn.net/woshiyjk/article/details/7895888
http://blog.csdn.net/woshiyjk/article/details/7895888
- 【转】Linux写时拷贝技术(copy-on-write)
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html 源于网上资料 COW技术初窥: 在Linux程序中,fork()会 ...