Cod sursa(job #2415967)

Utilizator cella.florescuCella Florescu cella.florescu Data 26 aprilie 2019 17:46:14
Problema Santa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.41 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 45e3;

vector < int > g[MAXN + 1], art, stk, path;
vector < vector < int > > bcc;
int lvl[MAXN + 1], low[MAXN + 1], ok[MAXN + 1], seen[MAXN + 1];

void bcc_dfs(int node, int father = 0) {
  low[node] = lvl[node] = lvl[father] + 1;
  stk.push_back(node);
  for (auto it : g[node])
    if (it ^ father) {
      if (lvl[it])
        low[node] = min(low[node], lvl[it]);
      else {
        bcc_dfs(it, node);
        low[node] = min(low[node], low[it]);
        ok[node] |= ok[it];
        if (low[it] >= lvl[node]) {
          vector < int > comp;
          if (ok[it] == 0) {
            while (stk.back() ^ it)
              stk.pop_back();
            stk.pop_back();
          } else {
            art.push_back(node);
            do {
              comp.push_back(stk.back());
              stk.pop_back();
            } while (comp.back() ^ it);
            comp.push_back(node);
            bcc.push_back(comp);
          }
        }
      }
    }
}

ofstream fout("santa.out");

inline void tzeapa() {
  fout << "No chance";
  exit(0);
}

int bkt(int num, int targ, int node, int lst) {
  seen[node] = 1;
  if (num > 1)
    path.push_back(node);
  if (num == targ) {
    if (node == lst || lst == 0)
      return 1;
    path.pop_back();
    seen[node] = 0;
    return 0;
  }
  for (auto it : g[node])
    if (seen[it] == 0 && (it != lst || num == targ - 1) && bkt(num + 1, targ, it, lst))
      return 1;
  path.pop_back();
  seen[node] = 0;
  return 0;
}

int main()
{
    int n, m, s, e, q;
    ifstream fin("santa.in");
    fin >> n >> m;
    for (int i = 0; i < m; ++i) {
      int x, y;
      fin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    fin >> s >> e >> q;
    fin.close();
    art = {s};
    ok[s] = 1;
    bcc_dfs(e, 0);
    if (ok[e] == 0)
      tzeapa();
    if (find(bcc[0].begin(), bcc[0].end(), q) == bcc[0].end()) {
      reverse(bcc.begin(), bcc.end());
      reverse(art.begin(), art.end());
    }
    if (find(bcc[0].begin(), bcc[0].end(), q) == bcc[0].end())
      tzeapa();
    art.back() = 0;
    art[0] = q;
    path.push_back(q);
    memset(seen, 1, sizeof seen);
    for (int i = 0; i < (int) bcc.size(); ++i) {
      for (auto it : bcc[i])
        seen[it] = 0;
      if (bkt(1, bcc[i].size(), art[i], art[i + 1]) == 0)
        tzeapa();
    }
    fout << path.size() << '\n';
    for (auto it : path)
      fout << it << " ";
    return 0;
}