C++namespace maxflow
{
const int N = 205,M = 1205;
int n,m,s,t,level[N],it[N],head[N],tot = 1;
struct node {int to,cf,rev,nxt;} g[M];
inline void add(int u,int v,int w,int rw = 0)
{
g[tot+1] = {v,w,head[v],head[u]}, head[u] = tot+1,
g[tot+2] = {u,rw,head[u],head[v]}, head[v] = tot+2,
tot += 2,
g[head[u]].rev = head[v],
g[head[v]].rev = head[u];
}
bool bfs()
{
queue<int> q;int u;
for (int i = 1;i <= n;i ++) level[i] = 0;
q.push(s),level[s] = 1;
while (!q.empty())
{
u = q.front(),q.pop();
for (int i = head[u];i;i = g[i].nxt)
{
auto& e = g[i];
if (!level[e.to] && e.cf)
level[e.to] = level[u]+1,q.push(e.to);
}
}
return level[t];
}
int dfs(int u,int w)
{
if (u == t) return w;
int cnt = 0,c;
for (int& i = it[u];i;i = g[i].nxt)
{
auto& e = g[i];
if (level[u]+1==level[e.to] && e.cf)
{
c = dfs(e.to,min(w,e.cf));
if (!c) level[e.to] = INT_MAX;
e.cf -= c,g[e.rev].cf += c,w -= c,cnt += c;
}
}
return cnt;
}
int dinic()
{
int ans = 0;
while (bfs())
{
for (int i = 1;i <= n;i ++) it[i] = head[i];
ans += dfs(s,INT_MAX);
}
return ans;
}
}