Cod sursa(job #2351376)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 22 februarie 2019 12:24:20
Problema Felinare Scor 4
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const string file = "felinare";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647;

vector<int> adj[10005];
int n, m, e, pairu[10005], pairv[10005], dist[10005], sol;

bool bfs()
{
    queue<int> q;
    for (int i = 1; i <= n; ++i)
        if(pairu[i] == 0){
            dist[i] = 0;
            q.push(i);
        }else
            dist[i] = inf;
    dist[0] = inf;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(dist[u] < dist[0])
            for (vector<int>::iterator it = adj[u].begin(); it != adj[u].end(); ++it)
                if(dist[pairv[*it]] == inf){
                    dist[pairv[*it]] = dist[u] + 1;
                    q.push(pairv[*it]);
                }
    }
    return (dist[0] != inf);
}

bool dfs(int x)
{
    if(x != 0){
        for (vector<int>::iterator it = adj[x].begin(); it != adj[x].end(); ++it)
            if(dist[pairv[*it]] == dist[x] + 1)
                if(dfs(pairv[*it])){
                    pairv[*it] = x;
                    pairu[x] = *it;
                    return true;
                }
        dist[x] = inf;
        return false;
    }
    return true;
}

int main()
{
    ifstream fin (file+".in");
    ofstream fout (file+".out");
    fin >> n >> m; sol = 2*m;
    for (int i = 1; i <= m; ++i){
        int x, y;
        fin >> x >> y;
        adj[x].push_back(y);
    }
    while(bfs())
        for (int i = 1; i <= n; ++i)
            if(pairu[i] == 0 && dfs(i))
                --sol;
    fout << sol << "\n";
    return 0;
}