Cod sursa(job #3241486)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 30 august 2024 18:38:08
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "andrei";

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

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

const int NMAX = 2e5;

int n;

int m;

short cer;

int x;

int y;

int nx;

int ny;

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

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

std :: bitset <NMAX> visited;

std :: vector <int> tout;

std :: bitset <NMAX> ans;

std :: stack <int> s;

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)
{
    s.push(nod);

    while(!s.empty())
    {
        int nod = s.top();

        s.pop();

        ans[nod] = 0;

        ans[(nod >= n) ? (nod - n) : (nod + n)] = 1;

        visited[nod] = false;

        visited[(nod >= n) ? (nod - n) : (nod + n)] = false;

        for(int & i : v[nod])
        {
            if(visited[i] == true)
            {
                s.push(i);
            }
        }
    }
}

int main()
{

    in >> n >> m;

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

        x --;

        y --;

        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 = 0; i < 2 * n; i ++)
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

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

    for(int & i : tout)
    {
        if(visited[i] == true && visited[(i >= n) ? (i - n) : (i + n)] == true)
        {
            dfs1(i);
        }
    }

    for(int i = 0; i < n; i ++)
    {
        out << ans[i] << " ";
    }

    return 0;
}