Pagini recente » Cod sursa (job #162777) | Cod sursa (job #2959680) | Cod sursa (job #2709904) | Cod sursa (job #3003385) | Cod sursa (job #177893)
Cod sursa(job #177893)
#include <fstream>
#include <utility>
#include <stdlib.h>
#include <vector>
#include <ctime>
using namespace std;
#define maxn 2001
#define FOR(i,a,b) for (i = (a); i <= (b); i++)
#define sz size()
#define pb push_back
#define mp make_pair
#define s second
#define f first
int N, M, bad;
int v[maxn], c[maxn], b[maxn];
vector<pair<int, int> > exp;
int eval(int pos)
{
pair<int, int> e = exp[pos];
if (c[pos] == 0)
return (v[e.f] || v[e.s]);
if (c[pos] == 1)
return (v[e.f] || !v[e.s]);
if (c[pos] == 2)
return (!v[e.f] || v[e.s]);
return (!v[e.f] || !v[e.s]);
}
int extra()
{
int i;
FOR(i, 1, N)
if (v[i]) return 1;
return 0;
}
int main()
{
int i, x, y, pas;
ifstream fin("party.in");
ofstream fout("party.out");
srand(time(NULL));
fin >> N >> M;
FOR(i, 0, M - 1)
{
fin >> x >> y >> c[i];
exp.pb(mp(x, y));
}
/* FOR(i, 0, M - 1)
fout << exp[i].f << " " << exp[i].s << " " << c[i] << '\n';*/
FOR(i, 1, N) v[i] = (i % 2);
FOR(pas, 0, 100000)
{
bad = 0;
int ok = eval(0);
if (!eval(0)) b[++bad] = 0;
FOR(i, 1, M - 1)
{
ok = ok && eval(i);
if (!eval(i)) b[++bad] = i;
}
if (ok && extra()) break;
if (!bad) b[++bad] = rand() % N + 1;
//fout << bad << "\n";
int nr = b[rand() % bad + 1], nr2 = rand() % 2;
if (nr2)
v[exp[nr].f] = !v[exp[nr].f];
else
v[exp[nr].s] = !v[exp[nr].s];
}
int res = 0;
for (i = 1; i <= N; i++)
if (v[i]) res++;
fout << res << '\n';
FOR(i, 1, N)
if (v[i]) fout << i << '\n';
fin.close(), fout.close();
}