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 ll int
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define sz(x) (ll)x.size()
typedef vector <ll> vi;
typedef pair <ll,ll> ii;
typedef vector <ii> vii;
#define dbg(x) cout<<#x<<": "<<x<<endl;
#define dbg2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl;
#define dbg3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<endl;
void printVct(vi &v){
cout<<"v: ";
for (ll i =0; i<sz(v); i++){
cout<<v[i]<<" ";
}
cout<<endl;
}
vector <vi> arr, adj;
ll n;
vi con, hos, pro;
vector <vi> dp; //dp[n][2] => pos, bool (can get - cannot get)
ll dfs(ll u, ll p, bool can_get){
if (dp[u][can_get] != -1) return dp[u][can_get];
ll ans_get = con[u], ans_not_get = 0;
for (ll i =0; i<sz(adj[u]); i++){
ll c = adj[u][i];
if (c != p){
ll child_can_get_ans = dfs(c,u,1);
ll child_cannot_get_ans = dfs(c,u,0);
ans_get += child_cannot_get_ans;
ans_not_get += child_can_get_ans;
}
}
if (!can_get) ans_get = 0;
dp[u][can_get] = max(ans_get, ans_not_get);
return dp[u][can_get];
}
int findSample(int N,int cc[],int hh[],int pp[]){
n = N;
for (ll i=0; i<n; i++){
con.pb(cc[i]);
hos.pb(hh[i]);
pro.pb(pp[i]);
}
adj.assign(n,vi());
for (ll i =1; i<n; i++){
ll c = hos[i];
if (pro[i] != 0){
for (ll j = 0; j<sz(adj[c]); j++){
ll d = adj[c][j];
adj[d].pb(i);
adj[i].pb(d);
}
}
if (pro[i] != 1){
adj[c].pb(i);
adj[i].pb(c);
}
}
arr.assign(n, vi(n,0));
for (ll i =0; i<n; i++){
for (ll j =0; j<sz(adj[i]); j++){
arr[i][adj[i][j]] = 1;
}
}
dp.assign(n, vi(2,-1));
ll ans = dfs(0,-1,1);
return ans;
}
/*
3
1 1 1
0 0 0 1
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
*/
# | 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... |