Skip to content

Instantly share code, notes, and snippets.

@takageymt
Created March 9, 2017 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takageymt/3f28617909df845bb44c42eed160595c to your computer and use it in GitHub Desktop.
Save takageymt/3f28617909df845bb44c42eed160595c to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
struct Dinic {
struct edge {
int to, cap, rev;
edge(){}
edge(int to, int cap, int rev):to(to), cap(cap), rev(rev){}
};
vector< vector<edge> > graph;
vector<int> level, iter;
Dinic(int V):graph(V), level(V), iter(V){}
void add_edge(int from, int to, int cap) {
graph[from].emplace_back(to, cap, graph[to].size());
graph[to].emplace_back(from, 0, graph[from].size()-1);
}
void bfs(int s) {
fill(all(level), -1);
queue<int> que;
level[s] = 0;
que.push(0);
while(que.size()) {
int v = que.front(); que.pop();
for(edge& e : graph[v]) {
if(e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f) {
if(v == t) return f;
for(int& i = iter[v]; i < graph[v].size(); i++) {
edge& e = graph[v][i];
if(e.cap > 0 && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(e.cap, f));
if(d > 0) {
e.cap -= d;
graph[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t) {
int flow = 0;
while(1) {
bfs(s);
if(level[t] < 0) return flow;
fill(all(iter), 0);
int f; while((f = dfs(s, t, inf)) > 0) flow += f;
}
}
};
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
int N, G, E;
cin >> N >> G >> E;
int s = 0, t = N+1, V = t+1;
Dinic graph(V);
rep(i, G) {
int p; cin >> p;
graph.add_edge(p, t, 1);
}
rep(i, E) {
int a, b;
cin >> a >> b;
graph.add_edge(a, b, 1);
graph.add_edge(b, a, 1);
}
cout << graph.max_flow(s, t) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment