Codeforces1238F. The Maximum Subtree(树形dp)

题目链接:传送门

思路:

题意说用线段的相交作为边,来构造树,所以不存在大于等于3个的线段两两相交,否则会构成环。因而构造出的树中,每个点最多只会与2个度大于1的节点相邻。

不妨把1设为树根,用degu表示原树中节点u的度,ans表示答案。

用fu表示:假设以u为根的子树,已经有一条边连向了一个度大于1的点时,所能构成的最大的“子树的子树”的大小,则有:

f= 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 = 0; T fg = 1; char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == ‘-‘) fg = -1;
        ch = getchar();
    }
    while (isdigit(ch)) x = x*10+ch-‘0‘, ch = getchar();
    x = fg * x;
}
template <typename T, typename... Args>
inline void read(T &x, Args &... args) { read(x), read(args...); }

int tot = 0;
int head[N], nxt[M<<1], ver[M<<1], 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 != -1; 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]-2 + 1);
    }
}
int main()
{
    int q; read(q);
    while (q--) {
        int n; read(n);
        ans = 0;
        tot = 0;
        for (int i = 1; i <= n; i++) {
            head[i] = -1;
            deg[i] = 0;
            f[i] = 0;
            vis[i] = false;
        }
        int u, v;
        for (int i = 1; i <= n-1; i++) {
            read(u, v);
            addEdge(u, v);
            addEdge(v, u);
        }
        dfs(1);
        cout << ans << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/11701465.html

时间: 2024-10-13 00:52:12

Codeforces1238F. The Maximum Subtree(树形dp)的相关文章

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

ZOJ3201(树形DP)

Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weights of each node, you need to find the maximum subtree of specified size of this tree. Tree Definition A tree is a connected graph which contains no cycles. In

hdu4118 树形dp

http://acm.hdu.edu.cn/showproblem.php?pid=4118 Problem Description Nowadays, people have many ways to save money on accommodation when they are on vacation. One of these ways is exchanging houses with other people. Here is a group of N people who wan

poj 1947 Rebuilding Roads 【树形DP】 【求至少删去树中 多少条边 使得树中节点数为P】

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10066   Accepted: 4595 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. Th

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)

P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but

HDU 1011 Starship Troopers(树形DP)

Starship Troopers Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description You, the leader of

SGU 149. Computer Network( 树形dp )

题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码).最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N) ----------------------------------------------------------

hdu1011 树形dp背包

http://acm.hdu.edu.cn/showproblem.php?pid=1011 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with