Cod sursa(job #3217743)

Utilizator stefan05Vasilache Stefan stefan05 Data 24 martie 2024 15:03:40
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>
#include <vector>

#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];
vector<int> lt[NMAX*2];
vector<int> postord;
int cc[NMAX*2]; int nr;
bool f[NMAX*2];
int i;

void dfs(int);
void dfst(int);

int main()
{
    fin >>n>>m;
    for (i = 1; i <= m; ++i)
    {
        fin >>a>>b>>c;
        if (c == 0)
        {
            l[a+n].push_back(-b+n);
            l[b+n].push_back(-a+n);

            lt[-b+n].push_back(a+n);
            lt[-a+n].push_back(b+n);
            continue;
        }
        if (c == 1)
        {
            l[-a+n].push_back(b+n);
            l[-b+n].push_back(a+n);

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

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

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

    for (i = 0; i <= 2*n; ++i)
        if (!f[i])
            dfs(i);

    for (i = postord.size()-1; i >= 0; --i)
        if (f[postord[i]])
        {
            nr ++;
            dfst(postord[i]);
        }

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

void dfst(int vf)
{
    f[vf] = 0; cc[vf] = nr;
    for (auto vfnou: lt[vf])
        if (f[vfnou])
            dfst(vfnou);
}

void dfs(int vf)
{
    f[vf] = 1;
    for (auto vfnou: l[vf])
        if (!f[vfnou])
            dfs(vfnou);

    postord.push_back(vf);
}