Cod sursa(job #3240342)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 14 august 2024 11:56:36
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "andrei";

std :: ifstream in (FILENAME + ".in");

std :: ofstream out (FILENAME + ".out");

const int NMAX = 2e5 + 5;

int n;

int m;

int cer;

int x;

int y;

int nx;

int ny;

int cnt;

int comp[NMAX];

std :: vector <int> v[NMAX];

std :: vector <int> t[NMAX];

std :: bitset <NMAX> visited;

std :: vector <int> tout;

void dfs(int nod)
{
    for(int i : v[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    tout.push_back(nod);
}

void dfs1(int nod)
{
    comp[nod] = cnt;

    for(int i : t[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs1(i);
        }
    }
}

int main()
{

    in >> n >> m;

    for(int i = 1; i <= m; i ++)
    {
        in >> x >> y >> cer;

        nx = x + n;

        ny = y + n;

        if(cer == 0)
        {
            v[nx].push_back(y);

            t[y].push_back(nx);

            v[ny].push_back(x);

            t[x].push_back(ny);
        }
        else if(cer == 1)
        {
            v[x].push_back(ny);

            t[ny].push_back(x);

            v[y].push_back(nx);

            t[nx].push_back(y);
        }
        else if(cer == 2)
        {
            v[x].push_back(y);

            t[y].push_back(x);

            v[ny].push_back(nx);

            t[nx].push_back(ny);

            v[nx].push_back(ny);

            t[ny].push_back(nx);

            v[y].push_back(x);

            t[x].push_back(y);
        }
    }

    for(int i = 1; i <= 2 * n; i ++)
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    std :: reverse(tout.begin(), tout.end());

    visited &= 0;

    for(int i : tout)
    {
        if(visited[i] == false)
        {
            cnt ++;

            visited[i] = true;

            dfs1(i);
        }
    }

    for(int i = 1; i <= n; i ++)
    {
        if(comp[i] > comp[i + n])
        {
            out << 0 << " ";
        }
        else
        {
            out << 1 << " ";
        }

    }

    return 0;
}