树形dp&&树的重心(D - Godfather POJ - 3107)
题目链接:https://cn.vjudge.net/contest/277955#problem/D
题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小)。
具体思路:对于每一个点,我们求出以当前的点为根的根数的节点个数, 然后在求树的重心的时候,一共有两种情况,一种树去除该点后这个点的子节点中存在所求的最大值,还有一种情况是这个点往上会求出最大值,往上的最大值就是(n-dp[rt][0]).
AC代码:
#include<iostream>
#include<cmath>
#include<stack>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 2e5+;
struct node
{
int nex;
int to;
} edge[maxn];
int num,head[maxn],dp[maxn][],father[maxn];
int sto[maxn],minn,n;
void init()
{
minn=inf;
num=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int fr,int to)
{
edge[num].to=to;
edge[num].nex=head[fr];
head[fr]=num++;
}
void dfs1(int fr,int rt)
{
dp[fr][]=;
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs1(to,fr);
dp[fr][]+=dp[to][];
}
}
void dfs2(int fr,int rt)
{
dp[fr][]=n-dp[fr][];
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs2(to,fr);
dp[fr][]=max(dp[fr][],dp[to][]);
}
minn=min(minn,dp[fr][]);
}
int main()
{
init();
scanf("%d",&n);
int t1,t2;
for(int i=; i<=n; i++)
{
scanf("%d %d",&t1,&t2);
addedge(t1,t2);
addedge(t2,t1);
}
dfs1(,-);
dfs2(,-);
int flag=;
for(int i=; i<=n; i++)
{
if(dp[i][]==minn)
{
if(flag)
{
printf("%d",i);
flag=;
}
else
printf(" %d",i);
}
}
printf("\n");
return ;
}
树形dp&&树的重心(D - Godfather POJ - 3107)的更多相关文章
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- POJ3107Godfather[树形DP 树的重心]
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6121 Accepted: 2164 Descrip ...
- hdu-4118 Holiday's Accommodation(树形dp+树的重心)
题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 ...
- poj1655(dfs,树形dp,树的重心)(点分治基础)
题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- [poj3107]Godfather_树形dp_树的重心
Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...
随机推荐
- 无法安装HAXM (VT-X is not turned on)
安装HAXM的时候,VT-X is not turned on. 进入bios查看,已经启动了vt-x.上网搜索发现解决有一部分电脑是因为安装了Hyper-V,解决的方法: 管理员运行cmd,输入如下 ...
- linux c 判断文件存在,遍历文件,随机修改文件内容
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<assert.h> #in ...
- 【Django】用pycharm初学习使用Django
开发框架流程 M V C(99%的开发都是这种流程.) 1.URL控制器 2.Views 视图 3.models 库 1.首先创建一个Django 2.创建成功后里面几个模块的功能 用它来 ...
- centOS基本操作和命令(更新)
1.文字输入和图形界面切换 CTRL+ALT+(F1~F6)为切换至文字输入,分别对应六个不同输入界面,可用以不同账号:CTRL+ALT+F7为切换至图形界面 2.修改时区 date -R date ...
- BZOJ3252 攻略(贪心+dfs序+线段树)
考虑贪心,每次选价值最大的链.选完之后对于链上点dfs序暴力修改子树.因为每个点最多被选一次,复杂度非常正确. #include<iostream> #include<cstdio& ...
- python之插入排序
插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2).是稳定的排序方法.插入算法把要排序的数组分成两部 ...
- 洛谷 P1072 Hankson 的趣味题 解题报告
P1072 \(Hankson\)的趣味题 题目大意:已知有\(n\)组\(a0,a1,b0,b1\),求满足\((x,a0)=a1\),\([x,b0]=b1\)的\(x\)的个数. 数据范围:\( ...
- uoj50【UR#3】链式反应
题解: 令$a(x)$为破坏死光的$EFG$,$f(x)$为方案的$EGF$:$f(x) = x + \int \ \frac{1}{2} f^2(x) a(x) \ dt$; 注意到$f(0)= ...
- python之旅:面向对象的程序设计
一 面向对象的程序设计的由来 面向对象设计的由来见概述:http://www.cnblogs.com/moyand/p/8784210.html 二 什么是面向对象的程序设计及为什么要有它 面向过程的 ...
- 《编程快速上手》--web抓取--利用webbrowser模块的mapIT.py
1.代码如下 #! python3 # mapIT.py - Launches a map in the browser using an address from the # command lin ...