1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| # include<algorithm> # include<cstring> # include<cstdio> # include<queue>
using namespace std;
const int INF = 1e9; const int N = 205;
int n, m, u, v, S, T;
struct Edge{ int from, nxt, to, cap; }edge[N*N*2]; int head[N], edgeNum = 1; void addEdge(int from, int to, int cap){ edge[++edgeNum].nxt = head[from]; edge[edgeNum].from = from; edge[edgeNum].to = to; edge[edgeNum].cap = cap; head[from] = edgeNum; }
int lay[N]; bool bfs(){ memset(lay, 0, sizeof lay); queue<int> q; q.push(S); lay[S] = 1; while(!q.empty()){ int cur = q.front(); q.pop(); for(int i = head[cur]; i; i = edge[i].nxt){ if(edge[i].cap <= 0) continue; if(!lay[edge[i].to]){ lay[edge[i].to] = lay[cur] + 1; q.push(edge[i].to); } } } return lay[T]; } int dfs(int x, int minn){ if(x == T) return minn; for(int i = head[x]; i; i = edge[i].nxt){ if(edge[i].cap <= 0 || lay[edge[i].to] != lay[x] + 1) continue; int t = dfs(edge[i].to, min(minn, edge[i].cap)); if(t){ edge[i].cap -= t; edge[i^1].cap += t; return t; } } return 0; } int dinic(){ int maxFlow = 0; while(bfs()) if(int flow = dfs(S, INF)) maxFlow += flow; return maxFlow; }
int main(){ scanf("%d%d", &m, &n); S = n + 1, T = n + 2; for(int i = 1; i <= m; i++){ addEdge(S, i, 1); addEdge(i, S, 0); } for(int i = m + 1; i <= n; i++){ addEdge(i, T, 1); addEdge(T, i, 0); } while(scanf("%d%d", &u, &v) && (u != -1 && v != -1)){ addEdge(u, v, 1); addEdge(v, u, 0); } int ans = dinic(); if(ans == 0) return puts("No Solution!"), 0; printf("%d\n", ans); for(int i = 1; i <= edgeNum; i += 2) if(edge[i].cap == 1 && edge[i].from != S && edge[i].from != T && edge[i].to != S && edge[i].to != T) printf("%d %d\n", edge[i].from, edge[i].to); return 0; }
|