#pragma region template #include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vld = vector; using vvld = vector; using vvvld = vector; using vs = vector; using pll = pair; using vp = vector; template using pqrev = priority_queue, greater>; #define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++) #define repb(i, n) for (ll i = (n)-1; i >= 0; i--) #define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++) #define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--) #define ALL(a) (a).begin(), (a).end() #define SZ(x) ((ll)(x).size()) //* constexpr ll MOD = 1000000007; /*/ constexpr ll MOD = 998244353; //*/ constexpr ll INF = 1e+18; constexpr ld EPS = 1e-12L; constexpr ld PI = 3.14159265358979323846L; constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; } template inline bool chmax(S &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(S &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #ifdef OJ_LOCAL #include "dump.hpp" #else #define dump(...) ((void)0) #endif template bool print_(const T &a) { cout << a; return true; } template bool print_(const vector &vec) { for (auto &a : vec) { cout << a; if (&a != &vec.back()) { cout << " "; } } return false; } template bool print_(const vector> &vv) { for (auto &v : vv) { for (auto &a : v) { cout << a; if (&a != &v.back()) { cout << " "; } } if (&v != &vv.back()) { cout << "\n"; } } return false; } void print() { cout << "\n"; } template void print(Head &&head, Tail &&... tail) { bool f = print_(head); if (sizeof...(tail) != 0) { cout << (f ? " " : "\n"); } print(forward(tail)...); } #pragma endregion // Primal-Dual FE(log V) // https://ei1333.github.io/luzhiled/snippets/graph/primal-dual.html template struct GraphMCF { struct edge { int to; flow_t cap; cost_t cost; int rev; bool isrev; }; vector> graph; vector potential, min_cost; vector prevv, preve; const cost_t MAX; GraphMCF(int V) : graph(V), MAX(sqrtl(numeric_limits::max())) {} void add_edge(int from, int to, flow_t cap, cost_t cost) { graph[from].emplace_back( (edge){to, cap, cost, (int)graph[to].size(), false}); graph[to].emplace_back( (edge){from, 0, -cost, (int)graph[from].size() - 1, true}); } cost_t flow(int s, int t, flow_t f) { int V = (int)graph.size(); cost_t ret = 0; using Pi = pair; priority_queue, greater> que; potential.assign(V, 0); preve.assign(V, -1); prevv.assign(V, -1); while (f > 0) { min_cost.assign(V, MAX); que.emplace(0, s); min_cost[s] = 0; while (!que.empty()) { Pi p = que.top(); que.pop(); if (min_cost[p.second] < p.first) continue; for (int i = 0; i < int(graph[p.second].size()); i++) { edge &e = graph[p.second][i]; cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = p.second, preve[e.to] = i; que.emplace(min_cost[e.to], e.to); } } } if (min_cost[t] == MAX) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; flow_t addflow = f; for (int v = t; v != s; v = prevv[v]) { addflow = min(addflow, graph[prevv[v]][preve[v]].cap); } f -= addflow; ret += addflow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = graph[prevv[v]][preve[v]]; e.cap -= addflow; graph[v][e.rev].cap += addflow; } } return ret; } void output() { for (int i = 0; i < graph.size(); i++) { for (auto &e : graph[i]) { if (e.isrev) continue; auto &rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl; } } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll n, m, a, b; cin >> n >> m >> a >> b; GraphMCF g(n+3); rep(i, m){ ll l, r; cin >> l >> r; g.add_edge(l-1, r, 1, 1); g.add_edge(r, l-1, 1, 1); } rep(i, a) g.add_edge(n+1, i, 1, 0); repr(i, b, n+1) g.add_edge(i, n+2, 1, 0); print(g.flow(n+1, n+2, 1)); }