Cod sursa(job #3338178)

Utilizator cristi95Plesnicute Cristian-Jovani cristi95 Data 1 februarie 2026 11:14:12
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, e, x, y, cuplat[20001], viz[20001], cnt;

bool ok;

vector <int> v[20001];

void cuplare (int a, int b) {
    cuplat[cuplat[a]] = 0;
    cuplat[a] = b;
    cuplat[cuplat[b]] = 0;
    cuplat[b] = a;
}

int dfs (int nod){
    viz[nod] = cnt;
    for (auto it : v[nod]){
        if (viz[it] != cnt){
            viz[it] = cnt;
            if (!cuplat[it]){
                cuplare(nod, it);
                return 1;
            }
            if (cuplat[it] && dfs(cuplat[it]) == 1){
                cuplare(nod, it);
                return 1;
            }
        }
    }
    return 0;
}



int main()
{
    fin >> n >> m >> e;

    while (e--){
        fin >> x >> y;
        v[x].push_back(y+n);
        v[y+n].push_back(x);
    }

    ok = 1;

    while (ok){
        ok = 0;
        cnt++;
        for (int i = 1; i <= m + n; ++i){
            if (!cuplat[i] && viz[i]){
                ok = ok|dfs(i);
            }
        }
    }

    return 0;
}