当前位置:网站首页>[code source] daily question tree
[code source] daily question tree
2022-07-25 09:37:00 【self_ disc】
2022.05.07
Topic link : Trees - subject - Daimayuan Online Judge
Title Description :
There is a tree n The number of nodes is in 11 For rooted trees . You can now operate on this tree several times , Each operation can select a point in the tree and delete all edges between the point and its son .
Now I want to know for every k∈[1,n], At least how many operations are needed to make the graph happen to exist k A link block .
Input format
Enter a positive integer in the first line n.
Second line input n−1 It's an integer fi Express i+1 Father of point , Guarantee 1≤fi≤i.
Output format
Output n It's an integer , The first i The number means k=i The answer is , If you can't make the graph just exist k A link block , The output -1.
The sample input
6
1 2 1 1 2Sample output
0 -1 1 1 -1 2Data scale
common 10 A test point .
Test point 1,2,31,2,3 Satisfy n≤20.
Test point 4,5,64,5,6 Satisfy n≤100.
For all the data , Satisfy 1≤n≤3000.
analysis :
Delete a node , The number of connected blocks increases the number of child nodes of the node . that , We regard each node as an object , The value of an item is the number of child nodes . The knapsack capacity is the number of connected blocks .
f[i] Express i The minimum number of operations required for connected blocks .
Transfer equation :f[i]=min(f[i],f[i-a[i]]+1).
See the code for details. :
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
#define ll long long
int n, a[3009], f[3009];
void solve()
{
cin >> n;
for (int i = 2; i <= n; i++)
{
int x;
cin >> x;
a[x]++; // Count the number of child nodes
}
memset(f, 127, sizeof(f)); // The initial setting is infinite
f[1] = 0; // Initial is 1 A connecting block , Without the operating
for (int i = 1; i <= n; i++)
{
if (a[i]) // Has child nodes
{
for (int j = n; j >= a[i]; j--)
{
f[j] = min(f[j], f[j - a[i]] + 1); // 01 knapsack
}
}
}
for (int i = 1; i <= n; i++) // Output
if (f[i] < 9999999)
cout << f[i] << " \n"[i == n];
else
cout << -1 << " \n"[i == n];
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
}边栏推荐
- @3-2 CCF 2020-12-2 期末预测之最佳阈值
- How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
- 【代码源】每日一题 分割(nlogn&n解法)
- 【代码源】每日一题 三段式
- ¥1-1 SWUST oj 941: 有序顺序表的合并操作的实现
- Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
- ~5 ccf 2021-12-2 序列查询新解
- [GYCTF2020]Node Game
- How many regions can a positive odd polygon be divided into
- Redis string 结构命令
猜你喜欢
随机推荐
What are stand-alone, cluster and distributed?
[GYCTF2020]Node Game
main函数的一些操作
OC -- first acquaintance
Redis set 结构命令
laravel 调用第三方 发送邮件 (php)
SurfaceView 闪屏(黑一下问题)
【代码源】每日一题 添加括号
C language and SQL Server database technology
Redis string structure command
~4.1 剑指 Offer 05. 替换空格
STM32+HC05串口蓝牙设计简易的蓝牙音箱
Student management system (summary)
Definition of cell
~1 ccf 2022-06-2 寻宝!大冒险!
浏览器访问swagger失败,显示错误ERR_UNSAFE_PORT
文件--初识
[gplt] 2022 popular lover (Floyd)
cf #785(div2) C. Palindrome Basis
语音聊天app源码-钠斯网络源码出品









