Cod sursa(job #485314)

Utilizator johnny2008Diaconu Ion johnny2008 Data 17 septembrie 2010 21:40:09
Problema Party Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
ifstream f("party.in");
ofstream g("party.out");
const int NMAX = 1010;
const int MMAX = 10010;
 
int N, M;
int Bl[MMAX][4];
int val[NMAX];
 
void citire()
{
    f>>N>>M;
	for(int i = 1 ; i <= M ; i++)
        f>>Bl[i][1]>>Bl[i][2]>>Bl[i][0];
}
 
bool corect(int rel, int x, int y)
{
    switch(rel)
    {
    case 0: return(x || y);
    case 1: return(x || (!x && !y));
    case 2: return(y || (!y && !x));
    case 3: return(!x || !y);
    }
}
 
bool dif_zero()
{
    for(int i = 1 ; i <= N ; i++)
        if(val[i])
            return 1;
    return 0;
}
 
void rezolva()
{
    int gasit, esuat;
    srand(time(NULL));
    for(int i = 1 ; i <= N ; i++)
        val[i] = rand()%2;
    for(int i = 1 ; i <= 50 * N ; i++)
    {
        gasit = 1; esuat = 1;
        for(int j = 1 ; j <= M ; j++)
            if(!corect(Bl[j][0], val[Bl[j][1]], val[Bl[j][2]]))
            {
                gasit = 0;
                esuat = j;
                break;
            }
       if(gasit == 1 && dif_zero())
            return;
        if(rand() % 2 + 1 == 1)
            val[Bl[esuat][1]] ^= 1;
        else
            val[Bl[esuat][2]] ^= 1;
    }
   
}
 
void scriere()
{
    int NR = 0;
    for(int i = 1 ; i <= N ; i++)
        if(val[i])
            NR++;
    g<<NR<<"\n";
    for(int i = 1 ; i <= N ; i++)
        if(val[i])
            g<<i<<"\n";
}
 
int main()
{
    
    citire();
    rezolva();
    scriere();
    return 0;
}