Submission #1268916

#TimeUsernameProblemLanguageResultExecution timeMemory
1268916Born_To_LaughMagic Tree (CEOI19_magictree)C++17
83 / 100
1324 ms804900 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]; int p[maxn]; namespace sub2{ bool check(){ for(int i=1; i<=n; ++i){ if(d[i] != 0 && adj[i].size() != 1)return 0; } return 1; } void solve(){ ll ans = 0; for(int i=1; i<=n; ++i){ ans += w[i]; } cout << ans << '\n'; } } 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(d[a]); if(it == dp[a].end()){ dp[a].insert(d[a]); } else{ dp[a].erase(it); dp[a].insert(d[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; p[i] = 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(sub2::check()){ sub2::solve(); } else 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...