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 <vector>
#include <cassert>
#include <tuple>
#include <queue>
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
#define INF 1145141919
using namespace std;
typedef pair<int, int> P;
struct Dinic {
struct Edge{ int to, cap, rev; };
vector<Edge> G[1002];
int level[1002];
void add_edge(int x, int y, int cap) {
G[x].pb((Edge) { y, cap, (int)G[y].size() });
G[y].pb((Edge) { x, 0, (int)G[x].size()-1 });
}
int dfs(int x, int goal, int f) {
if (x == goal) return f;
for (Edge &e : G[x]) if (level[x] < level[e.to] && e.cap > 0) {
int w = min(f, e.cap);
int ret = dfs(e.to, goal, w);
if (ret > 0) {
e.cap -= w;
G[e.to][e.rev].cap += w;
return ret;
}
}
return 0;
}
void bfs(int s) {
rep(i, 1002) level[i] = INF;
level[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
for (Edge e : G[x]) if (level[e.to] == INF && e.cap > 0) {
level[e.to] = level[x]+1;
q.push(e.to);
}
}
}
int maxflow(int s, int t) {
int flow = 0;
while (true) {
bfs(s);
if (level[t] == INF) return flow;
while (true) {
int f = dfs(s, t, INF);
if (f == 0) break;
flow += f;
}
}
}
} dinic;
bool color[1000];
bool G[1000][1000];
int findSample(int N, int A[], int host[], int protocol[]) {
assert(N<=1000);
rep(i, N) assert(A[i] == 1);
color[0] = 0;
for (int i=1; i<N; i++) {
int p = host[i];
if (protocol[i] == 0) {
color[i] = color[p]^1;
G[i][p] = G[p][i] = true;
}
else {
assert(protocol[i] == 1);
color[i] = color[p];
rep(j, N) G[i][j] = G[j][i] = G[p][j];
}
}
vector<int> left, right;
rep(i, N) if (color[i] == 0) left.pb(i); else right.pb(i);
int s = N, t = N+1;
for (int x : left) dinic.add_edge(s, x, 1);
for (int x : right) dinic.add_edge(x, t, 1);
for (int x : left) for (int t : right) if (G[x][t]) dinic.add_edge(x, t, 1);
return N-dinic.maxflow(s, t);
}
# | 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... |