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>
#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];
int dp0[200000], dp1[200000];
void dfs(int x, int p) {
dp0[x] = 0, dp1[x] = W[x];
for (int t : Gchildren[x]) if (t != p) {
dfs(t, x);
dp0[x] += dp1[t];
dp1[x] += dp0[t];
}
if (!Gclone[x].empty()) {
assert(Gclone[x].size() == 2);
int t1 = Gclone[x][0], t2 = Gclone[x][1];
dfs(t1, x);
dfs(t2, x);
dp0[x] += dp0[t1]+dp0[t2];
if (both[x]) dp1[x] += dp1[t1]+dp1[t2];
else dp1[x] += max(dp1[t1]+dp0[t2], dp0[t1]+dp1[t2]);
}
dp1[x] = max(dp1[x], dp0[x]);
}
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;
}
}
dfs(0, -1);
return dp1[0];
}
# | 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... |