Cod sursa(job #2909053)

Utilizator rares89_Dumitriu Rares rares89_ Data 8 iunie 2022 15:12:30
Problema Andrei Scor 75
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

int cnt, ctc[200005], st[200005], nr;
vector<int> G[200005], GT[200005];
bitset<200005> v;

void adaugare(int a, int b) {
    G[a].push_back(b);
    GT[b].push_back(a);
}

void dfs(int nod) {
    v[nod] = true;
    for(auto i : G[nod]) {
        if(!v[i]) {
            dfs(i);
        }
    }
    st[++nr] = nod;
}

void dfsT(int nod) {
    v[nod] = true;
    ctc[nod] = cnt;
    for(auto i : GT[nod]) {
        if(!v[i]) {
            dfsT(i);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    int n, m, x, y, z;
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        fin >> x >> y >> z;
        if(z == 0) {
            adaugare(x, y + n);
            adaugare(y, x + n);
        }
        if(z == 1) {
            adaugare(x + n, y);
            adaugare(y + n, x);
        }
        if(z == 2) {
            adaugare(x, y);
            adaugare(y, x);
            adaugare(x + n, y + n);
            adaugare(y + n, x + n);
        }
    }
    for(int i = 1; i <= 2 * n; i++) {
        if(!v[i]) {
            dfs(i);
        }
    }
    for(int i = 1; i <= 2 * n; i++) {
        v[i] = false;
    }
    for(int i = 2 * n; i >= 1; i--) {
        if(!v[st[i]]) {
            cnt++;
            dfsT(st[i]);
        }
    }
    for(int i = 1; i <= n; i++) {
        fout << (ctc[i] < ctc[i + n]) << " ";
    }
    return 0;
}