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 s,t,n,m = 0;
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 <= 0) 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 < 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 <= 0) 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;
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];
int dp[maxn][2];
void dfs(int beg, int par)
{
dp[beg][0] = dp[beg][1] = 0;
int sum[2] = {0,0};
for(int nb : g[beg])
{
if(nb==par) continue;
dfs(nb,beg);
sum[0] += dp[nb][0];
sum[1] += dp[nb][1];
}
dp[beg][0] = sum[1];
dp[beg][1] = max(dp[beg][0] , sum[0] + con[beg]);
}
int used[maxn];
vector<int>team[3];
void dfs_color(int beg)
{
for(int nb : g[beg])
{
if(used[nb]) continue;
used[nb] = 3-used[beg];
team[used[nb]].push_back(nb);
dfs_color(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;
bool f24 = true;
for(int i = 1; i < n; i++)
{
if(i>1 && protocol[i-1] != protocol[i]) f24 = false;
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(f24)
{
if(protocol[1]==1)
{
int ans = 0;
for(int i = 0; i < n; i++) ans += con[i];
return ans;
}
dfs(0,0);
return max(dp[0][0],dp[0][1]);
}
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_color(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 n-patok.flow();
}
Compilation message (stderr)
friend.cpp: In constructor 'Dinic::Dinic(int, int, int)':
friend.cpp:19:13: warning: 'Dinic::n' will be initialized after [-Wreorder]
19 | int s,t,n,m = 0;
| ^
friend.cpp:19:9: warning: 'int Dinic::s' [-Wreorder]
19 | int s,t,n,m = 0;
| ^
friend.cpp:23:5: warning: when initialized here [-Wreorder]
23 | Dinic(int n, int s, int t) : n(n), s(s), t(t)
| ^~~~~
friend.cpp: In member function 'long long int Dinic::dfs(int, long long int)':
friend.cpp:61:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int &cid = ptr[v];cid < adj[v].size(); cid++)
| ~~~~^~~~~~~~~~~~~~~
# | 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... |