This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
struct FlowEdge {
int v, u;
long long cap, flow = 0;
FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
const long long flow_inf = 1e18;
vector<FlowEdge> edges;
vector<vector<int>> adj;
int n, m = 0;
int s, t;
vector<int> level, ptr;
queue<int> q;
Dinic(int n, int s, int t) : n(n), s(s), t(t) {
adj.resize(n);
level.resize(n);
ptr.resize(n);
}
void add_edge(int v, int u, long long cap) {
edges.emplace_back(v, u, cap);
edges.emplace_back(u, v, 0);
adj[v].push_back(m);
adj[u].push_back(m + 1);
m += 2;
}
bool bfs() {
while (!q.empty()) {
int v = q.front();
q.pop();
for (int id : adj[v]) {
if (edges[id].cap - edges[id].flow < 1)
continue;
if (level[edges[id].u] != -1)
continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
long long dfs(int v, long long pushed) {
if (pushed == 0)
return 0;
if (v == t)
return pushed;
for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
int id = adj[v][cid];
int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
continue;
long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0)
continue;
edges[id].flow += tr;
edges[id ^ 1].flow -= tr;
return tr;
}
return 0;
}
long long flow() {
long long f = 0;
while (true) {
fill(level.begin(), level.end(), -1);
level[s] = 0;
q.push(s);
if (!bfs())
break;
fill(ptr.begin(), ptr.end(), 0);
while (long long pushed = dfs(s, flow_inf)) {
f += pushed;
}
}
return f;
}
};
const int maxn = 1024;
vector<int>g[maxn];
int con[maxn],used[maxn];
vector<int>team[3];
void dfs(int beg)
{
for(int nb : g[beg])
{
if(used[nb]) continue;
used[nb] = 3-used[beg];
team[used[nb]].push_back(nb);
dfs(nb);
}
}
int findSample(int n, int confidence[], int host[], int protocol[])
{
for(int i = 0; i < n; i++) con[i] = confidence[i];
bool f3 = true;
for(int i = 1; i < n; i++)
{
if(protocol[i]!=2) f3 = false;
if(protocol[i]==0)
{
g[host[i]].push_back(i);
g[i].push_back(host[i]);
}
if(protocol[i]==1)
{
for(int nb : g[host[i]])
{
g[nb].push_back(i);
g[i].push_back(nb);
}
}
if(protocol[i]==2)
{
for(int nb : g[host[i]])
{
g[nb].push_back(i);
g[i].push_back(nb);
}
g[host[i]].push_back(i);
g[i].push_back(host[i]);
}
}
///SUBTASKS
if(f3)
{
int ans = 0;
for(int i = 0; i < n; i++) if(con[i]>ans) ans = con[i];
return ans;
}
if(n<=10)
{
int ans = 0;
for(int mask = 1; mask < (1<<n); mask++)
{
int ch = 0;
for(int j = 0; j < n; j++) if(mask&(1<<j)) ch += confidence[j];
bool f = true;
for(int j = 0; j < n; j++)
{
if((mask&(1<<j))==0) continue;
for(int nb : g[j]) if(mask&(1<<nb)) f = false;
}
if(f) ans = max(ans,ch);
}
return ans;
}
long long inf = 1e18;
for(int i = 0; i < n; i++)
{
if(used[i]) continue;
team[1].push_back(i);
used[i] = 1;
dfs(i);
}
Dinic patok(n+2,n,n+1);
for(int red : team[1])
{
patok.add_edge(n,red,1);
for(int blue : g[red]) patok.add_edge(red,blue,inf);
}
for(int blue : team[2]) patok.add_edge(blue,n+1,1);
return patok.flow();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |