zoukankan      html  css  js  c++  java
  • poj 2378 Tree Cutting 树形dp

    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).
     
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn = 1e5 + 10;
     6 const int INF = 0x7fffffff;
     7 int n, dp[maxn][2], head[maxn], tot, ans[maxn];
     8 struct node {
     9     int v, next;
    10 } edge[maxn];
    11 void init() {
    12     tot = 0;
    13     memset(head, -1, sizeof(head));
    14 }
    15 void add(int u, int v) {
    16     edge[tot].v = v;
    17     edge[tot].next = head[u];
    18     head[u] = tot++;
    19     edge[tot].v = u;
    20     edge[tot].next = head[v];
    21     head[v] = tot++;
    22 }
    23 int k = 0;
    24 int solve(int x, int fa) {
    25     int sum = 1, maxs = -1;
    26     for (int i = head[x] ; i != -1 ; i = edge[i].next) {
    27         int v = edge[i].v;
    28         if (v == fa) continue;
    29         int cost = solve(v, x);
    30         if (cost > maxs) maxs = cost;
    31         sum += cost;
    32     }
    33     if (n - sum > maxs) maxs = n - sum;
    34     if (maxs <= n / 2) ans[k++] = x;
    35     return sum;
    36 }
    37 int main() {
    38     while(scanf("%d", &n) != EOF) {
    39         init();
    40         for (int i = 0 ; i < n - 1 ; i++) {
    41             int u, v;
    42             scanf("%d%d", &u, &v);
    43             add(u, v);
    44         }
    45         solve(1, -1);
    46         if (k == 0) printf("NONE
    ");
    47         else {
    48             sort(ans, ans + k);
    49             for (int i = 0 ; i < k ; i++)
    50                 printf("%d
    ", ans[i]);
    51         }
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    一步步打造QQ群发消息群发器
    正确理解IEnumerable和IQueryable两接口的区别
    分享破解公众号裂变涨粉工具、吸粉方案。
    快40岁了,我还要不要继续写代码呢?
    精准营销、批量提取QQ群成员号码
    分享一个公众号h5裂变吸粉源码工具
    C#(.NET) HMAC SHA256实现
    mybatis的<if>标签,<foreach>标签,<collection>标签,<association>标签以及useGeneratedKeys用法
    springBoot解决跨域问题
    springBoot实现文件上传与下载
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9345710.html
Copyright © 2011-2022 走看看