Cod sursa(job #467156)

Utilizator DraStiKDragos Oprica DraStiK Data 28 iunie 2010 12:21:22
Problema Andrei Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2010, clasele X-XII, Ziua 2 Marime 2.73 kb
#include <algorithm>
#include <vector>
#include <bitset>
#include <cstdio>
#include <ctime>
using namespace std;

#define pb push_back
#define mp make_pair
#define DIM 100005
#define sc second
#define fs first
#define VIOLET 2
#define ROSU 1
#define ALB 0

pair <pair <int,int>,int> muc[DIM<<1];
vector <pair <int,int> > g[DIM];
bitset <DIM> viz,mt;
double start;
int n,m;

void df_atribA (int nod);
void df_atribB (int nod);

void read ()
{
    int i,x,y,z;

    scanf ("%d%d",&n,&m);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d%d%d",&x,&y,&z);
        muc[i]=mp (mp (x,y),z);
        g[x].pb (mp (y,z));
        g[y].pb (mp (x,z));
    }
}

void df_atribA (int nod)
{
    vector <pair <int,int> > :: iterator it;

    viz[nod]=1;
    for (it=g[nod].begin (); it!=g[nod].end (); ++it)
        if (!viz[it->fs])
        {
            if (it->sc==ALB)
                df_atribB (it->fs);
            else if (it->sc==VIOLET)
                df_atribA (it->fs);
            else if (rand ()%2)
                df_atribA (it->fs);
            else
                df_atribB (it->fs);
        }
}

void df_atribB (int nod)
{
    vector <pair <int,int> > :: iterator it;

    viz[nod]=mt[nod]=1;
    for (it=g[nod].begin (); it!=g[nod].end (); ++it)
        if (!viz[it->fs])
        {
            if (it->sc==ROSU)
                df_atribA (it->fs);
            else if (it->sc==VIOLET)
                df_atribB (it->fs);
            else if (rand ()%2)
                df_atribB (it->fs);
            else
                df_atribA (it->fs);
        }
}

inline int valid ()
{
    int i;

    for (i=1; i<=m; ++i)
    {
        if (muc[i].sc==ALB && !mt[muc[i].fs.fs] && !mt[muc[i].fs.sc])
            return 0;
        if (muc[i].sc==ROSU && mt[muc[i].fs.fs] && mt[muc[i].fs.sc])
            return 0;
        if (muc[i].sc==VIOLET && !mt[muc[i].fs.fs] && mt[muc[i].fs.sc])
            return 0;
        if (muc[i].sc==VIOLET && mt[muc[i].fs.fs] && !mt[muc[i].fs.sc])
            return 0;
    }
    return 1;
}

void solve ()
{
    int i;

    do
    {
        viz.reset ();
        for (i=1; i<=n; ++i)
            if (!viz[i])
                if (rand ()%2)
                    df_atribA (i);
                else
                    df_atribB (i);
    }
    while ((clock ()-start)/CLOCKS_PER_SEC<1 && !valid ());
}

void print ()
{
    int i;

    for (i=1; i<=n; ++i)
        if (!mt[i])
            printf ("0 ");
        else
            printf ("1 ");
}

int main ()
{
    start=clock ();
    srand (time (0));
    freopen ("andrei.in","r",stdin);
    freopen ("andrei.out","w",stdout);

    read ();
    solve ();
    print ();

    return 0;
}