Pagini recente » Cod sursa (job #431609) | Cod sursa (job #2862380) | Cod sursa (job #1174311) | Cod sursa (job #1997339) | Cod sursa (job #485314)
Cod sursa(job #485314)
#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;
}