Cod sursa(job #2425412)

Utilizator FlaviusFeteanFetean Flavius FlaviusFetean Data 24 mai 2019 19:51:43
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.15 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

int M[10005];

bool u[10005], v[10005];

vector<int> uList[10005], vList[10005];

struct muchie{
int x, y; bool matched = 0;
} mList[10005], p;

int main()
{
    int n, m, e, x, y, i, j, s, ctm = 0, sp, z, selu[10005] = {0}, selv[10005] = {0};

    fin >> n >> m >> e;

    for(i = 1; i <= e; i++){
        fin >> x >> y;
        mList[i].x = x;
        mList[i].y = y;
        uList[x].push_back(i); vList[y].push_back(i);
    }



    for(i = 1; i <= n; i++){
        s = uList[i].size();
        for(j = 0; j < s; j++){
            x = uList[i][j]; p = mList[x];
            if(!v[p.y]){
                M[p.x] = p.y; ctm++;
                mList[x].matched = 1;
                u[i] = v[p.y] = 1;
                break;
            }
        }
    }



    stack<pair<int, int> > st;

    for(i = 1; i <= n; i++){
        if(u[i]) continue; sp = 0;
        selu[i] = 1;
        s = uList[i].size();
        for(j = 0; j < s; j++){
            x = uList[i][j];
            if(!mList[x].matched){
                st.push(make_pair(x, j + 1));
                selv[mList[x].y] = 1; break;
            }
        }
        while(!st.empty()){
            x = st.top().first;
            if(mList[x].matched){
                y = mList[x].x;
                s = uList[y].size();
                for(j = sp; j < s; j++){
                    z = uList[y][j];
                    if(!mList[z].matched && !selv[mList[z].y]){
                        selu[mList[z].x] = 1;
                        st.push(make_pair(z, j + 1));
                        sp = 0; break;
                    }
                }
                if(j == s){
                    x = st.top().first;
                    selu[mList[x].x] = 0;
                    sp = st.top().second; st.pop();
                }
            }
            else{
                y = mList[x].y;
                if(!v[y]) break;
                s = vList[y].size();
                for(j = sp; j < s; j++){
                    z = vList[y][j];
                    if(mList[z].matched && !selu[mList[z].x]){
                        selv[mList[z].y] = 1;
                        st.push(make_pair(z, j + 1));
                        sp = 0; break;
                    }
                }
                if(j == s){
                    x = st.top().first;
                    selv[mList[x].y] = 0;
                    sp = st.top().second; st.pop();
                }
            }
        }
        if(!st.empty()){
            while(!st.empty()){
                x = st.top().first; st.pop();
                u[mList[x].x] = v[mList[x].y] = 1;
                selu[mList[x].x] = selv[mList[x].y] = 0;
                if(!mList[x].matched){
                    M[mList[x].x] = mList[x].y; mList[x].matched = 1;
                }
            }
            ctm++;
        }
    }
    fout << ctm << "\n";
    for(i = 1; i <= 10000; i++){
        if(M[i] != 0) fout << i << " " << M[i] << "\n";
    }
    return 0;
}