Cod sursa(job #1955120)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 5 aprilie 2017 20:05:20
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.8 kb
#include <bits/stdc++.h>
using namespace std; 
 
int n,m,e,k;
int flow[1234][1234];
int ad[12340][12340];
int parent[1234];
bool visited[1234];
int maxFlow;
vector <int> adj[1234];
bool BFS(){
        memset(visited, 0, sizeof(visited));
        queue <int> que;
        que.push(1);
        visited[1] = 1;
        while(!que.empty()){
            int node = que.front(); 
            que.pop();
            if(node == n)
                return(true);
            for(unsigned i=0;i<adj[node].size(); i++){
                if(!visited[adj[node][i]] && flow[node][adj[node][i]]){
                    visited[adj[node][i]] = true;
                    que.push(adj[node][i]);
                    parent[adj[node][i]] = node;
                }
            }
        }
        return(false);
}

void MaxFlow(){
		while( BFS() ){
                    int current;
                    for(unsigned i=0; i<adj[n].size(); i++){
                            if(!visited[adj[n][i]] || flow[adj[n][i]][n] == 0)
                                continue;
                            parent[n] = adj[n][i];
                            current = 1e9;
                            for(int node=n; node != 1; node = parent[node])
                                current = min(current, flow[parent[node]][node]);
                            maxFlow += current;
                            for (int node = n; node != 1; node = parent[node]){
								//cout << node << ' ';
                                flow[parent[node]][node] -= current;
                                flow[node][parent[node]] += current;
                                 
                            }
                            //cout << endl;
                    }
            }
            printf("%d\n", maxFlow);
            for(int i=2;i<=6; i++)
				for(int j=7; j<=10; j++){
					if(ad[i][j] && flow[i][j] == 0)
					cout << i-1 << ' ' << j-k-1 << endl; // << ' ' << flow[i][j] << endl;
				}
}
 
int main(){
            freopen ("cuplaj.in", "r", stdin);
            freopen ("cuplaj.out", "w", stdout);
            scanf("%d%d%d", &n,&m,&e);
            for(int i = 0; i < e; i++){
                    int x,y;
                    scanf("%d%d", &x, &y);
                    ad[x+1][y+n+1] = 1;
                    ad[y+n+1][x+1] = 1;
                    adj[x+1].push_back(y+n+1);
                    adj[1].push_back(x+1);
                    adj[x+1].push_back(1);
                    adj[y+n+1].push_back(x+1);
                    adj[y+n+1].push_back(n+m+2);
                    adj[n+m+2].push_back(y+n+1);
                    flow[x+1][y+n+1] = 1;
                    flow[1][x+1] = 1;
                    flow[y+n+1][n+m+2] = 1;
                    //cout << 1 << ' ' << x+1 << ' ' << y+n+1 <<' ' << n+m+2 << endl;
            }
            k = n;
            n=n+m+2;
            MaxFlow();
}