# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
316912 | shivensinha4 | Friend (IOI14_friend) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#include "friend.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
const int MXN = 1e5;
vi adj[MXN+1];
int val[MXN+1], dp[MXN+1][3]; // [][0] -> exclude root; [][1] -> no restriction
void dfs(int p, int parent) {
dp[p][1] = val[p];
for (int i: adj[p]) if (i != parent) {
dfs(i, p);
dp[p][0] += dp[i][1];
dp[p][1] += dp[i][0];
}
dp[p][1] = max(dp[p][1], dp[p][0]);
}
int findSample(int n, int confidence[], int host[], int protocol[]) {
for_(i, 0, n) val[i] = confidence[i];
for_(i, 1, n) {
if (protocol[i] == 0) adj[host[i]].push_back(i);
else if (protocol[i] == 1) val[host[i]] += val[i];
else val[host[i]] = max(val[host[i]], val[i]);
}
dfs(0, 0);
return dp[0][1];
}
int main() {
#ifdef shiven
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n = 6;
int confidence[] = {13, 3, 6, 20, 10, 15};
int protocol[] = {-1, 0, 1, 2, 1, 0};
int host[] = {-1, 0, 0, 1, 2, 0};
cout << findSample(n, confidence, host, protocol) << endl;
return 0;
}