Cod sursa(job #2942407)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 19 noiembrie 2022 17:30:30
Problema Andrei Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin ("andrei.in");
ofstream cout ("andrei.out");

const int N = 2e5;
int ctc[N + 1];

bool viz[N + 1];

vector <int> g[N + 1], gb[N + 1], v;

int n, m, cer, x, y, k;

int neg (int x)
{
    return (x > n) ? x - n : x + n;
}

void dfsplus (int node)
{
    viz[node] = 1;
    for (auto it : g[node])
        if (!viz[it])
            dfsplus(it);
    v.push_back(node);
}

void dfsminus (int node)
{
    ctc[node] = k;
    for (auto it : gb[node])
        if (!ctc[it])
            dfsminus(it);
}

int main()
{
    for (cin >> n >> m; m && cin >> x >> y >> cer; --m)
    {
        if (cer == 0)
        {
            g[x].push_back(neg(y));
            g[y].push_back(neg(x));
            g[neg(y)].push_back(x);
            g[neg(x)].push_back(y);
        }
        else if (cer == 1)
        {
            g[neg(x)].push_back(y);
            g[neg(y)].push_back(x);
            gb[y].push_back(neg(x));
            gb[x].push_back(neg(y));
        }
        else
        {
            g[x].push_back(y);
            g[neg(x)].push_back(neg(y));
            gb[y].push_back(x);
            gb[neg(y)].push_back(neg(x));
            gb[x].push_back(y);
            gb[neg(x)].push_back(neg(y));
            g[y].push_back(x);
            g[neg(y)].push_back(neg(x));
        }
    }

    for (int i = 1; i <= (n << 1); ++i)
        if (!viz[i])
            dfsplus(i);
    reverse (v.begin(), v.end());
    for (auto it : v)
        if (!ctc[it])
            ++k, dfsminus (it);
    for (int i = 1; i <= n; ++i)
        cout << (ctc[i] < ctc[i + n]) << ' ';
    return 0;
}