R - Tree Cutting (树形dp)

本文介绍了一个有趣的问题:如何通过移除特定节点将树状网络分割成较小的部分,确保每部分的节点数量不超过总数的一半。文章提供了一种利用树形深度优先搜索(DFS)的方法来解决这个问题,并附带了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses. 

Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ. 

Please help Bessie determine all of the barns that would be suitable to disconnect.
Input
* Line 1: A single integer, N. The barns are numbered 1..N. 

* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
* Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "NONE".
Sample Input
10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8
Sample Output
3
8
Hint
INPUT DETAILS: 

The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles. 

OUTPUT DETAILS: 

If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).

题意:

给一个树状图,有n个点。求出,去掉哪个点,使得剩下的每个连通子图中点的数量不超过n/2。如果有很多这样的点,就按升序输出。n<=10000

思路:

树形dp,dfs每个节点缺省之后,每个点拥有的节点数的个数,详细见代码

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=50010;
struct Edge    //建结构体
{
    int next;
    int to;
}edge[maxn*3];

int head[maxn];//前驱节点
int vis[maxn];//是否访问过,做标记
int dp[maxn];//记录拥有最大的节点数
int num[maxn];//统计以每个结点为根的树的结点数,记为num[].
int N, tot;

void add(int u, int v)//建树
{
    edge[++tot].next=head[u];
    edge[tot].to=v;
    head[u]=tot;
}

void dfs(int now)
{
    vis[now]=1;

    num[now]=1; //加一

    for(int i=head[now];i!=0;i=edge[i].next)
    {
        int next=edge[i].to;
        if(vis[next]) continue;
        dfs(next);
        dp[now]=max(dp[now],num[next]);//dfs
        num[now]+=num[next];  //记录节点数
    }
    dp[now]=max(dp[now],N-num[now]); //比较此节点父结点子节点所在连通图的最大

}
int main()
{
    int a, b;
    while(~scanf("%d", &N))
    {
        memset(dp, 0, sizeof(dp));
        memset(vis, 0, sizeof(vis));
        memset(num, 0, sizeof(num));
        memset(edge, 0, sizeof(edge));
        tot=0;
        for(int i=1; i<=N-1; i++)
        {
            scanf("%d%d", &a, &b);
            add(a, b);
            add(b, a);
        }
        dfs(1);
        int answer[maxn];
        int k=0;
        for(int i=1; i<=N; i++)
        {
            if(dp[i]<=N/2)
                answer[++k]=i;
        }
        for(int i=1; i<=k; i++)
        {
            printf("%d\n", answer[i]);

        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值