Submission #1114159

#TimeUsernameProblemLanguageResultExecution timeMemory
1114159n3rm1nMagic Tree (CEOI19_magictree)C++17
3 / 100
31 ms18760 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const int MAXN = 2e5 + 10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, m, k; int p[MAXN]; long long v[MAXN], d[MAXN], w[MAXN]; vector < int > g[MAXN]; pair < int, int > f[MAXN]; long long leaf[MAXN], total = 0; long long maxon[MAXN]; void read() { cin >> n >> m >> k; for (int i = 1; i <= n; ++ i) leaf[i] = 1; for (int i = 2; i <= n; ++ i) { cin >> p[i]; g[p[i]].push_back(i); leaf[p[i]] = 1; } bool no = 0; for (int i = 1; i <= m; ++ i) { cin >> v[i] >> d[i] >> w[i]; total += w[i]; f[v[i]] = make_pair(d[i], w[i]); } if(!no) { cout << total << endl; exit(0); } } bool cmp(pair < int, int > p1, pair < int, int > p2) { return (p1.first > p2.first); } long long sub[MAXN][2], dp[MAXN]; void dfs(int beg, int par) { sub[beg][1] = 0; sub[beg][2] = 0; if(f[beg].first == 1)sub[beg][1] += f[beg].second; else sub[beg][2] += f[beg].second; for (auto nb: g[beg]) { if(nb == par)continue; dfs(nb, beg); sub[beg][1] += sub[nb][1]; sub[beg][2] += sub[nb][2]; dp[beg] = max(dp[beg], dp[nb]); } dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]); } int main() { speed(); read(); vector < pair < int, int > > f; vector < pair < int, int > > s; long long ans = 0; for (auto x: g[1]) { dfs(x, 1); f.push_back(make_pair(sub[x][1], x)); s.push_back(make_pair(sub[x][2], x)); ans = max(ans, dp[x] + sub[x][2]); } sort(f.begin(), f.end(), cmp); sort(s.begin(), s.end(), cmp); long long case1 = f[0].first; if(s[0].second == f[0].second && s.size() > 1)case1 += s[1].first; long long case2 = s[0].first; if(f[0].second == s[0].second && f.size() > 1)case2 += f[1].first; ans = max(ans, case1); ans = max(ans, case2); cout << ans << endl; return 0; } /*** 7 2 2 1 2 2 1 4 4 2 2 1 3 2 1 4 1 1 6 1 1 5 1 10 */

Compilation message (stderr)

magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:51:15: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   51 |     sub[beg][2] = 0;
      |     ~~~~~~~~~~^
magictree.cpp:53:22: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   53 |     else sub[beg][2] += f[beg].second;
      |          ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
magictree.cpp:62:52: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   62 |     dp[beg] = max(dp[beg], sub[beg][1] - sub[beg][2]);
      |                                          ~~~~~~~~~~^
magictree.cpp:59:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   59 |         sub[beg][2] += sub[nb][2];
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~
magictree.cpp:59:33: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   59 |         sub[beg][2] += sub[nb][2];
      |                        ~~~~~~~~~^
magictree.cpp:59:21: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   59 |         sub[beg][2] += sub[nb][2];
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~
#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...