POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
来源:互联网 发布:动量梯度下降算法 编辑:程序博客网 时间:2024/05/29 03:07
题目:http://poj.org/problem?id=3107
题意:给你一棵树,让你找到结点,使得剩下的联通块中的结点数量最大的尽量小
即树的重心的定义https://baike.baidu.com/item/%E6%A0%91%E7%9A%84%E9%87%8D%E5%BF%83/20416316?fr=aladdin
思路:直接dfs找其子树的max(结点数量,n-结点数量)
这个题卡vector(而且poj竟然不支持auto
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;typedef long long ll;const int N = 5e4+5;struct edge{ int to,nxt; edge(int t = 0,int n = 0):to(t),nxt(n){}}E[N*2];int n,tot,head[N*2];int dp[N],num[N],pos[N],cnt,minn;void add_edge(int u,int v){ E[tot] = edge(v,head[u]); head[u] = tot++;}void dfs(int u,int fa){ num[u] = 1; for(int i = head[u];~i;i = E[i].nxt) { int v = E[i].to; if(v == fa) continue; dfs(v,u); num[u] += num[v]; dp[u] = max(dp[u],num[v]); } dp[u] = max(dp[u],n-num[u]); if(dp[u] < minn) { minn = dp[u]; cnt = 0; pos[cnt++] = u; } else if(dp[u] == minn) pos[cnt++] = u;}int main(){ tot = 0; memset(head,-1,sizeof(head)); scanf("%d",&n); int u,v; for(int i = 1;i < n;i++) { scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); } cnt = 0; minn = 50000; dfs(1,-1); sort(pos,pos+cnt); printf("%d",pos[0]); for(int i = 1;i < cnt;i++) printf(" %d",pos[i]); printf("\n"); return 0;}
阅读全文
0 0
- POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
- Poj 3107 Godfather (树的重心)
- POJ 3107 Godfather (树的重心)
- poj 3107 Godfather 求树的重心
- poj 3107 Godfather(树的重心)
- 【POJ 3107 】Godfather 【树的重心】
- POJ.2378 Tree Cutting (树形DP 树的重心)
- poj 3107 Godfather(树形dp,树的重心)
- POJ 题目3107 Godfather(树的重心)
- POJ 3107 Godfather (求树的重心)
- poj 3107 Godfather(树的重心问题)
- Poj 2599 Godfather(树的重心)
- POJ.3107 Godfather (树形DP 树的重心)
- POJ 3107 Godfather (树的重心模板题)
- POJ 3107 Godfather 求重心
- Tree Cutting(树的重心)
- poj 3107 Godfather 求树的所有重心 树形DP基础题
- poj 2378 Tree Cutting
- HDU 3586 树形DP
- 时间序列规则法快速入门(转自dropout)
- redis操作
- spring报错:通配符的匹配很全面, 但无法找到元素 'context:annotation-config' 的声明
- oracle,创建表
- POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
- Java压力测试工具Jmeter
- 求一个n阶方阵对角线元素之和。
- spring boot 使用@ConfigurationProperties
- Linux free -m 详细说明
- python算法和数据结构笔记--python下堆栈的实现及其应用
- DCI模型架构
- ThinkPHP-通过session对发送短信做时间校验
- orcle基础语法--------(定义数据类型,循环)