Cod sursa(job #1205016)

Utilizator andreiiiiPopa Andrei andreiiii Data 4 iulie 2014 18:31:52
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <vector>

using namespace std;

const int Nmax = 100005;

vector<int> G[2 * Nmax], Gt[2 * Nmax];
bitset<2 * Nmax> V;

int SortedNodes[2 * Nmax], Cc[2 * Nmax];
int N, Ncc;

inline int Non(const int x)
{
    return x > N ? x - N: x + N;
}

void add_edge(const int x, const int y)
{
    G[Non(x)].push_back(y);
    G[Non(y)].push_back(x);

    Gt[y].push_back(Non(x));
    Gt[x].push_back(Non(y));
}

void Dfs(const int node)
{
    V[node] = 1;

    for (const int next: G[node])
        if (!V[next])
            Dfs(next);

    SortedNodes[++SortedNodes[0]] = node;
}

void Dfst(const int node)
{
    Cc[node] = Ncc;
    for (const int next: Gt[node])
        if (!Cc[next])
            Dfst(next);
}

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

    int M;
    scanf("%d%d", &N, &M);

    while (M--)
    {
        int x, y, type;
        scanf("%d%d%d", &x, &y, &type);

        if (type == 0) add_edge(Non(x), Non(y));
        else if (type == 1) add_edge(x, y);
        else add_edge(x, Non(y)), add_edge(Non(x), y);
    }

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

    for (int i = 2 * N; i; i--)
    {
        const int node = SortedNodes[i];

        if (!Cc[node])
        {
            Ncc++;
            Dfst(node);
        }
    }

    for (int i = 1; i <= N; i++)
    {
        if (Cc[i] > Cc[Non(i)])
            printf("0\n");
        else
            printf("1\n");
    }
}