제출 #285837

#제출 시각아이디문제언어결과실행 시간메모리
285837Saboon친구 (IOI14_friend)C++14
69 / 100
101 ms20472 KiB
#include "friend.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
const int inf = 1e9;

int n, V[maxn], c[4*maxn];
short type[4*maxn];
vector<int> t[4*maxn];
int par[4*maxn];

pair<int,int> dfs(int v){
	if (type[v] == 1){
		int lc = t[v][0], rc = t[v][1];
		auto it1 = dfs(lc);
		auto it2 = dfs(rc);
		int dp = it1.first+it2.second;
		int pd = it1.second+it2.first;
		dp = max(dp,pd);
		return {dp,pd};
	}
	else if (type[v] == 0){
		int dp = c[v], pd = 0;
		for (auto u : t[v]){
			auto it = dfs(u);
			dp += it.first, pd += it.second;
		}
		dp = max(dp,pd);
		return {dp,pd};
	}
	else{
		int lc = t[v][0], rc = t[v][1];
		auto it1 = dfs(lc);
		auto it2 = dfs(rc);
		int dp = max(it1.first+it2.second, it1.second+it2.first);
		int pd = it1.second+it2.second;
		dp = max(dp,pd);
		return {dp,pd};
	}
}

void addEdge(int v, int u){
	t[v].push_back(u);
	par[u] = v;
}

int findSample(int N,int confidence[],int host[],int protocol[]){
	n = N;
	if (*min_element(protocol+1, protocol+n) == 2 and *max_element(protocol+1, protocol+n) == 2)
		return *max_element(confidence, confidence+n);
	int newint = n+1;
	addEdge(0, 1);
	V[0] = 1;
	c[1] = confidence[0];
	for (int i = 1; i < n; i++){
		int v = V[host[i]];
		if (protocol[i] == 0){
			c[v] = 0;
			type[v] = 1;
			addEdge(v, newint++);
			addEdge(newint-1, newint);
			V[host[i]] = newint++;
			addEdge(v, newint++);
			addEdge(newint-1, newint);
			V[i] = newint++;
			c[V[i]] = confidence[i];
			c[V[host[i]]] = confidence[host[i]];
		}
		else if (protocol[i] == 1){
			addEdge(par[v], newint);
			V[i] = newint++;
			c[V[i]] = confidence[i];
		}
		else{
			c[v] = 0;
			type[v] = 2;
			addEdge(v, newint++);
			addEdge(newint-1, newint);
			V[host[i]] = newint++;
			addEdge(v, newint++);
			addEdge(newint-1, newint);
			V[i] = newint++;
			c[V[i]] = confidence[i];
			c[V[host[i]]] = confidence[host[i]];
		}
	}
	return dfs(0).first;
}
#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...