Cod sursa(job #2670999)

Utilizator dimi999Dimitriu Andrei dimi999 Data 11 noiembrie 2020 10:15:07
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m;

int NEG(int x)
{
    return x + n;
}

int idx[200005], llink[200005], k, ctc[200005], kk;

stack <int> st;

bool inst[200005];

vector <int> v[200005];

void tarjan(int node)
{
    llink[node] = idx[node] = ++k;

    inst[node] = true;

    st.push(node);

    for(int i = 0; i < v[node].size(); i++)
        if(idx[v[node][i]] == 0)
    {
        tarjan(v[node][i]);

        llink[node] = min(llink[node], llink[v[node][i]]);
    }
    else
        if(inst[v[node][i]] == true)
            llink[node] = min(llink[node], llink[v[node][i]]);

    if(idx[node] == llink[node])
    {
        int naux = st.top();

        inst[naux] = false;

        st.pop();

        ctc[naux] = ++kk;

        while(naux != node)
        {
            naux = st.top();

            inst[naux] = false;

            st.pop();

            ctc[naux] = ++kk;

        }
    }
}


int main()
{
    fin >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y, t;

        fin >> x >> y >> t;

        if(t == 0)
            {
                v[NEG(x)].push_back(y);
                v[NEG(y)].push_back(x);
            }

        if(t == 1)
            {
                v[y].push_back(NEG(x));
                v[x].push_back(NEG(y));
            }

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

    for(int i = 1; i <= 2 * n; i++)
        if(idx[i] == 0)
            tarjan(i);

    for(int i = 1; i <= n; i++)
        if(ctc[i] < ctc[i + n])
        fout << 0 << " ";
    else
        fout << 1 << " ";

    return 0;
}