Cod sursa(job #2593490)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 4 aprilie 2020 00:15:53
Problema Andrei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <bits/stdc++.h>
using namespace std;

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

// Începe să-mi placă mai mult CodeForces decât InfoArena.
// Acolo nu iei MLE pentru simplul fapt că folosești STL sau OOP...
const int NMAX = 2e5 + 1;

int n, m;
vector<int> adG[NMAX], adT[NMAX];

int top, topo[NMAX];
bool vis[NMAX], ans[NMAX];

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

void addProp(int x, int y) {
    if (x < 0) x = n - x;
    if (y < 0) y = n - y;
    adG[non(x)].push_back(y);
    adT[y].push_back(non(x));
    adG[non(y)].push_back(x);
    adT[x].push_back(non(y));
}

void dfsG(int node) {
    vis[node] = true;
    for (int nghb : adG[node])
        if (!vis[nghb])
            dfsG(nghb);
    topo[top++] = node;
}

void dfsT(int node) {
    vis[node] = false;
    ans[non(node)] = true;
    for (int nghb : adT[node])
        if (vis[nghb])
            dfsT(nghb);
}

int main() {
    fin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y, t; fin >> x >> y >> t;
        if (t == 0)
            addProp(+x, +y);
        else if (t == 1)
            addProp(-x, -y);
        else {
            addProp(-x, +y);
            addProp(+x, -y);
        }
    }

    for (int i = 1; i <= 2 * n; i++)
        if (!vis[i])
            dfsG(i);
    for (int i = 2 * n - 1; i >= 0; i--)
        if (vis[topo[i]] && vis[non(topo[i])])
            dfsT(topo[i]);

    for (int i = 1; i <= n; i++)
        fout << ans[i] << ' ';
    fout << '\n';

    fout.close();
    return 0;
}