Cod sursa(job #3310115)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 11 septembrie 2025 18:37:20
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <bits/stdc++.h>
#define cin ci
#define cout co
using namespace std;
ifstream cin("andrei.in");
ofstream cout("andrei.out");
int n, m, x, y, notx, noty, cmp, c;
vector<vector<int>> graf, graft;
vector<int> ord, viz, comp;
void dfs(int node)
{
    viz[node] = 1;
    for(auto next:graf[node])
        if(!viz[next])
            dfs(next);
    ord.push_back(node);
}
void dfst(int node)
{
    comp[node] = cmp;
    viz[node] = 1;
    for(auto next:graft[node])
        if(!viz[next])
            dfst(next);
}
int main()
{
    cin >> n >> m;
    graf.assign(2 * n + 1, vector<int>());
    graft.assign(2 * n + 1, vector<int>());
    viz.resize(2 * n + 1);
    comp.resize(2 * n + 1);
    while(m--)
    {
        cin >> x >> y >> c;
        notx = n + x;
        noty = n + y;
        if(c == 0)
        {
            graf[x].push_back(noty);
            graf[y].push_back(notx);
            graft[noty].push_back(x);
            graft[notx].push_back(y);
        }
        if(c == 1)
        {
            graf[notx].push_back(y);
            graf[noty].push_back(x);
            graft[y].push_back(notx);
            graft[x].push_back(noty);
        }
        if(c == 2)
        {
            graf[x].push_back(y);
            graf[noty].push_back(notx);
            graf[notx].push_back(noty);
            graf[y].push_back(x);
            graft[y].push_back(x);
            graft[x].push_back(y);
            graft[noty].push_back(notx);
            graft[notx].push_back(noty);
        }
    }
    for(int i=1; i<=2*n; i++)
        if(!viz[i])
            dfs(i);
    fill(viz.begin(), viz.end(), 0);
    reverse(ord.begin(), ord.end());
    for(auto i: ord)
        if(!viz[i])
    {
        cmp++;
        dfst(i);
    }
    for(int i=1; i<=n; i++)
        if(comp[i] > comp[n + i])
            cout << 1 << " ";
        else
            cout << 0 << " ";
    return 0;
}