Cod sursa(job #1122090)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 25 februarie 2014 16:09:32
Problema 2SAT Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

const int Nmax = 100002;
const int Vmax = 100000000;

int sol[Nmax];
pair<int,int> v[2 * Nmax];

int N, M;

inline bool value(int i)
{
	int x=sol[abs(v[i].first)],y=sol[abs(v[i].second)];
	if(v[i].first<0)
		x^=1;
	if(v[i].second<0)
		y^=1;
	return x|y;
}

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;
}