持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第29天,点击查看活动详情
本文已参与「新人创作礼」活动,一 起开启掘金创作之路
题目
Pak Chanek has blank heart-shaped cards. Card is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card () is hanging onto card (). In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation of . Then, the number written on card is .
After that, Pak Chanek must do the following operation times while maintaining a sequence (which is initially empty):
Choose a card such that no other cards are hanging onto it. Append the number written on card to the end of . If and the number on card is larger than the number on card , replace the number on card with the number on card . Remove card . After that, Pak Chanek will have a sequence with elements. What is the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally? A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. For example, is a subsequence of , and , but not and .
中文大意
给定一棵 n 个节点的树和一个空串 S,最初树中每个节点的值是一个 1 到 n 的排列,时随机的。 进行 n 次操作,每次操作可以选一个叶子节点把它加入 S 的末尾,然令它父节点的值为原来的值与被选的节点的值中小的那一个,问 n 次操作后最终序列 S 的最长不下降子序列长度最长是多少。
解法
我们发现每次对这个结点操作时如果这个结点的值小于它父节点的值时他会将它父节点的值改为它的值, 也就是说当我们按照贪心的想法来放时就可以保证在一条长链上的值是可以保证是不下降子序列,然后我们又可以发现如果是针对的子树来说 如果是以u为结尾的最长不下降子序列为的最大值为 = ,因为他们的数字是可以随机的
代码
vector<int> v[N];
int len[N];
int dp[N];
void dfs(int x) {
for(auto u : v[x]) {
dfs(u);
len[x] = max(len[x],len[u]);
dp[x] += dp[u];
}
len[x] ++;
dp[x] = max(dp[x],len[x]);
}
void solve()
{
int n; cin >> n;
rep(i,n) v[i].clear();
for(int i = 2; i <= n; i++) {
int x; cin >> x;
v[x].pb(i);
}
dfs(1);
cout << dp[1] << endl;
}