Submission #1232953

#TimeUsernameProblemLanguageResultExecution timeMemory
1232953kl0989eFriend (IOI14_friend)C++20
23 / 100
1 ms812 KiB
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

/*const int maxn=10;

vector<vi> graph(maxn,vi(maxn,0));

int findSample(int n, int val[], int host[], int prot[]) {// sub1
	for (int i=1; i<n; i++) {
		if (prot[i]==0 || prot[i]==2) {
			graph[i][host[i]]=1;
			graph[host[i]][i]=1;
		}
		if (prot[i]) {
			for (int j=0; j<n; j++) {
				graph[i][j]|=graph[host[i]][j];
				graph[j][i]|=graph[host[i]][j];
			}
		}
	}
	int mx=0;
	for (int i=0; i<(1<<n); i++) {
		bool ok=1;
		for (int j=0; j<n && ok; j++) {
			for (int k=j+1; k<n && ok; k++) {
				if ((i&(1<<j)) && (i&(1<<k)) && graph[j][k]) {
					ok=0;
				}
			}
		}
		if (ok) {
			int cur=0;
			for (int j=0; j<n; j++) {
				if (i&(1<<j)) {
					cur+=val[j];
				}
			}
			mx=max(mx,cur);
		}
	}
	return mx;
}*/

/*int findSample(int n, int val[], int host[], int prot[]) {// sub2
	return accumulate(val,val+n,0);
}*/

/*int findSample(int n, int val[], int host[], int prot[]) {// sub3
	return *max_element(val,val+n);
}*/

/*const int maxn=1010;

vector<vi> graph(maxn);
vector<pi> dp(maxn,pi({0,0}));

void dfs(int cur, int prev=-1) {
	for (auto to:graph[cur]) {
		if (to==prev) {
			continue;
		}
		dfs(to,cur);
		dp[cur].fi+=dp[to].se;
		dp[cur].se+=max(dp[to].fi,dp[to].se);
	}
}

int findSample(int n, int val[], int host[], int prot[]) {// sub4
	for (int i=0; i<n; i++) {
		dp[i].fi=val[i];
	}
	for (int i=1; i<n; i++) {
		graph[i].pb(host[i]);
		graph[host[i]].pb(i);
	}
	dfs(0);
	return max(dp[0].fi,dp[0].se);
}*/

const int maxn=1010;

vector<vi> graph(maxn);
vi seen(maxn,-1);

void dfs(int cur, int mul=1) {
	seen[cur]=mul;
	for (auto to:graph[cur]) {
		if (seen[to]!=-1) {
			continue;
		}
		dfs(to,mul^1);
	}
}

struct Dinic {
	struct edge {
		int u,v;
		ll cap=0,flow=0;

		edge(int _u, int _v, ll _cap): u(_u),v(_v),cap(_cap) {}
	};
	int m=0;
	int n;
	int s,t;
	vector<edge> edges;
	vector<vi> graph;
	vi ptr,lvl;
	queue<int> q;

	Dinic(int _n, int _s, int _t): n(_n),s(_s),t(_t) {
		graph.resize(n);
		ptr.resize(n,0);
		lvl.resize(n,-1);
	}

	int add(int u, int v, ll cap) {
		edges.emplace_back(u,v,cap);
		edges.emplace_back(v,u,0);
		graph[u].pb(m++);
		graph[v].pb(m++);
		return m-2;
	}

	bool bfs() {
		while (q.size()) {
			int a=q.front();
			q.pop();
			for (auto to:graph[a]) {
				if (lvl[edges[to].v]==-1 && edges[to].cap-edges[to].flow) {
					q.push(edges[to].v);
					lvl[edges[to].v]=lvl[a]+1;
				}
			}
		}
		return lvl[t]!=-1;
	}

	ll dfs(int cur, ll push) {
		if (cur==t || push==0) {
			return push;
		}
		for (int& pt=ptr[cur]; pt<graph[cur].size(); pt++) {
			int to=edges[graph[cur][pt]].v;
			if (lvl[to]!=lvl[cur]+1) {
				continue;
			}
			ll p=dfs(to,min(push,edges[graph[cur][pt]].cap-edges[graph[cur][pt]].flow));
			if (p) {
				edges[graph[cur][pt]].flow+=p;
				edges[graph[cur][pt]^1].flow-=p;
				return p;
			}
		}
		return 0;
	}

	ll flow() {
		ll f=0;
		while (1) {
			fill(all(lvl),-1);
			lvl[s]=0;
			q.push(s);
			if (!bfs()) {
				break;
			}
			fill(all(ptr),0);
			while (ll p=dfs(s,1e18)) {
				f+=p;
			}
		}
		return f;
	}
};

int findSample(int n, int val[], int host[], int prot[]) {// sub5
	for (int i=1; i<n; i++) {
		if (prot[i]==0) {
			graph[i].pb(host[i]);
			graph[host[i]].pb(i);
		}
		else {
			for (auto a:graph[host[i]]) {
				graph[a].pb(i);
				graph[i].pb(a);
			}
		}
	}
	for (int i=0; i<n; i++) {
		if (seen[i]==-1) {
			dfs(i);
		}
	}
	Dinic data(n+2,n,n+1);
	for (int i=0; i<n; i++) {
		if (seen[i]) {
			data.add(n,i,1);
			for (auto to:graph[i]) {
				data.add(i,to,n);
			}
		}
		else {
			data.add(i,n+1,1);
		}
	}
	return n-data.flow();
}
#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...