Cod sursa(job #1936783)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 23 martie 2017 13:47:56
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include<fstream>
#include<vector>
#include<stack>
#include<cstring>
#define DIM 200005
using namespace std;
int n, m, nr, i, j, x, y, cnt, t;
int sol[DIM], low[DIM], niv[DIM], f[DIM], viz[DIM], ctc[DIM];
vector<int> v[DIM], w[DIM];
stack<int> s;
ifstream fin("andrei.in");
ofstream fout("andrei.out");
void dfs(int nod){
    viz[nod] = f[nod] = 1;
    s.push(nod);
    cnt++;
    low[nod] = niv[nod] = cnt;
    for(int i = 0; i < v[nod].size(); i++){
        int vecin = v[nod][i];
        if(viz[vecin] == 0){
            dfs(vecin);
        }
        if(f[vecin] == 1){
            low[nod] = min(low[nod], low[vecin]);
        }
    }
    if(low[nod] == niv[nod]){
        int x;
        nr++;
        do{
            x = s.top();
            ctc[x] = nr;
            w[nr].push_back(x);
            f[x] = 0;
            s.pop();
        }while(x != nod);
    }
}
int inv(int x){
    if(x <= n){
        return x + n;
    }
    return x - n;
}
void ff(int x, int y){
    v[ inv(x) ].push_back(y);
    v[ inv(y) ].push_back(x);
}
int main(){
    fin>> n >> m;
    for(i = 1; i <= m; i++){
        fin>> x >> y >> t;
        if(t == 0){
            ff(x, y);
        }
        else{
            if(t == 1){
                ff(inv(x), inv(y));
            }
            else{
                ff(x, y);
                ff(inv(x), inv(y));
            }
        }
    }
    for(i = 1; i <= n + n; i++){
        if(viz[i] == 0){
            dfs(i);
        }
    }
    memset(viz, 0, sizeof(viz));
    for(i = 1; i <= nr; i++){
        if(viz[i] == 1){
            continue;
        }
        for(j = 0; j < w[i].size(); j++){
            x = w[i][j];
            sol[x] = 0;
            sol[ inv(x) ] = 1;
            viz[ ctc[ inv(x) ] ] = 1;
        }
    }
    for(i = 1; i <= n; i++){
        fout<< sol[i] <<" ";
    }
    return 0;
}