Cod sursa(job #1940899)

Utilizator robx12lnLinca Robert robx12ln Data 26 martie 2017 21:09:56
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include<fstream>
#include<stack>
#include<vector>
#define DIM 100005
using namespace std;
ifstream fin("andrei.in");
ofstream fout("andrei.out");
int n, m, x, y, c, val[DIM], niv[DIM], low[DIM], cod, f[DIM], nr;
vector<int> v[DIM];
stack<int> st;
void dfs( int nod ){
    cod++;
    niv[nod] = low[nod] = cod;
    f[nod] = 1;
    st.push( nod );
    for( int i = 0; i < v[nod].size(); i++ ){
        int vecin = v[nod][i];
        if( niv[vecin] == 0 ){
            dfs( vecin );
            low[nod] = min( low[nod], low[vecin] );
        }else{
            if( f[vecin] == 1 ){
                low[nod] = min( low[nod], low[vecin] );
            }
        }
    }
    if( niv[nod] == low[nod] ){
        nr++;
        while( st.top() != nod ){
            int x = st.top();
            f[x] = 0;
            if( val[x] == 0 ){
                val[x] = 1;
                if( x <= n ){
                    val[x + n] = -1;
                }else{
                    val[x - n] = -1;
                }
            }
            st.pop();
        }
        int x = st.top();
        f[x] = 0;
        if( val[x] == 0 ){
            val[x] = 1;
            if( x <= n ){
                val[x + n] = -1;
            }else{
                val[x - n] = -1;
            }
        }
        st.pop();
    }
    return;
}
int main(){
    fin >> n >> m;
    for( int i = 1; i <= n; i++ ){
        fin >> x >> y >> c;
        if( c == 0 ){
            v[x + n].push_back(y);
            v[y + n].push_back(x);
            continue;
        }
        if( c == 1 ){
            v[x].push_back(y + n);
            v[y].push_back(x + n);
            continue;
        }
        v[x].push_back(y);
        v[y].push_back(x);
        v[x + n].push_back(y + n);
        v[y + n].push_back(x + n);
    }
    cod = 0;
    for( int i = 1; i <= n * 2; i++ ){
        if( niv[i] == 0 ){
            dfs( i );
        }
    }
    for( int i = 1; i <= n; i++ ){
        if( val[i] == 1 ){
            fout << "0 ";
        }else{
            fout << "1 ";
        }
    }
    return 0;
}