Cod sursa(job #1105559)

Utilizator poptibiPop Tiberiu poptibi Data 11 februarie 2014 21:14:13
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

const int NMAX = 200010;

int N, M, X, Y, C, TopSort[NMAX], Val[NMAX];
bool Used[NMAX];
vector<int> G[NMAX], GT[NMAX];

int Neg(int X)
{
    if (X <= N) return X + N;
    else return X - N;
}

void DFP(int Node)
{
    Used[Node] = 1;
    for (vector<int> :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it)
        if (!Used[*it])
            DFP(*it);
    TopSort[ ++ TopSort[0]] = Node;
}

void DFM(int Node)
{
    Val[ Neg(Node) ] = 1;
    Used[Node] = 0;
    for (vector<int> :: iterator it = GT[Node].begin(); it != GT[Node].end(); ++ it)
        if (Used[*it])
            DFM(*it);
}

void Add_Edges(int X, int Y)
{
    G[Neg(X)].push_back(Y);
    G[Neg(Y)].push_back(X);
    GT[X].push_back(Neg(Y));
    GT[Y].push_back(Neg(X));
}

int main()
{
    freopen("andrei.in", "r", stdin);
    freopen("andrei.out", "w", stdout);

    scanf("%i %i", &N, &M);
    for (int i = 1; i <= M; ++ i)
    {
        scanf("%i %i %i", &X, &Y, &C);
        if (C == 0) Add_Edges( X, Y);
        else if (C == 1) Add_Edges( Neg(X), Neg(Y) );
        else
        {
            Add_Edges( X, Neg(Y) );
            Add_Edges( Neg(X), Y );
        }
    }

    for (int i = 1; i <= 2 * N; ++ i)
        if (!Used[i])
            DFP(i);

    reverse(TopSort + 1, TopSort + TopSort[0] + 1);

    for (int i = 1; i <= 2 * N; ++ i)
        if (Used[ TopSort[i] ] && Used[ Neg(TopSort[i]) ])
            DFM(TopSort[i]);

    for (int i = 1; i <= N; ++ i) printf("%i ", Val[i]);
}