제출 #764070

#제출 시각아이디문제언어결과실행 시간메모리
764070SanguineChameleon친구 (IOI14_friend)C++17
100 / 100
36 ms7528 KiB
#include "friend.h" #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 20; vector<pair<int, int>> ch[maxn]; int a[maxn]; int dp[maxn][2]; void dfs(int u) { for (auto e: ch[u]) { int v = e.first; dfs(v); } int sum = a[u]; for (auto e: ch[u]) { int v = e.first; int op = e.second; if (op == 1) { sum += max(dp[v][0], dp[v][1]); } else { sum += dp[v][0]; } } dp[u][1] = max(dp[u][1], sum); int sz = ch[u].size(); vector<int> suf(sz); sum = 0; for (int i = sz - 1; i >= 0; i--) { pair<int, int> e = ch[u][i]; int v = e.first; int op = e.second; if (op == 0) { sum += max(dp[v][0], dp[v][1]); } else { sum += dp[v][0]; } suf[i] = sum; } sum = 0; for (int i = 0; i < sz; i++) { pair<int, int> e = ch[u][i]; int v = e.first; int op = e.second; if (op == 1) { sum += dp[v][0]; dp[u][0] = max(dp[u][0], sum + (i < sz - 1 ? suf[i + 1] : 0)); } else { int pref = sum + (op == 0 ? max(dp[v][0], dp[v][1]) : dp[v][0]); dp[u][0] = max(dp[u][0], pref + (i < sz - 1 ? suf[i + 1] : 0)); sum += dp[v][0]; } } dp[u][0] = max(dp[u][0], sum); sum = 0; for (int i = 0; i < sz; i++) { pair<int, int> e = ch[u][i]; int v = e.first; int op = e.second; if (op == 1) { sum += max(dp[v][0], dp[v][1]); dp[u][1] = max(dp[u][1], sum + (i < sz - 1 ? suf[i + 1] : 0)); } else { int pref = sum + max(dp[v][0], dp[v][1]); dp[u][1] = max(dp[u][1], pref + (i < sz - 1 ? suf[i + 1] : 0)); sum += dp[v][0]; } } dp[u][1] = max(dp[u][1], sum); } int findSample(int n, int confidence[], int host[], int protocol[]){ for (int i = 0; i < n; i++) { a[i] = confidence[i]; } for (int v = 1; v < n; v++) { ch[host[v]].emplace_back(v, protocol[v]); } for (int i = 0; i < n; i++) { sort(ch[i].begin(), ch[i].end()); } dfs(0); return max(dp[0][0], dp[0][1]); }
#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...