Submission #298056

#TimeUsernameProblemLanguageResultExecution timeMemory
298056miss_robotFriend (IOI14_friend)C++14
46 / 100
1092 ms49776 KiB
#include <bits/stdc++.h>
#include "friend.h"

#pragma GCC optimize("O3")

using namespace std;

int st1(int n, int confidence[], int host[], int protocol[]){
	vector<int> g(n);
	for(int i = 1; i < n; i++){
		if(protocol[i]){
			g[i] |= g[host[i]];
			for(int j = 0; j < i; j++) if(g[host[i]]&(1<<j)) g[j] |= (1<<i);
		}
		if(protocol[i] != 1){
			g[i] |= (1<<host[i]);
			g[host[i]] |= (1<<i);
		}
	}
	int t, s = 0;
	for(int i = 0; i < (1<<n); i++){
		t = 0;
		for(int j = 0; j < n; j++) if(i&(1<<j)) t += confidence[j];
		for(int j = 0; j < n; j++) if((i&(1<<j)) && (i&(g[j]))) t = 0;
		s = max(s, t);
	}
	return s;
}

int st2(int n, int confidence[]){
	int s = 0;
	for(int i = 0; i < n; i++) s += confidence[i];
	return s;
}

int st3(int n, int confidence[]){
	int mx = 0;
	for(int i = 0; i < n; i++) mx = max(mx, confidence[i]);
	return mx;
}

void dfs(int u, vector< vector<int> >& g, vector<int>& dp, vector<int>& a){
	for(int v : g[u]){
		dfs(v, g, dp, a);
		a[u] += dp[v];
		dp[u] += a[v];
	}
	dp[u] = max(dp[u], a[u]);
}

int st4(int n, int confidence[], int host[]){
	vector< vector<int> > g(n);
	vector<int> dp(n), a(n);
	for(int i = 1; i < n; i++) g[host[i]].push_back(i);
	for(int i = 0; i < n; i++) dp[i] = confidence[i];
	dfs(0, g, dp, a);
	return dp[0];
}

int match(int u, vector<int>& x, vector<int>& vis, vector< vector<int> >& g, vector<int>& y){
	for(int v : g[u]){
		if(vis[y[v]]) continue;
		vis[y[v]] = 1;
		if(x[y[v]] == -1 || match(x[y[v]], x, vis, g, y)){
			x[y[v]] = u;
			return 1;
		}
	}
	return 0;
}

void fnd(int u, vector< vector<int> >& g, vector<int>& vis, int c, vector<int>& a, vector<int>& b){
	if(vis[u]) return;
	vis[u] = 1;
	if(c) a.push_back(u);
	else b.push_back(u);
	for(int v : g[u]) fnd(v, g, vis, c^1, a, b);
}

int st5(int n, int host[], int protocol[]){
	vector< vector<int> > g(n);
	for(int i = 1; i < n; i++){
		if(protocol[i]){
			for(int v : g[host[i]]) g[i].push_back(v), g[v].push_back(i);
		}
		else{
			g[i].push_back(host[i]), g[host[i]].push_back(i);
		}
	}
	vector<int> vis(n), a, b, y(n);
	for(int i = 0; i < n; i++) fnd(i, g, vis, 1, a, b);
	int p = a.size(), q = b.size();
	for(int i = 0; i < p; i++) y[a[i]] = i;
	for(int i = 0; i < q; i++) y[b[i]] = i;
	int s = n;
	vector<int> x(q, -1);
	for(int i = 0; i < p; i++){
		vis.assign(q, 0);
		if(match(a[i], x, vis, g, y)) s-=2;
	}
	return s;
}

int findSample(int n, int confidence[], int host[], int protocol[]){
	if(n <= 10) return st1(n, confidence, host, protocol);
	for(int i = 2; i < n; i++) if(protocol[i] != protocol[i-1])
		return st5(n, host, protocol);
	if(protocol[1] == 1) return st2(n, confidence);
	if(protocol[1]) return st3(n, confidence);
	return st4(n, confidence, host);
}
#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...