Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- #define endl '\n'
- const int N = 1e2 + 10;
- const int INF = 0x3f3f3f3f;
- class Dinic {
- private:
- struct Edge {
- int v, nxt, cap, flow;
- } e[N * N];
- int fir[N], cnt = 0;
- int dep[N], cur[N];
- ll maxflow = 0;
- int S, T;
- public:
- void init (int s, int t) {
- S = s;
- T = t;
- memset(fir, -1, sizeof fir);
- cnt = 0;
- }
- void addEdge (int u, int v, int c) {
- e[cnt] = {v, fir[u], c, 0};
- fir[u] = cnt ++;
- e[cnt] = {u, fir[v], 0, 0};
- fir[v] = cnt ++;
- }
- int bfs () {
- queue<int> q;
- memset(dep, 0, sizeof dep);
- dep[S] = 1;
- q.push(S);
- while (!q.empty()) {
- int x = q.front();
- q.pop();
- for (int i = fir[x]; ~i; i = e[i].nxt) {
- int y = e[i].v;
- if (!dep[y] && e[i].cap > e[i].flow) {
- dep[y] = dep[x] + 1;
- q.push(y);
- }
- }
- }
- return dep[T];
- }
- int dfs (int x, int flow) {
- if (x == T || flow == 0) return flow;
- int res = 0;
- for (int i = cur[x]; ~i; i = e[i].nxt) {
- int y = e[i].v;
- if (dep[y] == dep[x] + 1) {
- int d = dfs(y, min(flow - res, e[i].cap - e[i].flow));
- res += d;
- e[i].flow += d;
- e[i ^ 1].flow -= d;
- if (res == flow) return res;
- }
- }
- return res;
- }
- int dinic () {
- while (bfs()) {
- memcpy(cur, fir, sizeof cur);
- maxflow += dfs(S, INF);
- }
- return maxflow;
- }
- void print() {
- for (int x = 1; x < S; x ++) {
- for (int i = cur[x]; ~i; i = e[i].nxt) {
- int y = e[i].v;
- if(y < S && e[i].flow > 0) {
- cout << x << ' ' << y << endl;
- }
- }
- }
- }
- };
- Dinic dinic;
- int n, m;
- void solve() {
- cin >> m >> n;
- int s = n + 1, t = n + 2;
- dinic.init(n + 1, n + 2);
- for (int i = 1; i <= n; i ++) {
- if (i <= m) dinic.addEdge(s, i, 1);
- else dinic.addEdge(i, t, 1);
- }
- while (1) {
- int u, v;
- cin >> u >> v;
- if (u == - 1 && v == -1) break;
- dinic.addEdge(u, v, 1);
- }
- cout << dinic.dinic() << endl;
- dinic.print();
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- int _ = 1;
- // cin >> _;
- while (_--){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement