Cod sursa(job #2034267)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 7 octombrie 2017 17:41:57
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

vector <pair<int,int> > graf[100001] ;
bool viz[100001] ;
bool t[100001] ;
int n , m ;

void citire ()
{
    int i , x , y , c ;
    ifstream fin ("andrei.in") ;
    fin >> n >> m ;
    memset(viz,false,n+1) ;
    memset(t,0,n+1) ;
    for ( i = 1 ; i <= m ; i++ )
    {
        fin >> x >> y >> c ;
        graf[x].push_back(make_pair(y,c)) ;
        graf[y].push_back(make_pair(x,c)) ;
    }
}

void arcu ( int x , int y )
{
    //cout << " intru in arcu cu marcu " << x << " " << y << " " << graf[x][y].second ;
    if ( graf[x][y].second == 0 || graf[x][y].second == 1 )
    {
        if ( t[x] == 0 )
            t[graf[x][y].first] = 1 ;
        else
            t[graf[x][y].first] = 0 ;
    }
    else if ( graf[x][y].second == 2 )
        t[graf[x][y].first] = t[x] ;
    //cout << " am pus in el " << t[graf[x][y].first] << endl ;
}

void DFS ( int nod )
{
    //cout << " intru in DFS " << nod << endl ;
    int i ;
    viz[nod] = true ;
    for ( i = 0 ; i < graf[nod].size() ; i++ )
    {
        if ( viz[graf[nod][i].first] == false )
        {
            arcu(nod,i) ;
            //cout << " aiciiicii " ;
            DFS(graf[nod][i].first) ;
        }
    }
}

int main()
{
    ofstream fout ("andrei.out") ;
    citire() ;
    for ( int i = 1 ; i <= n ; i++ )
    {
        if ( viz[i] == false )
        {
            t[i] = 0 ;
            DFS(i) ;
        }
    }
    for ( int i = 1 ; i <= n ; i++ )
        fout << t[i] << " " ;
}