Submission #1257964

#TimeUsernameProblemLanguageResultExecution timeMemory
1257964duyanhloveavPotemkin cycle (CEOI15_indcyc)C++20
70 / 100
1096 ms2236 KiB
#include <bits/stdc++.h>
using namespace std;

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)

using ll = long long;

const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;

int n, m;
vector<vector<int>> adj;
vector<array<int, 2>> e;

namespace SUB1 {
  bool check() {
    return (n <= 10 && m <= 45);
  }

  vector<int> par;
  vector<char> used;

  void dfs(int u, int s, int h) {
    if (h >= 4) {
      vector<int> res;
      int cur = u;
      while (cur) {
        res.push_back(cur);
        cur = par[cur];
      }
      reverse(begin(res), end(res));
      if (binary_search(begin(adj[res.back()]), end(adj[res.back()]), res.front())) {
        bool f = false;
        for (int i = 0; i < (int)res.size() && !f; ++i) {
          for (int j = i + 1; j < (int)res.size(); ++j) {
            if (j == i + 1 || (i == 0 && j == (int)res.size() - 1)) continue;
            if (binary_search(begin(adj[res[i]]), end(adj[res[i]]), res[j])) {
              f = true;
              break;
            }
          }
        }
        if (!f) {
          for (auto &v : res) {
            cout << v << " ";
          }
          cout << "\n";
          exit(0);
        }
      }
    }
    used[u] = true;
    for (auto &v : adj[u]) {
      if (!used[v]) {
        par[v] = u;
        dfs(v, s, h + 1);
      }
    }
    used[u] = false;
  }

  void solve() {
    par.assign(n + 1, 0);
    used.assign(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
      sort(begin(adj[i]), end(adj[i]));
    }
    for (int i = 1; i <= n; ++i) {
      par[i] = 0;
      dfs(i, i, 1);
    }
    cout << "no\n";
  }
}

namespace SUBFULL {
  void solve() {
    vector<vector<bool>> check(n + 1, vector<bool>(n + 1));
    for (int i = 1; i <= n; ++i) {
      for (auto &j : adj[i]) {
        check[i][j] = true;
      }
    }
    sort(begin(e), end(e));
    e.erase(unique(begin(e), end(e)), end(e));
    vector<int> dist(n + 1), par(n + 1);
    deque<int> q;
    for (auto &x : e) {
      int A = x[0], B = x[1];
      for (auto &x : adj[A]) {
        if (x == B) {
          continue;
        }
        vector<bool> f(n + 1);
        f[A] = true;
        for (int w = 1; w <= n; ++w) {
          if (w == x || w == B) {
            continue;
          }
          if (check[A][w]) {
            f[w] = true;
          }
        }
        fill(begin(dist), end(dist), -1);
        fill(begin(par), end(par), -1);
        dist[x] = 0;
        while (!q.empty()) {
          q.pop_back();
        }
        q.push_back(x);
        bool ck = false;
        while (!q.empty()) {
          int u = q.front(); 
          q.pop_front();
          for (auto &v : adj[u]) {
            if (f[v] || (u == A && v == B) || (u == B && v == A)) {
              continue;
            }
            if (!~dist[v]) {
              dist[v] = dist[u] + 1;
              par[v] = u;
              if (v == B) {
                ck = true;
                break;
              }
              q.push_back(v);
            }
          }
          if (ck) {
            break;
          }
        }
        if (!ck) {
          continue;
        }
        vector<int> res;
        int cur = B;
        while (~cur) { 
          res.push_back(cur);
          cur = par[cur]; 
        }
        res.push_back(A);
        if ((int) (res.size()) < 4) {
          continue;
        }
        reverse(begin(res), end(res));
        bool r = false;
        for (int i = 0; i < (int)(res.size()) && !r; ++i) {
          for (int j = i + 1; j < (int)(res.size()); ++j) {
            if (j == i + 1 || (i == 0 && j == (int)(res.size()) - 1)) continue;
            if (check[res[i]][res[j]]) { 
              r = true; 
              break; 
            }
          }
        }
        if (!r) {
          for (auto &x : res) {
            cout << x << " ";
          }
          return;
        }
      }
    }
    cout << "no\n";
  }
}


void _27augain(void) {
  cin >> n >> m;
  adj.resize(n + 1);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    e.push_back({u, v});
  }
  if (SUB1::check()) {
    SUB1::solve();
    return;
  }
  SUBFULL::solve();
}

int32_t main(void) {
#define task "27augain"
  cin.tie(0)->sync_with_stdio(0);
  for (string iext : {"in", "inp"}) {
    if (fopen((task "." + iext).c_str(), "r")) {
      freopen((task "." + iext).c_str(), "r", stdin);
      freopen(task ".out", "w", stdout);
    }
  }
  int testcase = 1;
  // cin >> testcase;
  while (testcase--) {
    _27augain();
  }
  return 0;
}

Compilation message (stderr)

indcyc.cpp: In function 'int32_t main()':
indcyc.cpp:194:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  194 |       freopen((task "." + iext).c_str(), "r", stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
indcyc.cpp:195:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |       freopen(task ".out", "w", stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...