This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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).first;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |