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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int NORMAL=0; //normal friendship
const int COPY1=1; //both itself and parent can be on
const int COPY2=2; //either itself or parent must be off
const int DUP=3; //copy of certain node
int n;
int arr[100005];
vector<ii> al[200005];
int val[200005];
int id[100005];
int IDX=1;
int memo[200005][2][2][2];
//for a normal friendship edge, for the guy to be toggled
//everyone connected by copy/dup edges must be toggled off
//for copy2 edges, all subtrees with copy2 edge but all be off
//if you are on, all parents traversed by copy edges shld be togglable
//states:
// b1: anyone in your copy CC toggled
// b2: anyone in copy1 CC toggled
// b3: are you toggled
//if there is someone in your copy CC toggled
//anyone connected by normal edges cannot have their copy CC toggled
//you also need to be same toggle as duplicate if yourself if it exists
int dp(int i,int b1,int b2,int b3){
if (memo[i][b1][b2][b3]!=-1) return memo[i][b1][b2][b3];
int res=0;
if (b3) res+=val[i];
if (b3 && (!b1 || !b2)) return memo[i][b1][b2][b3]=-1e9;
for (auto &it:al[i]){
if (it.se==DUP){
res+=dp(it.fi,b1,b2,b3);
}
if (it.se==NORMAL){
if (b1) res+=dp(it.fi,0,0,0);
else{
int temp=0;
rep(x,0,2) rep(y,0,2) rep(z,0,2)
temp=max(temp,dp(it.fi,x,y,z));
res+=temp;
}
}
if (it.se==COPY1){
res+=max(dp(it.fi,b1,b2,0),dp(it.fi,b1,b2,1));
}
if (it.se==COPY2){
if (b3) res+=dp(it.fi,b1,0,0);
else{
int temp=0;
rep(y,0,2) rep(z,0,2)
temp=max(temp,dp(it.fi,b1,y,z));
res+=temp;
}
}
}
return memo[i][b1][b2][b3]=res;
}
int findSample(int N,int confidence[],int host[],int protocol[]){
n=N;
rep(x,0,n) arr[x]=confidence[x];
id[0]=0;
rep(x,1,n){
int h=host[x];
if (protocol[x]==0){
al[id[h]].pub(ii(IDX,DUP));
id[h]=IDX++;
al[id[h]].pub(ii(IDX,NORMAL));
id[x]=IDX++;
}
else if (protocol[x]==1){
al[id[h]].pub(ii(IDX,COPY1));
id[x]=IDX++;
}
else{
al[id[h]].pub(ii(IDX,DUP));
id[h]=IDX++;
al[id[h]].pub(ii(IDX,COPY2));
id[x]=IDX++;
}
}
//rep(x,0,n) cout<<id[x]<<" "; cout<<endl;
rep(x,0,n) val[id[x]]=arr[x];
memset(memo,-1,sizeof(memo));
int ans=0;
rep(x,0,2) rep(y,0,2) rep(z,0,2){
//cout<<x<<" "<<y<<" "<<z<<" "<<dp(0,x,y,z)<<endl;
ans=max(ans,dp(0,x,y,z));
}
return ans;
}
# | 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... |