Cod sursa(job #3217775)

Utilizator stefan05Vasilache Stefan stefan05 Data 24 martie 2024 17:00:12
Problema Andrei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <fstream>
#include <vector>
#include <stack>

#define NMAX 100005

using namespace std;

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

int n, m;
int a, b, c;
vector<int> l[NMAX*2];
int cc[NMAX*2]; int nr;
int low[NMAX*2], disc[NMAX*2];
int counter;
stack<int> s;
int i;

void dfs(int);

int main()
{
    fin >>n>>m;
    for (i = 1; i <= m; ++i)
    {
        fin >>a>>b>>c;

        //a+n = !A, -a+n = A
        //A=1 inseamna ca nodul apartine multimii A
        if (c == 0)
        {
            l[-a+n].push_back(b+n);
            l[-b+n].push_back(a+n);
            continue;
        }
        if (c == 1)
        {
            l[a+n].push_back(-b+n);
            l[b+n].push_back(-a+n);
            continue;
        }

        l[a+n].push_back(b+n);
        l[-b+n].push_back(-a+n);
        l[-a+n].push_back(-b+n);
        l[b+n].push_back(a+n);
    }

    for (i = 1; i <= n; ++i)
    {
        if (!low[-i+n])
            dfs(-i+n);
        if (!low[i+n])
            dfs(i+n);
    }

    for (i = 1; i <= n; ++i)
        fout <<(cc[-i+n] > cc[i+n])<<' ';
    return 0;
}

void dfs(int vf)
{
    s.push(vf);
    disc[vf] = low[vf] = ++counter;
    for (auto vfnou: l[vf])
    {
        if (!low[vfnou])
            dfs(vfnou);
        if (low[vfnou] > 0)
            low[vf] = min(low[vf], low[vfnou]);
    }

    if (low[vf] == disc[vf])
    {
        nr ++;
        int nod;
        do
        {
            nod = s.top(); s.pop();
            cc[nod] = nr; low[nod] = -1;
        }
        while (nod != vf);
    }
}