# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
137814 | Osama_Alkhodairy | 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 "grader.h"
//~ #include "grader.cpp"
using namespace std;
const int N = 100001;
int p[N], dp[N][2];
vector <int> c, v[N];
int find(int x){
if(p[x] == x) return x;
return p[x] = find(p[x]);
}
void merge(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
p[y] = x;
}
void dfs(int node){
dp[node][1] = c[node];
for(auto &i : v[node]){
dfs(i);
dp[node][0] += max(dp[i][0], dp[i][1]);
dp[node][1] += dp[i][0];
}
}
int findSample(int n, int confidence[], int host[], int protocol[]){
for(int i = 0 ; i < n ; i++) p[i] = i;
for(int i = 1 ; i < n ; i++){
if(protocol[i] == 0){
v[find(host[i])].push_back(i);
}
else if(protocol[i] == 1){
confidence[find(host[i])] += confidence[i];
merge(host[i], i);
}
else{
confidence[find(host[i])] = max(confidence[find(host[i])], confidence[i]);
merge(host[i], i);
}
}
for(int i = 0 ; i < n ; i++){
c.push_back(confidence[i]);
}
dfs(0);
return max(dp[0][0], dp[0][1]);
}