Cod sursa(job #2332838)

Utilizator gabiluciuLuciu Gabriel gabiluciu Data 31 ianuarie 2019 12:27:29
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.25 kb
//*
//ID: gabriel100
//LANG: C++
//TASK:
//*/
#include <cstdio>
#include <algorithm>
//#include <time.h>
#include <queue>
#include <cmath>
#include <stack>
#include <fstream>
#include <bitset>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <cstring>
//#include <iostream>

#define nl '\n'
#define F first
#define S second
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define eb(x) emplace_back(x)
#define ull unsigned long long
#define ll long long
#define ProblemName "party"
#define LocalName "data"
#ifdef INFOARENA
#define Filename ProblemName
#else
#define Filename LocalName
#endif
#define Input Filename".in"
#define Output Filename".out"
using namespace std;
ifstream cin(Input);
ofstream cout(Output);

template<class a, class type>
void print(a v, type t) {
    for_each(all(v), [](type x) { cout << x << ' '; });
    cout << nl;
}

struct nod {
    vi v1;
    vi v2; // invers
};
#define N 207
int n, m;
nod v[2 * N + 1];
stack<int> s;
bitset<2*N> viz;
vector<vector<int> > componente;
int nrP;
inline void add(const int &ind,const int &next){
    v[ind].v1.push_back(next);
    v[next].v2.push_back(ind);
}
void dfs(int i){
    viz[i] = 1;
    for(auto it = v[i].v1.begin();it!=v[i].v1.end();++it){
        if(!viz[*it])
            dfs(*it);
    }
    s.push(i);
}
int nrGr;
void dfs_inv(int i){
    viz[i] = 1;
    componente[nrGr].eb(i);
    for(auto it = v[i].v2.begin();it!=v[i].v2.end();++it){
        if(!viz[*it])
            dfs_inv(*it);
    }
}
inline int negat(int nr){
    return (nr<=n?nr+n:nr-n);
}
int main() {
    ios_base::sync_with_stdio(false);
    clock_t tStart = clock();
    cin >> n >> m;
    int x1,x2,cod;
    for(int i=0;i<m;++i){
        cin >> x1 >> x2 >> cod;
        if(!cod){
            add(negat(x1),x2);
            add(negat(x2),x1);
        } else if(cod == 1){
            add(negat(x1),negat(x2));
            add(x2,x1);
        } else if(cod == 2) {
            add(x1,x2);
            add(negat(x2),negat(x1));
        } else if (cod == 3){
            add(x1,negat(x2));
            add(x2,negat(x1));
        }
    }
    for(int i=1;i<=2*n;++i){
        if(!viz[i])
            dfs(i);
    }
    viz.reset();
    vi temp;
    componente.push_back(temp);
    while(!s.empty()){
        if(!viz[s.top()]){
            componente.push_back(temp);
            ++nrGr;
            dfs_inv(s.top());
        }
        s.pop();
    }

    vi sol(2*N,-1);

    for(int i = nrGr;i;--i){
        if(sol[componente[i][0]] == -1){
            for(auto it = componente[i].begin();it != componente[i].end(); ++it){
                sol[*it] = 1;
                sol[negat(*it)] = 0;
//                if(*it<=n){
//                    sol[*it+n] = 0;
//                } else {
//                    sol[*it - n] = 0;
//                }
            }
        }
    }

    for(int i=1;i<=n;++i){
        if(sol[i]<0){
            sol[i] = 1;
        }
        nrP += sol[i];
    }
    cout << nrP << nl;
    for (int i = 1; i <= n; ++i) {
        if (sol[i])
            cout << i << nl;
    }
    cout.close();
    printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}