Submission #1059353

# Submission time Handle Problem Language Result Execution time Memory
1059353 2024-08-14T21:42:20 Z MilosMilutinovic Potemkin cycle (CEOI15_indcyc) C++14
90 / 100
1000 ms 1664 KB
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m; 
  vector<vector<bool>> c(n, vector<bool>(n));
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    c[x][y] = true;
    c[y][x] = true;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  auto Solve = [&](int i, int j, int k) {
    vector<bool> del(n);
    del[k] = true;
    for (int p : g[k]) {
      if (p != i && p != j) {
        del[p] = true;
      }
    }
    vector<int> p(n, -1);
    vector<int> que(1, i);
    vector<int> d(n, -1);
    d[i] = 0;
    for (int b = 0; b < (int) que.size(); b++) {
      int i = que[b];
      for (int j : g[i]) {
        if (del[j]) {
          continue;
        }
        if (d[j] == -1) {
          p[j] = i;
          d[j] = d[i] + 1;
          que.push_back(j);
        }
      }
    }
    vector<int> seq;
    for (int v = j; v != -1; v = p[v]) {
      seq.push_back(v);
    }
    seq.push_back(k);
    for (int i = 0; i < (int) seq.size(); i++) {
      cout << seq[i] + 1 << " ";
    }
    cout << '\n';
  };
  for (int k = 0; k < n; k++) {
    vector<bool> del(n);
    del[k] = true;
    for (int i : g[k]) {
      del[i] = true;
    }
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    vector<int> sz(n, 1);
    function<int(int)> Root = [&](int x) {
      return p[x] == x ? x : p[x] = Root(p[x]);
    };
    auto Merge = [&](int x, int y) {
      x = Root(x);
      y = Root(y);
      if (x == y) {
        return;
      }
      if (sz[x] < sz[y]) {
        swap(x, y);
      }
      sz[x] += sz[y];
      p[y] = x;
    };
    for (int i = 0; i < n; i++) {
      if (del[i]) {
        continue;
      }
      for (int j : g[i]) {
        if (del[j]) {
          continue;
        }
        Merge(i, j);
      }
    }
    vector<vector<int>> ids(n);
    for (int i = 0; i < n; i++) {
      if (!c[i][k]) {
        continue;
      }
      for (int j : g[i]) {
        if (del[j]) {
          continue;
        }
        ids[Root(j)].push_back(i);
      }
    }
    vector<int> in(n);
    for (int i = 0; i < n; i++) {
      sort(ids[i].begin(), ids[i].end());
      ids[i].erase(unique(ids[i].begin(), ids[i].end()), ids[i].end());
      sort(ids[i].begin(), ids[i].end(), [&](int i, int j) {
        return (int) g[i].size() > (int) g[j].size();
      });
      for (int i : ids[i]) {
        in[i] += 1;
      }
      for (int x = 0; x < (int) ids[i].size(); x++) {
        if ((int) ids[i].size() - x - 1 < (int) g[x].size()) {
          for (int y = x + 1; y < (int) ids[i].size(); y++) {
            if (!c[ids[i][x]][ids[i][y]]) {
              Solve(ids[i][x], ids[i][y], k);
              return 0;
            }
          }
        } else {
          int cnt = 0;
          for (int y : g[ids[i][x]]) {
            cnt += in[y];
          }
          if (cnt + 1 != (int) ids[i].size()) {
            for (int y = x + 1; y < (int) ids[i].size(); y++) {
              if (!c[ids[i][x]][ids[i][y]]) {
                Solve(ids[i][x], ids[i][y], k);
                return 0;
              }
            }
          }
        }
      }
      for (int i : ids[i]) {
        in[i] -= 1;
      }
    }
  }
  cout << "no" << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 31 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 21 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1116 KB Output is correct
2 Correct 7 ms 992 KB Output is correct
3 Correct 865 ms 1116 KB Output is correct
4 Correct 401 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 856 KB Output is correct
2 Correct 461 ms 1076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1368 KB Output is correct
2 Correct 59 ms 1368 KB Output is correct
3 Correct 43 ms 1644 KB Output is correct
4 Execution timed out 1008 ms 1664 KB Time limit exceeded
5 Halted 0 ms 0 KB -