Submission #1324830

#TimeUsernameProblemLanguageResultExecution timeMemory
1324830tsetsenbilegFriend (IOI14_friend)C++20
16 / 100
1 ms332 KiB
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using pr = pair<int, int>;
const int INF = 1e9+7, MOD = 1e9+7;
vector<vector<int>> edge;
vector<array<int, 2>> dp;
vector<int> v;

void dfs(int node, int par) {
	dp[node][1] = v[node];
	for (auto next : edge[node]) {
		if (next == par) continue;
		dfs(next, node);
		dp[node][0] += max(dp[next][0], dp[next][1]);
		dp[node][1] += dp[next][0];
	}
}

// Find out best sample
int findSample(int n,int a[],int host[],int type[]){
	if (n <= 10) {
		vector<vector<bool>> con(n, vector<bool> (n, 0));
		for (int i = 1; i < n; i++) {
			int h = host[i];
			if (type[i] == 0 || type[i] == 2) {
				edge[i][h] = 1;
				edge[h][i] = 1;
			}
			if (type[i] == 1 || type[i] == 2) {
				for (int j = 0; j < n; j++) edge[i][j] = edge[h][j];
			}
		}
		int res = 0;
		for (int m = 0; m < (1 << n); m++) {
			vector<int> lit;
			bool good = 1;
			int sum = 0;
			for (int i = 0; i < n; i++) {
				if (m & (1 << i)) lit.pb(i);
			}
			for (int i = 0; i < lit.size(); i++) {
				sum += a[lit[i]];
				for (int j = i + 1; j < lit.size(); j++) {
					if (edge[lit[i]][lit[j]]) good = 0;
				}
			}
			if (good) res = max(res, sum);
		}
		return res;
	}
	edge.resize(n);
	v.resize(n);
	for (int i = 0; i < n; i++) v[i] = a[i];
	dp.resize(n, {0, 0});
	for (int i = 1; i < n; i++) {
		if (type[i] == 0) {
			edge[i].pb(host[i]);
			edge[host[i]].pb(i);
		}
	}
	dfs(0, -1);
	int res = -INF;
	if (type[1] == 0) {
		res = max(dp[0][0], dp[0][1]);
	}
	if (type[1] == 1) {
		res = 0;
		for (int i = 0; i < n; i++) res += a[i];
	}
	if (type[1] == 2) {
		for (int i = 0; i < n; i++) res = max(res, a[i]);
	}
	return res;
}
#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...