Cod sursa(job #2380586)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 15 martie 2019 10:55:01
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 100 + 5;
const int MMAX = 1000 + 5;

int N, M;
int x[2 * NMAX];
int a[MMAX], b[MMAX];

int main() {
    ifstream cin("party.in");
    ofstream cout("party.out");
    cin >> N >> M;
    for (int i = 1; i <= M; ++i) {
        cin >> a[i] >> b[i];
        --a[i], a[i] *= 2;
        --b[i], b[i] *= 2;
        int type;
        cin >> type;
        if (type == 1 || type == 3)
            b[i] ^= 1;
        if (type == 2 || type == 3)
            a[i] ^= 1;
    }
    for (int i = 1; i < 2 * N; i += 2)
        x[i] = true;
    // WalkSAT Algorithm
    while (true) {
        vector <int> clauses;
        for (int i = 1; i <= M; ++i)
            if (!(x[a[i]] || x[b[i]]))
                clauses.push_back(i);
        if (clauses.empty())
            break;
        const int where = clauses[rand() % clauses.size()];
        const int pos = rand() % 2 ? a[where] : b[where];
        x[pos] ^= 1, x[pos ^ 1] ^= 1;
    }
    vector <int> sol;
    for (int i = 0; i < 2 * N; i += 2)
        if (x[i])
            sol.push_back(i / 2 + 1);
    cout << sol.size() << endl;
    for (int i = 0; i < sol.size(); ++i)
        cout << sol[i] << '\n';
    return 0;
}