Pagini recente » Cod sursa (job #1778723) | Cod sursa (job #90104) | Cod sursa (job #347911) | Cod sursa (job #1831526) | Cod sursa (job #1122075)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
const int Nmax = 100002;
const int Vmax = 1000000;
int sol[Nmax];
pair<int,int> v[Nmax];
int N, M;
int value( int i )
{
return ( sol[ abs( v[i].first ) ] | sol[ abs( v[i].second ) ] );
}
int main()
{
ifstream f("2sat.in");
ofstream g("2sat.out");
f >> N >> M;
for ( int i = 1; i <= M; ++i )
{
f >> v[i].first >> v[i].second;
}
srand( time( NULL ) );
for ( int i = 1; i <= N; ++i )
sol[i] = rand() % 2;
for ( int i = 1; i <= Vmax; ++i )
{
for ( int j = 1; j <= M; ++j )
{
if ( value( j ) == 0 )
{
if ( ( rand() % 2 ) % 2 == 0 )
sol[ abs( v[j].first ) ] ^= 1;
else
sol[ abs( v[j].second ) ] ^= 1;
break;
}
if ( j == M )
{
for ( int i = 1; i <= N; ++i )
{
g << sol[i] << " ";
}
return 0;
}
}
}
g << "-1";
return 0;
}