#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; // const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, // dx[] = {0, -1, -1, -1, 0, 1, 1, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; /*-------------------------------------------------*/ template struct Dinic { struct Edge { int dst, rev; T cap; Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {} }; vector > graph; Dinic(int n) : graph(n), level(n), itr(n) {} void add_edge(int src, int dst, T cap) { graph[src].emplace_back(dst, cap, graph[dst].size()); graph[dst].emplace_back(src, 0, graph[src].size() - 1); } T maximum_flow(int s, int t, T limit) { T res = 0; while (true) { bfs(s); if (level[t] == -1) return res; fill(ALL(itr), 0); T tmp; while ((tmp = dfs(s, t, limit)) > 0) res += tmp; } } private: vector level, itr; void bfs(int s) { fill(ALL(level), -1); queue que; level[s] = 0; que.emplace(s); while (!que.empty()) { int ver = que.front(); que.pop(); for (Edge e : graph[ver]) if (level[e.dst] == -1 && e.cap > 0) { level[e.dst] = level[ver] + 1; que.emplace(e.dst); } } } T dfs(int ver, int t, T flow) { if (ver == t) return flow; for (; itr[ver] < graph[ver].size(); ++itr[ver]) { Edge &e = graph[ver][itr[ver]]; if (level[ver] < level[e.dst] && e.cap > 0) { T tmp = dfs(e.dst, t, min(flow, e.cap)); if (tmp > 0) { e.cap -= tmp; graph[e.dst][e.rev].cap += tmp; return tmp; } } } return 0; } }; template