Codeforces1238F. The Maximum Subtree(树形dp)
题目链接:传送门
思路:
题意说用线段的相交作为边,来构造树,所以不存在大于等于3个的线段两两相交,否则会构成环。因而构造出的树中,每个点最多只会与2个度大于1的节点相邻。
不妨把1设为树根,用degu表示原树中节点u的度,ans表示答案。
用fu表示:假设以u为根的子树,已经有一条边连向了一个度大于1的点时,所能构成的最大的“子树的子树”的大小,则有:
fu = 1,if degu=1。叶子本身就是一个点,大小为1。
fu = max{v是u的子节点 | fv} + degu-2 + 1。所有孩子中选一个fv最大的作为第2个度大于1的节点(第1个已经连出去了),这时相邻的点已经用掉2个了,剩余其他的所有相邻的点的数量为degu-2,再算上u本身。
这样处理完之后答案就很好算了。
对于一个节点u的子节点v中,fv最大的两个之和+degu-2+1,就是答案的一种可能。
(对答案的更新,代码中不是这样写的。我在更新答案的时候,利用了fu已经保存之前(fv+degu-2+1)的最大值相关信息的特点,直接用fu来更新答案了)
代码实现:O(N)
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 300005
#define M 300005
#define INF 0x3f3f3f3f
#define mk(x) (1<<x) // be conscious if mask x exceeds int
#define sz(x) ((int)x.size())
#define upperdiv(a,b) (a/b + (a%b>0))
#define mp(a,b) make_pair(a, b)
#define endl '\n'
#define lowbit(x) (x&-x) using namespace std;
typedef long long ll;
typedef double db; /** fast read **/
template <typename T>
inline void read(T &x) {
x = ; T fg = ; char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') fg = -;
ch = getchar();
}
while (isdigit(ch)) x = x*+ch-'', ch = getchar();
x = fg * x;
}
template <typename T, typename... Args>
inline void read(T &x, Args &... args) { read(x), read(args...); } int tot = ;
int head[N], nxt[M<<], ver[M<<], deg[N];
void addEdge(int u, int v) {
nxt[++tot] = head[u], ver[tot] = v, head[u] = tot;
deg[u]++;
} int f[N];
bool vis[N];
int ans;
void dfs(int u) {
vis[u] = true;
f[u] = deg[u];
for (int i = head[u]; i != -; i = nxt[i]) {
int v = ver[i];
if (vis[v])
continue;
dfs(v);
ans = max(ans, f[u] + f[v]);
f[u] = max(f[u], f[v] + deg[u]- + );
}
}
int main()
{
int q; read(q);
while (q--) {
int n; read(n);
ans = ;
tot = ;
for (int i = ; i <= n; i++) {
head[i] = -;
deg[i] = ;
f[i] = ;
vis[i] = false;
}
int u, v;
for (int i = ; i <= n-; i++) {
read(u, v);
addEdge(u, v);
addEdge(v, u);
}
dfs();
cout << ans << endl;
}
return ;
}
Codeforces1238F. The Maximum Subtree(树形dp)的更多相关文章
- CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。
题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...
- Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP
题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...
- [CF 1238F] The Maximum Subtree 树DP
题意 给定一颗树,求这个树的最大子树,且这个子树是一个good-tree. good-tree的定义是:每个节点可以表示成一个数值区间,而树上的边表示两个点表示的数值区间相交. 题解 通过分析可以发现 ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- F. Maximum Weight Subset(贪心or树形dp解法)
题:https://codeforces.com/contest/1249/problem/F 题意:给一颗树,边权为1,节点有点权,问取到一个点集,俩俩之间路径超过k,是点权和最大 思路:贪心地取点 ...
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- [USACO2002][poj1947]Rebuilding Roads(树形dp)
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...
- 树形DP 2013多校8(Terrorist’s destroy HDU4679)
题意: There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But no ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
随机推荐
- easyhook报错The given 64-Bit library does not exist
在调用 RemoteHooking.Inject 时,报错 查看easyhook源代码,出错位置如下 if(!RtlFileExists(UserLibrary)) { #ifdef _M_X64 T ...
- Reactjs之静态路由、动态路由以及Get传值以及获取
1.新增知识点 /* react路由的配置: 1.找到官方文档 https://reacttraining.com/react-router/web/example/basic 2.安装 cnpm i ...
- redis的LRU策略理解
首先看下serverCron中,服务器每次循环执行的时候,都会刷新server.lrulock. int serverCron(struct aeEventLoop *eventLoop, long ...
- 47全排列II
题目:给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入:[1,1,2]输出:[[1,1,2],[1,2,1],[2,1,1]] 来源:https://leetcode-cn.com ...
- 【汇总】Windows linux 敏感目录 路径汇总
日期:2019-08-02 10:53:52 更新:2019-08-19 15:48:01 作者:Bay0net 介绍:中间件.套件等等敏感信息,做个记录. 0x01. 基本信息 遇到文件包含.任意文 ...
- [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- CentOS 升级 openSSH+ sh脚本自动运维
升级前后对比 openSSH作为linux远程连接工具,容易受到攻击,必须更新版本来解决,低版本有如下等漏洞: OpenSSH 远程代码执行漏洞(CVE-2016-10009) OpenSSH au ...
- Python学习之进程
8.2 进程 8.2.1 进程的创建 开启多进程scoketserver:server.client 进程的开启:python中的多线程,一定是有一个主进程,由主进程创建几个子进程, Linux与Wi ...
- 《Python编程从0到1》笔记5——图解递归你肯定看完就能懂!
本小节的示例比较简单,因为在每次递归过程中原问题仅缩减为单个更小的问题.这样的问题往往能够用简单循环解决.这类递归算法的函数调用图是链状结构.这种递归类型被称为“单重递归”(single recurs ...
- C++ 优先队列 priority_queue
平时定义的时候,直接上就完事了: priority_queue<int>Q; 默认大根堆. 之前很菜的时候不知道小根堆怎么写,还在考场上干过加个负号甩到大根堆里面去的蠢事. 它的完整形式呢 ...