Cod sursa(job #1741391)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 13 august 2016 19:59:19
Problema Andrei Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <queue>
#include <cstring>
#include <algorithm>
#define MAXN 100010
using namespace std;

vector< int > g[2 * MAXN], gt[2 * MAXN], order;
int n, components = 0;
int component[MAXN];
bool seen[MAXN];

int Not(int a) {
    if (a <= n)
        return a + n;
    return a - n;
}

void AddEdge(int a, int b) {
    g[Not(a)].push_back(b);
    g[Not(b)].push_back(a);
    gt[a].push_back(Not(b));
    gt[b].push_back(Not(a));
}

void FirstDFS(int node) {
    seen[node] = true;
    for (auto &it : g[node])
        if(!seen[it])
            FirstDFS(it);
    order.push_back(node);
}

void SecondDFS(int node) {
    seen[node] = false;
    component[node] = components;
    for (auto &it : gt[node])
        if(seen[it])
            SecondDFS(it);
}

void StronglyConnectedComponents() {
    for (int i = 1; i <= 2 * n; i++)
        if (!seen[i])
            FirstDFS(i);
    for (int i = order.size() - 1; i >= 0; i--)
        if (seen[order[i]]) {
            components++;
            SecondDFS(order[i]);
        }
}

int main() {
    ifstream cin("andrei.in");
    ofstream cout("andrei.out");
    int m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        if (c == 0)
            AddEdge(a, b);
        if (c == 1)
            AddEdge(Not(a), Not(b));
        if (c == 2) {
            AddEdge(Not(a), b);
            AddEdge(Not(b), a);
        }
    }
    StronglyConnectedComponents();
    for (int i = 1; i <= n; i++)
        if (component[i] < component[Not(i)])
            cout << "0 ";
        else
            cout << "1 ";
    return 0;
}