hihocoder-1453-Rikka with Tree

#Hihocoder 1453 : Rikka with Tree

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

source: https://hihocoder.com/problemset/problem/1453

描述

众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

勇太有一棵 n 个节点的以1为根的有根树。现在他可以对这棵树进行若干次操作,每一次操作可以选择树上的一个点然后删掉这个点和它的儿子之间的所有边。

现在勇太想要知道对于每一个 k ∈ [1, n],最少需要多少次操作才能让图中恰好存在 k 个联通块。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?

输入

第一行输入一个正整数 n (n ≤ 3000)。

第二行输入 n-1 个整数 fi 表示 i+1 号点的父亲,保证 1 ≤ fi ≤ i

输出

输出 n 个整数,第 i 个数表示 k=i 时的答案,如果无法让图中恰好存在 k 个联通块,则输出-1。

样例输入
6
1 2 1 1 2
样例输出
0 -1 1 1 -1 2

题解:

每减去一个节点,产生的联通块的数量就是该节点的孩子数量。  所以, 实质上是一道单重背包问题的变形。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXN = 3005; 

int n, root[MAXN], dp[MAXN]; 

int main(){
	freopen("in.txt", "r", stdin);

	int x, max_x, tmp;
	scanf("%d", &n);
	memset(root, 0, sizeof(root));
	max_x = 0;
	for(int i=1; i<n; ++i) {
		scanf("%d", &x);
		root[x]++;
		if(max_x < x){
			max_x = x;
		}
	}
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	for(int i=max_x; i>=1; --i){
		if(root[i] != 0){
			for(int j=n; j>=root[i]; --j){
				if(dp[j-root[i]] != 0){
					if(dp[j] != 0){
						dp[j] = min(dp[j], dp[j-root[i]] + 1);
					}  else {
						dp[j] = dp[j-root[i]] + 1;
					}
				}
			}
		}
	}
	for(int i=0; i<n; ++i){
		if(dp[i]){
			printf("%d ", dp[i]-1);
		}else{
			printf("%d ", -1 );
		}
	}
	printf("\n");

	return 0;
}

  

时间: 2024-10-25 04:12:14

hihocoder-1453-Rikka with Tree的相关文章

hihoCoder #1454 : Rikka with Tree II

Description 一个\(n\)个节点的树,先根遍历为\(1...n\).已知两个数组,一个数组表示是否是叶节点,另一个数组表示十分有右兄弟节点...'?'表示未知,求方案数\(n\leqslant 500\). Solution 区间DP. DFS序的一段表示一个子树. \(f[l][r][0/1]\)表示[l,r]变成几个子树,并不是一个...右边是否还能放的方案数... 为了方便我多写了一个函数A,他表示仅有一个子树的方案数.. Code #include <bits/stdc++.

HDU 5423:Rikka with Tree Dijkstra算法

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有

hdu 5423 Rikka with Tree 树的性质

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 165    Accepted Submission(s): 85 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,

BC #53 1002 Rikka with Tree

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math t

HUD5423 Rikka with Tree(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 162 Problem Description As we know, Rikka is p

HDU 5423 Rikka with Tree(水题)

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 149 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

hdu 5423 Rikka with Tree(dfs)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

ACM学习历程—HDU5423 Rikka with Tree(搜索)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

hihocoder 1342 Full Binary Tree Picture【完全二叉树】

转自http://www.jianshu.com/p/e37495f72cf6 hihocoder 1342 解释:题目描述了一种用ASCII码绘制的满二叉树,然后将树的根设置在一个特殊坐标轴的原点(0,0),坐标轴x向下为正向,y向右是正向.树的每个树枝与节点都占用1*1的大小.现在需要求在坐标轴中任意画一个矩形,里面会有多少个树的节点.例如样例输入中,对于(0,0)与(2,2)形成的矩形里面,包含有根节点和它的右叶子节点,所以输出的是2. 分析:1.这是是一个二叉树的问题,肯定要构造树结构,