Cod sursa(job #2660103)

Utilizator victorzarzuZarzu Victor victorzarzu Data 18 octombrie 2020 11:02:34
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <bits/stdc++.h>
 
using namespace std;
ifstream f("party.in");
ofstream g("party.out");
int n, m, x, y, t, comp, rez_nr;
vector<int> graf[2 * 100 + 5], graf_t[2 * 100 + 5];
int c[2 * 100 + 5];
stack<int> st;
bool viz[2 * 100 + 5], rez[100 + 5];
 
int Not(int x)
{
  if(x < n)
    return x + n;
  return x - n;
}
 
void add(int x, int y)
{
  graf[Not(x)].push_back(y);
  graf[Not(y)].push_back(x);
  graf_t[y].push_back(Not(x));
  graf_t[x].push_back(Not(y));
}
 
void Read()
{
  f>>n>>m;
  for(int i = 1;i <= m;++i)
    {
      f>>x>>y>>t;
      if(t == 0)
        add(x, y); // one of them
      else if(t == 1)
        add(x, Not(y)); // not y for being one if x is 0 => y = 0 
      else if(t == 2)
        add(Not(x), y);
      else
        add(Not(x), Not(y)); // not both => 1 if one is 1 and one is 0
    }
}
 
void dfs(int nod)
{
  viz[nod] = true;
  for(auto it : graf[nod])
    if(!viz[it])
      dfs(it);
  st.push(nod);
}
 
void dfs_t(int nod) 
{
  c[nod] = comp;
  for(auto it : graf_t[nod]) 
    if(!c[it])
      dfs_t(it);
}
 
void Solve()
{
  for(int i = 1;i <= 2 * n;++i)
    if(!viz[i])
      dfs(i);
  while(!st.empty())
  {
    int node = st.top(); st.pop();
 
    if(!c[node])
    {
      ++comp;
      dfs_t(node);
    }
  }
  for(int i = 1;i <= n;++i)
    if(c[i] > c[Not(i)])
      ++rez_nr, rez[i] = true;
  g<<rez_nr<<'\n';
  for(int i = 1;i <= n;++i)
    if(rez[i])
      g<<i<<'\n';
}
 
int main()
{
  Read();
  Solve();
  return 0;
}