Submission #73859

#TimeUsernameProblemLanguageResultExecution timeMemory
73859funcsrFriend (IOI14_friend)C++17
69 / 100
137 ms17408 KiB
#include "friend.h" #include <vector> #include <cassert> #include <tuple> #define rep(i, n) for (int i=0; i<(n); i++) #define pb push_back using namespace std; typedef pair<int, int> P; vector<int> Gchildren[200000]; vector<int> Gclone[200000]; int repr[100000]; int W[200000]; bool both[200000]; P dfs(int x, int p) { int dp0 = 0, dp1 = W[x]; for (int t : Gchildren[x]) if (t != p) { int dp0t, dp1t; tie(dp0t, dp1t) = dfs(t, x); dp0 += dp1t; dp1 += dp0t; } if (!Gclone[x].empty()) { assert(Gclone[x].size() == 2); int t1 = Gclone[x][0], t2 = Gclone[x][1]; int dp0t1, dp1t1; tie(dp0t1, dp1t1) = dfs(t1, x); int dp0t2, dp1t2; tie(dp0t2, dp1t2) = dfs(t2, x); dp0 += dp0t1+dp0t2; if (both[x]) dp1 += dp1t1+dp1t2; else dp1 += max(dp1t1+dp0t2, dp0t1+dp1t2); } dp1 = max(dp1, dp0); return P(dp0, dp1); } int findSample(int N, int A[], int host[], int protocol[]) { W[0] = A[0]; repr[0] = 0; int V = 1; for (int i=1; i<N; i++) { int p = host[i]; if (protocol[i] == 0) { Gchildren[repr[p]].pb(V); W[V] = A[i]; repr[i] = V; V++; } else { // copy int root = repr[p], p_new = V++, x = V++; repr[p] = p_new; repr[i] = x; W[p_new] = W[root]; W[x] = A[i]; W[root] = 0; Gclone[root].pb(p_new); Gclone[root].pb(x); both[root] = protocol[i] == 1; } } return dfs(0, -1).second; }
#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...