제출 #1268914

#제출 시각아이디문제언어결과실행 시간메모리
1268914Born_To_LaughMagic Tree (CEOI19_magictree)C++17
25 / 100
2131 ms804540 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 1e9 + 7;
const int maxn = 1e5 + 10;
int n, m, k;
vector<int> adj[maxn];
int d[maxn], w[maxn];
namespace subw1{
    bool check(){
        for(int i=1; i<=n; ++i){
            if(w[i] != 0 && w[i] != 1)return 0;
        }
        return 1;
    }
    multiset<int> dp[maxn];
    void dfs(int a, int par){
        for(auto &elm: adj[a]){
            if(elm == par)continue;
            dfs(elm, a);
            if(dp[elm].size() > dp[a].size())swap(dp[a], dp[elm]);
            for(auto &sth: dp[elm]){
                dp[a].insert(sth);
            }
        }
        if(!w[a])return;
        auto it = dp[a].upper_bound(w[a]);
        if(it == dp[a].end()){
            dp[a].insert(w[a]);
        }
        else{
            dp[a].erase(it);
            dp[a].insert(w[a]);
        }
    }
    void solve(){
        dfs(1, -1);
        cout << dp[1].size() << '\n';
    }
}
namespace subnk{
    void compress(){
        vector<int> temp;
        for(int i=1; i<=n; ++i){
            temp.push_back(d[i]);
        }
        sort(alle(temp));
        temp.erase(unique(alle(temp)), temp.end());
        auto get = [&](int x) -> int {
            return upper_bound(alle(temp), x) - temp.begin();
        };
        for(int i=1; i<=n; ++i){
            d[i] = get(d[i]);
        }
        k = temp.size();
    }
    ll dp[maxn][(int)1e3 + 10];
    void dfs(int a, int par){
        for(auto &elm: adj[a]){
            if(elm == par)continue;
            dfs(elm, a);
        }
        for(int i=1; i<=k; ++i){
            if(i != d[a]){
                dp[a][i] = 0;
            }
            else dp[a][i] = w[a];
            for(auto &elm: adj[a]){
                if(elm == par)continue;
                dp[a][i] += dp[elm][i];
            }
            if(i > d[a]){
                dp[a][i] = max(dp[a][i], dp[a][d[a]]);
            }
        }
    }
    void solve(){
        compress();
        dfs(1, -1);
        cout << *max_element(dp[1], dp[1] + k + 3) << '\n';
    }
}
void solve(){
    cin >> n >> m >> k;
    for(int i=2; i<=n; ++i){
        int x;cin >> x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }
    for(int i=1; i<=m; ++i){
        int a, b, c;cin >> a >> b >> c;
        d[a] = b;
        w[a] = c;
    }
    if(subw1::check()){
        subw1::solve();
    }
    else{
        subnk::solve();
    }
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...