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;
const int maxn = 1e5 + 5;
int n, a[maxn], t[maxn];
vector<int> adj[maxn][4];
int dp[maxn][2];
void dfs(int u) {
for (int i=0;i<=2;i++) for (int v:adj[u][i]) dfs(v);
int pt = 0, def = 0;
for (int v:adj[u][0]) def += dp[v][1];
for (int v:adj[u][1]) def += dp[v][0];
for (int v:adj[u][2]) def += dp[v][0];
int val = def, cur = 0;
for (int v:adj[u][0]) {
for (;pt<adj[u][3].size() && adj[u][3][pt]<v; pt++) {
int V = adj[u][3][pt];
if (t[V]==1) cur += dp[V][1] - dp[V][0];
else if (t[V]==2) cur = max(dp[V][1] - dp[V][0], cur);
}
val = max(def + cur, val);
cur += dp[v][0] - dp[v][1];
}
for (;pt<adj[u][3].size(); pt++) {
int V = adj[u][3][pt];
if (t[V]==1) cur += dp[V][1] - dp[V][0];
else if (t[V]==2) cur = max(dp[V][1] - dp[V][0], cur);
}
val = max(def + cur, val);
dp[u][0] = 0;
for (int v:adj[u][0]) dp[u][0] += dp[v][1];
for (int v:adj[u][1]) dp[u][0] += dp[v][0];
for (int v:adj[u][2]) dp[u][0] += dp[v][0];
dp[u][1] = a[u];
for (int v:adj[u][0]) dp[u][1] += dp[v][0];
for (int v:adj[u][1]) dp[u][1] += dp[v][1];
for (int v:adj[u][2]) dp[u][1] += dp[v][0];
dp[u][1] = max({val, dp[u][0], dp[u][1]});
}
int findSample(int N, int confidence[], int host[], int T[]){
n = N;
for (int i=0;i<n;i++) a[i] = confidence[i];
t[0] = T[0];
for (int i=1;i<n;i++) t[i] = T[i];
for (int i=1;i<n;i++) adj[host[i]][t[i]].push_back(i), adj[host[i]][3].push_back(i);
dfs(0);
// for (int j=0;j<=1;j++) {
// cout << j << endl;
// for (int i=0;i<n;i++) cout << dp[i][j] << " "; cout << endl;
// }
return dp[0][1];
}
Compilation message (stderr)
friend.cpp: In function 'void dfs(int)':
friend.cpp:17:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (;pt<adj[u][3].size() && adj[u][3][pt]<v; pt++) {
| ~~^~~~~~~~~~~~~~~~~
friend.cpp:25:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (;pt<adj[u][3].size(); pt++) {
| ~~^~~~~~~~~~~~~~~~~
# | 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... |