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;
using idata = vector<int>;
using igrid = vector<idata>;
using di = pair<int, int>;
idata parents;
idata types;
idata A, B;
idata coeff;
igrid roads;
int createVersion (int parent) {
parents.push_back(parent);
types .push_back(0);
coeff .push_back(0);
A .push_back(-1);
B .push_back(-1);
return parents.size() - 1;
}
di compute(int node);
di computeI (int node) {
int dp0 = 0;
int dp1 = coeff[node];
for (int next : roads[node]) {
const auto data = compute(next);
dp0 += max(data.first, data.second);
dp1 += data.first;
}
return { dp0, dp1 };
}
di computeM (int node) {
const auto AD = compute(A[node]);
const auto BD = compute(B[node]);
int dp0 = AD.first + BD.first;
int dp1 = max(AD.first, AD.second) + max(BD.first, BD.second);
for (int next : roads[node]) {
const auto data = compute(next);
dp0 += max(data.first, data.second);
dp1 += data.first;
}
return { dp0, dp1 };
}
di computeW (int node) {
const auto AD = compute(A[node]);
const auto BD = compute(B[node]);
int dp0 = AD.first + BD.first;
int dp1 = max(BD.first + AD.second, AD.first + BD.second);
for (int next : roads[node]) {
const auto data = compute(next);
dp0 += max(data.first, data.second);
dp1 += data.first;
}
return { dp0, dp1 };
}
di compute (int node) {
if (types[node] == 0) return computeI(node);
if (types[node] == 1) return computeM(node);
if (types[node] == 2) return computeW(node);
return {0, 0};
}
// Find out best sample
int findSample(int n,int confidence[],int host[],int protocol[]){
idata versions(n, -1);
versions[0] = createVersion(-1);
for (int i = 1; i < n; i ++) {
int p = versions[host[i]];
versions[i] = createVersion(p);
int v = versions[i];
if (protocol[i] != 0) {
types[p] = protocol[i];
versions[host[i]] = createVersion(p);
int u = versions[host[i]];
A[p] = u; B[p] = v;
parents[u] = -1;
parents[v] = -1;
}
}
roads.resize(parents.size());
for (int i = 0; i < parents.size(); i ++)
if (parents[i] != -1)
roads[parents[i]].push_back(i);
for (int i = 0; i < n; i ++)
coeff[versions[i]] = confidence[i];
const auto data = compute(0);
return max(data.first, data.second);
}
Compilation message (stderr)
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:103:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i = 0; i < parents.size(); i ++)
| ~~^~~~~~~~~~~~~~~~
# | 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... |