Pagini recente » Cod sursa (job #744909) | Cod sursa (job #169919) | Cod sursa (job #1035568) | Cod sursa (job #244570) | Cod sursa (job #2087639)
#include <bits/stdc++.h>
#define NMAX 200003
using namespace std;
ifstream fin ("andrei.in");
ofstream fout("andrei.out") ;
vector<int> graf[NMAX] ;
vector<int> GFT[NMAX] ;
vector<int> stiva ;
int n , m ;
int t[2*NMAX] ;
bool viz[2*NMAX] ;
int sol[2*NMAX];
void citire()
{
int x , y , c;
fin >> n >> m ;
for ( int i = 1 ; i <= m ; i++ )
{
fin >> x >> y >> c ;
if ( c == 0 )
{
graf[x].push_back(y+n) ;
graf[y].push_back(x+n) ;
GFT[y+n].push_back(x);
GFT[x+n].push_back(y) ;
}
else if ( c == 1 )
{
graf[x+n].push_back(y);
graf[y+n].push_back(x);
GFT[y].push_back(x+n);
GFT[x].push_back(y+n) ;
}
else if ( c == 2 )
{
graf[x].push_back(y);
graf[y+n].push_back(x+n) ;
GFT[y].push_back(x);
GFT[x+n].push_back(y+n) ;
}
}
}
void DFS ( int nod )
{
viz[nod] = true ;
for ( int i = 0 ; i < graf[nod].size() ; i++ )
{
if ( viz[graf[nod][i]] == false )
{
DFS(graf[nod][i]) ;
}
}
stiva.push_back(nod) ;
}
void DFST ( int nod , int ct)
{
viz[nod] = false ;
for ( int i = 0 ; i < graf[nod].size() ; i++ )
{
if ( viz[GFT[nod][i]] == true )
{
DFST(GFT[nod][i],ct) ;
}
}
t[nod] = ct ;
}
int main()
{
int ct = 0 ;
citire();
memset(viz,false,2*n) ;
for ( int j = 1 ; j <= 2*n ; j++ )
if ( !viz[j] )
DFS(j) ;
for ( int j = 2 * n ; j >= 1 ; j-- )
{
if ( viz[j] )
{
DFST(j,ct) ;
ct++;
}
}
for ( int i = 0 ; i < stiva.size() ; i++ )
{
if ( stiva[i] <= n )
{
if ( t[stiva[i]] > t[stiva[i]+n] )
sol[stiva[i]] = 0 ;
else
sol[stiva[i]] = 1 ;
}
}
for ( int i = 1 ; i <= n ; i++ )
fout << sol[i] << " ";
}