# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
438265 | bobbilyking | 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 "friend.h"
#include<bits/stdc++.h>
using namespace std;
#define N 100010
typedef long long int ll;
ll cost[N], bip[N];
vector<ll> adj[N];
void dfs(int i, int p, int t){
if (bip[i]!=-1) return;
bip[i] = t;
for (int x: adj[i]){
if (x==p) continue;
dfs(x,i,(t+1)&1);
}
}
int findSample(int n, int c[], int h[], int p[]){
int sum = 0;
ll mx = 0;
int prot[3];
for (int i= 0 ; i < 3; i++) prot[i] = 0;
for (int i = 0 ; i< n; i++) {
cost[i] = c[i];
sum += c[i];
mx = max(mx,cost[i]);
}
for (int i =1; i < n; i++){
prot[p[i]]++;
}
int ans = 0;
if (prot[0]==n-1){
ans = sum;
}
else if (prot[1]==n-1){
ans = mx;
}
else if (prot[2] == n-1){
for (int i = 1; i < n; i++){
adj[i].push_back(h[i]);
adj[h[i]].push_back(i);
}
for (int i = 0 ; i < n; i+=1) bip[i] = -1;
dfs(0,0,0);
int s2 = 0;
for (int i = 0; i < n; i+=1) if (bip[i] ==1)s2+=cost[i];
ans = max(s2,sum-s2);
}
return ans;
}
void readInput(){
int n; cin>>n;
int c[n], h[n], p[n];
for (int i = 0; i < n; i++) cin>>c[i];
for (int i =0 ; i < n; i++) cin>>h[i];
for (int i =0; i < n; i++) cin>>p[i];
cout<<findSample(n,c,h,p)<<'\n';
}
int main(){
readInput();
}