Cod sursa(job #2948815)

Utilizator Giulian617Buzatu Giulian Giulian617 Data 28 noiembrie 2022 14:03:55
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.69 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("party.in");
ofstream g("party.out");
const int NMAX=205;///we keep double the number of nodes because we have x and !x
int n,m,x,y,z,negation_x,negation_y,no_scc;///scc=strongly connected components
vector<int>order,component,ans,graph[NMAX],GT[NMAX];
bitset<NMAX>used;
void create_graph()
{
    f>>n>>m;
    for(int i=1; i<=m; i++)
    {
        f>>x>>y>>z;
        negation_x=x+n;
        negation_y=y+n;
        if(z==0)
        {
            graph[negation_x].push_back(y);
            graph[negation_y].push_back(x);
            GT[y].push_back(negation_x);
            GT[x].push_back(negation_y);
        }
        else if(z==1)
        {
            graph[negation_x].push_back(negation_y);
            graph[y].push_back(x);
            GT[negation_y].push_back(negation_x);
            GT[x].push_back(y);
        }
        else if(z==2)
        {
            graph[negation_y].push_back(negation_x);
            graph[x].push_back(y);
            GT[negation_x].push_back(negation_y);
            GT[y].push_back(x);
        }
        else
        {
            graph[x].push_back(negation_y);
            graph[y].push_back(negation_x);
            GT[negation_y].push_back(x);
            GT[negation_x].push_back(y);
        }
    }
}
void dfs(int node)
{
    used[node]=1;
    for(int next:graph[node])
        if(!used[next])
            dfs(next);
    order.push_back(node);
}
void dfsGT(int node)
{
    component[node]=no_scc;///we know that this will be in topological order
    for(int next:GT[node])
        if(!component[next])
            dfsGT(next);
}
void kosaraju()
{
    for(int i=1; i<=n; i++)
        if(!used[i])
            dfs(i);
    component.assign(n+1,0);
    reverse(order.begin(),order.end());///because from the first dfs with get the vector in the non-decreasing order and we want them in the non-increasing order
    for(int i=0; i<(int)order.size(); i++)
        if(!component[order[i]])
        {
            no_scc++;
            dfsGT(order[i]);
        }
}
void satisfiability()
{
    n*=2;///because we now have x and the negation of x as variables;
    kosaraju();
    n/=2;///because we will check the variables related to their negations, which start from n+1(where n is the one from the input)
    for(int i=1; i<=n; i++)
        if(component[i]>component[i+n])
            ans.push_back(i);
}
void write()
{
    g<<ans.size()<<'\n';
    for(int i=0;i<(int)ans.size();i++)
        g<<ans[i]<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    create_graph();
    satisfiability();
    write();
    return 0;
}