POJ 2378 Tree Cutting(树的重心)

题目链接:http://poj.org/problem?id=2378

题目:

Description

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 <vector>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 const int N=1e4+10;
 9 vector <int> E[N];
10 vector <int> res;
11 bool vis[N];
12 int ans[N],d[N],n;
13
14 void DFS(int u){
15     vis[u]=true;
16     d[u]=1;
17     int mx=0;
18     for(int i=0;i<E[u].size();i++){
19         int v=E[u][i];
20         if(vis[v]) continue;
21         DFS(v);
22         d[u]+=d[v];
23         mx=max(mx,d[v]);
24     }
25     mx=max(mx,n-d[u]);
26     ans[u]=mx;
27 }
28
29 int main(){
30     scanf("%d",&n);
31     for(int i=1;i<n;i++){
32         int u,v;
33         scanf("%d%d",&u,&v);
34         E[u].push_back(v);
35         E[v].push_back(u);
36     }
37     DFS(1);
38     for(int i=1;i<=n;i++){
39         if(ans[i]<=n/2) res.push_back(i);
40     }
41     int m=res.size();
42     if(m==0) printf("NONE\n");
43     else{
44         for(int i=0;i<m;i++){
45             printf("%d\n",res[i]);
46         }
47     }
48     return 0;
49 }

原文地址:https://www.cnblogs.com/Leonard-/p/8908889.html

时间: 2024-10-06 00:31:07

POJ 2378 Tree Cutting(树的重心)的相关文章

POJ 2378 Tree Cutting (树的重心,微变形)

题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因为依然是在判别最大连通块的点数. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath&g

POJ 2378 Tree Cutting (DFS)

题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include &l

poj 2378 Tree Cutting (树形dp)

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3910   Accepted: 2347 Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible

POJ 2378 Tree Cutting 子树统计

题目大意:给出一棵树,将树中的一个节点去掉之后,这棵树会分裂成一些联通块,求去掉哪些点之后,所有联通块的大小不超过所有节点的一半,并按顺序输出. 思路:基础的子树统计问题,只要深搜一遍就可以出解.这个步骤和求树的重心很像,是树分治的基础. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 10010 using nam

poj 1741 Tree(树的点分治)

poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出来.接着对于每一个子树再次运算.如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链).如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内. 具体技巧是:首先选出树的重心,将重心视为根.接着计算出每个结点的深度,以此统计答案.由于子树中可能出现重复

POJ 1655 Balancing Act 树的重心 基础题

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10347   Accepted: 4285 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

POJ 1655 Balancing Act[树的重心/树形dp]

Balancing Act 时限:1000ms Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree

POJ 1655 Balancing Act (树的重心)

题目地址:POJ 1655 树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 树的重心可以用树形DP快速的找出来. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

POJ 3723 Tree(树链剖分)

POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后,交换即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 10005; const int INF = 0x3f3f3f3f; int dep[N],