Created
October 15, 2016 16:22
-
-
Save knuu/64ba401cfd58bd803e527cbd588ba721 to your computer and use it in GitHub Desktop.
AtCoder Beginner Contest 010 D. 浮気予防
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++) | |
#define REP(i,x) FOR(i,0,x) | |
#define INF 1<<29 | |
template <typename T> | |
struct MaxFlow { | |
struct Edge { | |
int to, rev; T cap; | |
Edge(int to, int rev, T cap) : to(to), rev(rev), cap(cap) { } | |
}; | |
typedef vector<Edge> Edges; | |
vector<Edges> G; | |
int V; | |
vector<int> level, iter; | |
MaxFlow(int V) : V(V) { G.resize(V); } | |
void add_edge(int from, int to, T cap) { | |
G[from].emplace_back(to, G[to].size(), cap); | |
G[to].emplace_back(from, (int)G[from].size()-1, 0); | |
} | |
void bfs(int source) { | |
level.assign(V, -1); | |
queue<int> que; | |
que.emplace(source); | |
level[source] = 0; | |
while (!que.empty()) { | |
int v = que.front(); que.pop(); | |
for (int i = 0; i < (int)G[v].size(); i++) { | |
Edge &e = G[v][i]; | |
if (e.cap > 0 && level[e.to] < 0) { | |
level[e.to] = level[v] + 1; | |
que.emplace(e.to); | |
} | |
} | |
} | |
} | |
T dfs(int v, int sink, T flow) { | |
if (v == sink) return flow; | |
for (int &i = iter[v]; i < (int)G[v].size(); i++) { | |
Edge &e = G[v][i]; | |
if (e.cap > 0 && level[v] < level[e.to]) { | |
T d = dfs(e.to, sink, min(e.cap, flow)); | |
if (d > 0) { | |
e.cap -= d; | |
G[e.to][e.rev].cap += d; | |
return d; | |
} | |
} | |
} | |
return 0; | |
} | |
T dinic(int source, int sink) { | |
T flow = 0; | |
while (true) { | |
bfs(source); | |
if (level[sink] < 0) return flow; | |
iter.assign(V, 0); | |
T f; | |
while ((f = dfs(source, sink, INF)) > 0) flow += f; | |
} | |
} | |
}; | |
int main() { | |
// use scanf in CodeForces! | |
cin.tie(0); | |
ios_base::sync_with_stdio(false); | |
int N, G, E; cin >> N >> G >> E; | |
MaxFlow<int> mf(N+1); | |
int source = 0, sink = N; | |
vector<int> girls(G); REP(i, G) cin >> girls[i]; | |
for (int g : girls) mf.add_edge(g, sink, 1); | |
REP(i, E) { | |
int a, b; cin >> a >> b; | |
if (a > b) swap(a, b); | |
mf.add_edge(a, b, 1); | |
mf.add_edge(b, a, 1); | |
} | |
cout << mf.dinic(source, sink) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment