Cod sursa(job #2659830)

Utilizator victorzarzuZarzu Victor victorzarzu Data 17 octombrie 2020 15:54:40
Problema Party Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 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];
vector<int> topo;
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);
  topo.push_back(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);
  for(vector<int>::reverse_iterator it = topo.rbegin();it != topo.rend();++it)
    if(!c[topo[*it]])
    {
      ++comp;
      dfs_t(topo[*it]);
    }
  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;
}