Cod sursa(job #2357523)

Utilizator Daria09Florea Daria Daria09 Data 27 februarie 2019 15:14:34
Problema Strazi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>
#define dim 256
using namespace std;
ifstream f("strazi.in");
ofstream g("strazi.out");
int Left[dim],Right[dim];
bool viz[dim];
int n,m,edge;
vector <int> graph[dim];
vector <vector <int > > drum;
void Read()
{
    f>>n>>m;
    for(int i=1; i<=m; ++i)
    {
        int x,y;
        f>>x>>y;
        graph[x].push_back(y);
    }
}
bool Match(int node)
{
    if(viz[node])return false;
    viz[node]=1;
    for(int i=0; i<graph[node].size(); ++i)
    {
        int son=graph[node][i];
        if(!Right[son])
        {
            Right[son]=node;
            Left[node]=son;
            return true;
        }
    }
    for(int i=0; i<graph[node].size(); ++i)
    {
        int son=graph[node][i];
        if(Match(Right[son]))
        {
            Right[son]=node;
            Left[node]=son;
            return true;
        }
    }
    return false;
}
void Cuplaj()
{
    bool okay;
    do
    {
        for(int i=1; i<=n; ++i)
            viz[i]=0;
        okay=false;
        for(int i=1; i<=n; ++i)
            if(!Left[i])
                okay|=Match(i);
    }
    while(okay);
}
void Solve()
{
    vector <int> path;
    for(int i=1; i<=n; ++i)
        if(!Right[i])
        {
            path.clear();
            int x=i;
            while(Left[x]!=0)
            {
                path.push_back(x);
                x=Left[x];
            }
            path.push_back(x);
            drum.push_back(path);
            ++edge;
        }
}
void Write()
{
    g<<edge-1<<'\n';
    for(int i=0; i<drum.size()-1; ++i)
        g<<drum[i][drum[i].size()-1]<<" "<<drum[i+1][0]<<'\n';
    for(int i=0; i<drum.size(); ++i)
        for(int j=0; j<drum[i].size(); ++j)
            g<<drum[i][j]<<" ";
}
int main()
{
    Read();
    Cuplaj();
    Solve();
    Write();
    return 0;
}