Pagini recente » Cod sursa (job #2446940) | Cod sursa (job #740540) | Cod sursa (job #2794466) | Cod sursa (job #5730) | Cod sursa (job #2948814)
#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,graph[NMAX],GT[NMAX];
bitset<NMAX>used;
bitset<NMAX/2>value;
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);
GT[negation_y].push_back(negation_x);
}
else if(z==2)
{
graph[negation_y].push_back(negation_x);
GT[negation_x].push_back(negation_y);
}
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])
value[i]=1;///else value[i] is 0, because they cannot be equal since there is granted that a solution exists
}
void write()
{
g<<value.count()<<'\n';
for(int i=1; i<=n; i++)
if(value[i])
g<<i<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
create_graph();
satisfiability();
write();
return 0;
}