POJ-2378 Tree Cutting

题目大意:一棵n个节点的树,找出所有的点满足:删除它之后,产生的最大(这里节点数最多即为最大)新树中节点数不超n的一半。

题目分析:两次深搜,过程类似求重心。

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long

const int N=1005;
const int INF=1000000000;

vector<int>e[N*10];
int size[N*10];
int n,dp[N*10];

void init()
{
	for(int i=1;i<=n;++i)
		e[i].clear();
	int a,b;
	for(int i=1;i<n;++i){
		scanf("%d%d",&a,&b);
		e[a].push_back(b);
		e[b].push_back(a);
	}
}

void dfs(int u,int fa)
{
	size[u]=1;
	for(int i=0;i<e[u].size();++i){
		int v=e[u][i];
		if(v==fa) continue;
		dfs(v,u);
		size[u]+=size[v];
	}
}

void dfs1(int u,int fa)
{
	dp[u]=n-size[u];
	for(int i=0;i<e[u].size();++i){
		int v=e[u][i];
		if(v==fa) continue;
		dp[u]=max(dp[u],size[v]);
		dfs1(v,u);
	}
}

void solve()
{
	dfs(1,-1);
	dfs1(1,-1);
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(dp[i]<=n/2){
			printf("%d\n",i);
			flag=false;
		}
	}
	if(flag) printf("NONE\n");
}

int main()
{
	while(~scanf("%d",&n))
	{
		init();
		solve();
	}
	return 0;
}

  

时间: 2025-01-06 22:53:09

POJ-2378 Tree Cutting的相关文章

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(树的重心)

题目链接: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, fee

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 (树的重心,微变形)

题意: 给定一棵树,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 子树统计

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

POJ 2378 树状dp

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3849   Accepted: 2304 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 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

Poj 1741 Tree (树的分治)

题目链接: Poj 1741 Tree 这个题目Tle的好苦啊,原来一直是树的重心没找对,Tle好长时间,终于对了,好感动,先贴个代码. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 10010; 8 struct node 9 { 10 int

3391: [Usaco2004 Dec]Tree Cutting网络破坏

3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 76  Solved: 59[Submit][Status][Discuss] Description 约翰意识到贝茜建设网络花费了他巨额的经费,就把她解雇了.贝茜很愤怒,打算狠狠报 复.她打算破坏刚建成的约翰的网络.    约翰的网络是树形的,连接着N(1≤N≤10000)个牛棚.她打算切断某一个牛棚的电源,使和这个牛棚相连的

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],