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 <bits/stdc++.h>
using namespace std;
// Find out best sample
const int MAXN = (int)(1e5) + 5;
vector<int> bigadj[MAXN];
int curSz = 0;
int inInner[MAXN];
int toInner[MAXN];
int takeDp[MAXN];
int notTakeDp[MAXN];
struct InnerTree {
vector<int> weight;
vector<vector<pair<int, int>>> adj;
InnerTree(int i, int ci) {
inInner[i] = 0;
toInner[i] = curSz;
weight.emplace_back();
adj.emplace_back();
weight[0] = ci;
curSz++;
}
void split(int i, int j, int ci, int cj) {
int cur = inInner[i];
weight[cur] -= ci;
adj.emplace_back();
adj.emplace_back();
weight.emplace_back();
weight.emplace_back();
adj[cur].emplace_back(adj.size()-1, -1);
weight[adj.size()-1] = ci;
inInner[i] = adj.size()-1;
adj[cur].emplace_back(adj.size()-2, -1);
weight[adj.size()-2] = cj;
inInner[j] = adj.size()-2;
}
void branch(int i, int ci) {
int cur = inInner[i];
weight[cur] -= ci;
adj.emplace_back();
weight.emplace_back();
weight[adj.size()-1] = ci;
adj[cur].emplace_back(adj.size()-1, curSz-1);
inInner[i] = adj.size()-1;
}
void add(int i, int ci, int to) {
inInner[i] = to;
weight[to] += ci;
}
int getMx(int n) {
int cur = 0;
int mcur = 0;
bool f = false;
for (auto& e : adj[n]) {
if (e.second == -1) {
if (f) {
mcur = max(mcur, getMx(e.first));
f = false;
cur += mcur;
mcur = 0;
}
else {
mcur = max(mcur, getMx(e.first));
f = true;
}
}
else {
cur += max(0, getMx(e.first) - takeDp[e.second] + notTakeDp[e.second]);
}
}
return cur + mcur + weight[n];
}
void upd(int n) {
for (int e : bigadj[n]) {
notTakeDp[n] += takeDp[e];
}
takeDp[n] = getMx(0) + notTakeDp[n];
}
};
vector<InnerTree> trees;
int findSample(int N,int confidence[],int host[],int protocol[]){
trees.emplace_back(0, confidence[0]);
for (int iter = 1; iter < N; iter++) {
int c = confidence[iter];
int h = host[iter];
int p = protocol[iter];
int ori = toInner[h];
if (p == 0) {
trees.emplace_back(iter, c);
bigadj[ori].push_back(curSz-1);
trees[ori].branch(h, confidence[h]);
}
else if (p == 1) {
toInner[iter] = ori;
trees[ori].add(iter, c, inInner[h]);
}
else if (p == 2) {
toInner[iter] = ori;
trees[ori].split(h, iter, confidence[h], c);
}
}
for (int j = trees.size()-1; j >= 0; j--) {
trees[j].upd(j);
}
return takeDp[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... |