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>
#define ll long long
#define pb push_back
using namespace std;
const ll N = 1e3 + 10;
vector<ll> g[N];
ll dp[N][2];
bool was[N];
ll we[N];
void dfs(ll v, ll p) {
was[v] = 1;
for(auto u : g[v]) {
if(u != p) {
dfs(u, v);
dp[v][0] += max(dp[u][0], dp[u][1]);
dp[v][1] += dp[u][0];
}
}
dp[v][1] += we[v];
}
ll col[N];
vector<int> comp;
void dfs2(ll v, ll a) {
comp.pb(v);
col[v] = a;
was[v] = 1;
for(auto u : g[v]) {
if(!was[u]) {
dfs2(u, a^3);
}
assert(col[v] != col[u]);
}
}
int findSample(int n,int confidence[],int host[],int protocol[]){
bool eq = 1;
for(int j = 0; j < n; j++)
we[j] = confidence[j];
for(int j = 1; j < n - 1; j++) {
if(protocol[j] != protocol[j + 1])
eq = 0;
}
if(eq) {
if(protocol[1] == 1) {
// empty
ll sum = 0;
for(int j = 0; j < n; j++)
sum += confidence[j];
return sum;
} else if(protocol[1] == 2) {
// full
ll mx = 0;
for(int j = 0; j < n; j++)
mx = max(mx, (ll)confidence[j]);
return mx;
} else if(protocol[1] == 0) {
// tree
for(int j = 1; j < n; j++) {
g[host[j]].pb(j);
g[j].pb(host[j]);
}
ll ans = 0;
for(int i = 0; i < n; i++) {
if(!was[i]) {
dfs(i, -1);
ans += max(dp[i][1], dp[i][0]);
}
}
return ans;
}
} else {
for(int j = 1; j < n; j++) {
if(protocol[j] == 0) {
g[j].pb(host[j]);
g[host[j]].pb(j);
} else if(protocol[j] == 1) {
for(auto u : g[host[j]]) {
g[j].pb(u);
g[u].pb(j);
}
} else if(protocol[j] == 2) {
for(auto u : g[host[j]]) {
g[j].pb(u);
g[u].pb(j);
}
g[j].pb(host[j]);
g[host[j]].pb(j);
}
}
if(n <= 10) {
ll mx = 0;
for(int j = 0; j < (1<<n); j++) {
bool incor = 0;
ll sum = 0;
for(int i = 0; i < n; i++) {
if((1<<i)&j)
sum = sum + confidence[i];
}
for(int i = 0; i < n; i++) {
if((1<<i)&j) {
for(auto u : g[i])
if((1<<u)&j)
incor = 1;
}
}
if(incor == 0)
mx = max(mx, sum);
}
return mx;
} else {
ll answ = 0;
for(int i = 0; i < n; i++)
comp.clear(), was[i] = 0, assert(we[i] == 1);
for(int i = 0; i < n; i++) {
if(!was[i]) {
comp.clear();
dfs2(i, 1);
ll kek = 0;
ll kek2 = 0;
for(auto u : comp) {
if(col[u] == 1)kek++;
else if(col[u] == 2)kek2++;
else assert(0);
}
answ += max(kek, kek2);
comp.clear();
}
}
return answ;
}
}
return 0;
}
/*
6
13 3 6 20 10 15
0 0 0 1 1 2 2 1 0 0
6
13 3 6 20 10 15
0 0 0 1 1 2 2 1 0 0
*/
/*
#define __MAXSIZE__ 100002
#include "friend.h"
using namespace std;
// Confidence
int confidence[__MAXSIZE__];
// Host
int host[__MAXSIZE__];
// Protocol
int protocol[__MAXSIZE__];
// Main
int main(void)
{
int n,i;
// Number of people
assert(scanf("%d",&n)==1);
// Confidence
for(i=0;i<n;i++)
assert(scanf("%d",&confidence[i])==1);
// Host and Protocol
for(i=1;i<n;i++)
assert(scanf("%d %d",&host[i],&protocol[i])==2);
// Answer
printf("%d\n",findSample(n,confidence,host,protocol));
return 0;
}
/**/
Compilation message (stderr)
friend.cpp:185:1: warning: "/*" within comment [-Wcomment]
185 | /**/
|| # | 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... |