CF1153D Serval and Rooted Tree

又是一道不会的CF题。。。

好吧这道题我是真的不知道。如果有弄明白的小可爱请多多指教qwq

代码:

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;

const int maxn = 300005;
const int INF = 0x3f3f3f3f;
std::vector<int> G[maxn];
int a[maxn];
int n, ans;
int dp[maxn];

void dfs(int u) {
    if(!G[u].size()) {
        dp[u] = 1; ans++; return;
    }
    int temp = 0;
    for(auto v: G[u]) {
        dfs(v);
        temp += dp[v];
        if(a[u]) dp[u] = std::min(dp[u], dp[v]);
        else dp[u] = std::max(dp[u], temp);
    }
}
int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        if(a[i]) dp[i] = INF;
    }
    for(int i = 2; i <= n; i++) {
        int f; cin >> f;
        G[f].push_back(i);
    }
    dfs(1);
    cout << ans - dp[1] + 1 << endl;
    return 0;
}

preference

  1. https://www.luogu.org/blog/hsfzLZH1/solution-cf1153d
  2. https://codeforces.com/blog/entry/66539
posted @ 2019-06-01 14:38  Garen-Wang  阅读(114)  评论(0编辑  收藏  举报