Cod sursa(job #1655844)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 18 martie 2016 13:34:07
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>

using namespace std;

ifstream in("andrei.in");
ofstream out("andrei.out");

#define N 100001
#define M 200001

int n, m, dublun;
int lst[2 * N], vf[8 * M], urm[8 * M], nvf = 0;
bool ok1[2 * N];
int stiva[2 * N], st = 0;
bool ok2[2 * N];
bool ok3[2 * N];

inline void adaugaMuchie(int x, int y)
{
    vf[++nvf] = y;
    urm[nvf] = lst[x];
    lst[x] = nvf;
    vf[++nvf] = x;
    urm[nvf] = lst[y];
    lst[y] = nvf;
}

void dfs1(int x)
{
    ok1[x] = 1;
    for(int i = lst[x]; i; i = urm[i])
        if(i % 2 && !ok1[vf[i]])
            dfs1(vf[i]);
    stiva[++st] = x;
}

void dfs2(int x)
{
    ok2[x] = 1;
    ok3[dublun - x] = 1;
    for(int i = lst[x]; i; i = urm[i])
        if(i % 2 == 0 && !ok2[vf[i]])
            dfs2(vf[i]);
}

int main()
{
    in >> n >> m;
    dublun = 2 * n + 1;
    while(m--)
    {
        int x, y, c;
        in >> x >> y >> c;
        int notx = dublun - x, noty = dublun - y;
        if(c == 0)
        {
            adaugaMuchie(notx, y);
            adaugaMuchie(noty, x);
        }
        else if(c == 1)
        {
            adaugaMuchie(x, noty);
            adaugaMuchie(y, notx);
        }
        else
        {
            adaugaMuchie(x, y);
            adaugaMuchie(y, x);
            adaugaMuchie(noty, notx);
            adaugaMuchie(notx, noty);
        }
    }

    for(int i = 1; i < dublun; i++)
        if(!ok1[i])
            dfs1(i);

    for(int i = st; i >= 1; i--)
        if(!ok2[stiva[i]] && !ok2[dublun - stiva[i]])
            dfs2(stiva[i]);

    for(int i = 1; i <= n; i++)
        out << ok3[i] << ' ';
    out << '\n';
    return 0;
}