제출 #438292

#제출 시각아이디문제언어결과실행 시간메모리
438292bobbilykingFriend (IOI14_friend)C++17
16 / 100
31 ms5816 KiB
#include "friend.h"
#include<bits/stdc++.h>
using namespace std;

#define N 100010
typedef long long int ll;
ll cost[N];
vector<ll> adj[N];

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 (n <= 10){
		for (int i =1;i<n;i++){
			if(p[i]==0 or p[i]==2){
				adj[i].push_back(h[i]);
				adj[h[i]].push_back(i);
			}
			if(p[i]==1 or p[i]==2){
				for (int x: adj[h[i]]){
					adj[x].push_back(i);
					adj[i].push_back(x);
				}
			}
		}

		for (int i =0 ; i < (1<<n); i++){
			int sum = 0;
			bool works = true;
			bool vis[N];
			for (int i =0 ; i < n; i++) vis[i] = false;
			for (int j =0 ; j < n; j++){
				if ((i&(1<<j))>0) {
					vis[j]=true;
					sum+=c[j];
				}
			}
			for (int i = 0 ;  i < n;  i++){
				if (vis[i]){
					for (int x:adj[i]) if(vis[x])works=false;
				}
			}
			if (!works)continue;
			ans = max(ans,sum);
		}
	}
	else if (prot[1]==n-1){
		ans  =  sum;
	}
	else if (prot[2]==n-1){
		ans  =  mx;
	}
	else if (prot[0] == n-1){
		for (int i = 1;  i <  n;  i++){
			adj[i].push_back(h[i]);
			adj[h[i]].push_back(i);
		}
		bool vis[N];
		for (int i  =  0 ; i <  n;  i++)vis[i] =false;
		int cur = 1;
		int sum  =  0;
		while(cur != -1){
			cur =-1;
			int cost = INT_MIN;
			for (int i = 0; i  <  n;  i++){
				if (!vis[i]){
					int cc= 0;
					cc = c[i];
					for (int x: adj[i]){
						if (vis[x]) continue;
						cc-=c[x];
					}
					if (cc>cost){
						cost  = cc;
						cur  = i;
					}
				}
			}
			//cout<<"Chose " << cur  <<'\n';
			if (cur !=  -1){
				sum+=c[cur];
				for (int x:  adj[cur])  vis[x]= true;
				vis[cur]=true;
			}
			else break;
		}
		ans  =  sum;
	}
	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';
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...