Cod sursa(job #2931863)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 1 noiembrie 2022 08:52:43
Problema Felinare Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

ifstream in ("felinare.in");
ofstream out ("felinare.out");

const int max_size = 8192;

int st[2 * max_size + 5], dr[2 * max_size + 5], n;
vector <int> mc[max_size];
bitset <2 * max_size + 5> viz, cpl;

bool pairup (int nod)
{
   if (viz[nod])
   {
      return false;
   }
   viz[nod] = 1;
   for (auto f : mc[nod])
   {
      if (!dr[f])
      {
         st[nod] = f;
         dr[f] = nod;
         return true;
      }
   }
   for (auto f : mc[nod])
   {
      if (pairup(dr[f]))
      {
         st[nod] = f;
         dr[f] = nod;
         return true;
      }
   }
   return false;
}

void dfs (int nod)
{
   for (auto f : mc[nod])
   {
      if (!cpl[f])
      {
         cpl[dr[f]] = 0;
         cpl[f] = nod;
         dfs(dr[f]);
      }
   }
}

int main ()
{
   int q;
   in >> n >> q;
   while (q--)
   {
      int x, y;
      in >> x >> y;
      mc[x].push_back(y + n);
   }
   /// faci cuplaj ca dupa vine min vertex cover
   while (1)
   {
      for (int i = 1; i <= n; i++)
      {
         viz[i] = 0;
      }
      bool ok = false;
      for (int i = 1; i <= n; i++)
      {
         if (!st[i])
         {
            ok |= pairup(i);
         }
      }
      if (!ok)
      {
         break;
      }
   }
   /// bagi mvxcover, nodurile care nu ies in mvx => noduri aprinse
   /// orcm teoretic pot afisa deja nr de felinare ca e 2 * n - cuplaj
   for (int i = 1; i <= 2 * n; i++)
   {
      viz[i] = 0;
   }
   for (int i = 1; i <= n; i++)
   {
      if (!st[i])
      {
         dfs(i);
      }
   }
   /// si acum fac invers, nu iau nodurile din mvxc
   int rez = 0;
   for (int i = 1; i <= n; i++)
   {
      if (st[i] > 0)
      {
         rez++;
      }
   }
   out << 2 * n - rez << '\n';
   for (int i = 1; i <= n; i++)
   {
      // proba daca pica
   }
   in.close();
   out.close();
   return 0;
}