Cod sursa(job #1254707)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 3 noiembrie 2014 11:16:18
Problema Felinare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <fstream>
#include <vector>
using namespace std;
#define Nmax 8200

int n, C = 0;
vector< int > G[Nmax];
vector< bool > viz(Nmax);
int st[Nmax], dr[Nmax], S[2 * Nmax];

void read() ;
bool ok() ;
bool pair_up(int) ;
void calc(int) ;
void write() ;

int main()
{
    read();
    
    //cuplaj
    while( ok() );
    
    //suport minim
    for(int i = 1; i <= n; ++i) if(dr[i]) S[i] = 1;
    for(int i = 1; i <= n; ++i) if(!dr[i]) calc(i);
    
    write();
    return 0;
}

void read()
{
    int m, a, b;
    
    ifstream fin("felinare.in");
    
    fin >> n >> m;
    for(; m; --m)
    {
        fin >> a >> b;
        G[a].push_back(b);
    }
    
    fin.close();
}

bool ok()
{
    fill(viz.begin(), viz.end(), 0);
    
    int i; bool rez = 0;
    for(i = 1; i <= n; ++i)
        if(!viz[i] && !dr[i])
            if(pair_up(i)) rez = 1, ++C;
    return rez;
}


bool pair_up(int vf)
{
    if(viz[vf]) return 0;
    viz[vf] = 1;
    
    for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
        if(!st[*it])
        {
            st[*it] = vf;
            dr[vf] = *it;
            return 1;
        }
    
    for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
        if(pair_up(st[*it]))
        {
            st[*it] = vf;
            dr[vf] = *it;
            return 1;
        }
        
    return 0;
}


void calc(int vf)
{
    for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
        if(S[(*it) + n] == 0)
        {
            S[ (*it) + n ] = 1;
            S[ st[*it] ] = 0;
            calc(st[*it]);
        }
}

void write()
{
    ofstream fout("felinare.out");
    
    fout << (n << 1) - C << '\n';
    
    for(int i = 1; i <= n; ++i)
    {
        if(S[i] && S[i + n]) fout << 0 << '\n';
        else if(!S[i] && S[i + n]) fout << 1 << '\n';
        else if(S[i] && !S[i + n]) fout << 2 << '\n';
        else fout << 3 << '\n';
    }
    
    fout.close();
}