Cod sursa(job #2595145)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 7 aprilie 2020 11:33:36
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.79 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

ifstream fin("party.in");
ofstream fout("party.out");

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int DMAX = 1e5+10;

class _2sat{
    private:
        int n;
        vector <vector<int>> V,gV;
        vector <bool> uz;
        vector <int> ord,comp;
        vector <bool> ans;

        void dfs1(int node){
            uz[node]=true;
            for(auto& it:V[node])
                if(!uz[it])
                    dfs1(it);
            ord.pb(node);
        }
        void dfs2(int node,int c){
            comp[node]=c;
            for(auto& it:gV[node])
                if(!comp[it])
                    dfs2(it,c);
        }
    public:
        _2sat(int n){
            this->n=n;
            V.resize(2*n+1);
            gV.resize(2*n+1);
            uz.resize(2*n+1);
            comp.resize(2*n+1);
            ans.resize(n+1);
        }
        void addegde(int x,int y){
            dbg(x,y);
            V[x].pb(y);
            gV[y].pb(x);
        }
        void solve(){
            for(int i=1;i<=2*n;i++)
                if(!uz[i])
                    dfs1(i);
            for(int i=1,cates=0;i<=2*n;i++){
                int node=ord[2*n-i];
                if(!comp[node])
                    dfs2(node,++cates);
            }
            for(int i=1;i<=n;i++){
                if(comp[i] == comp[i+n]){
                    fout<<"-1\n";
                    return;
                }
                ans[i]=(comp[i] > comp[i+n]);
            }
            int cate=0;
            for(int i=1;i<=n;i++)
                if(ans[i])
                    cate++;
            fout<<cate<<'\n';
            for(int i=1;i<=n;i++)
                if(ans[i])
                    fout<<i<<'\n';
            return;
        }
};

vector <int> vec;
vector <int> arb[DMAX];

int V[DMAX];

int n,m;

int NOT(int node){
    if(node > n)
        return node-n;
    return node+n;
}

int main(){
    int t,i,j;
    int op,x,y;

    fin>>n>>m;
    _2sat var(n);
    for(i=1;i<=m;i++){
        fin>>x>>y>>op;
        if(op == 1)
            y=NOT(y);
        else if(op == 2)
            x=NOT(x);
        else if(op == 3){
            x=NOT(x);
            y=NOT(y);
        }
        var.addegde(NOT(x),y);
        var.addegde(NOT(y),x);
    }
    var.solve();

    return 0;
}