Submission #569998

# Submission time Handle Problem Language Result Execution time Memory
569998 2022-05-28T12:28:05 Z 600Mihnea Potemkin cycle (CEOI15_indcyc) C++17
30 / 100
419 ms 2520 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1000 + 7;
const int INF = (int) 1e9 + 7;
int n, m, dist[N], par[N], t[N];
bool valid[N];
vector<int> g[N];

void clr() {
  for (int i = 1; i <= n; i++) t[i] = i;
}

int get_root(int a) {
  if (t[a] == a) return a;
  return t[a] = get_root(t[a]);
}

void join(int a, int b) {
  a = get_root(b);
  b = get_root(b);
  t[a] = b;
}

bool is_edge(int a, int b) {
  int low = 0, high = (int) g[a].size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (g[a][mid] == b) {
      return 1;
    }
    if (g[a][mid] < b) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return 0;
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  ///freopen ("input.txt", "r", stdin);

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
  }
  for (int root = 1; root <= n; root++) {
    clr();
    for (int i = 1; i <= n; i++) {
      valid[i] = 1;
    }
    valid[root] = 0;
    for (auto &v : g[root]) {
      valid[v] = 0;
    }
    for (int i = 1; i <= n; i++) {
      if (valid[i]) {
        for (auto &j : g[i]) {
          if (valid[j]) {
            join(i, j);
          }
        }
      }
    }
    for (auto &v1 : g[root]) {
      for (auto &v2 : g[root]) {
        if (v1 == v2) break;
        if (is_edge(v1, v2)) continue;
        set<int> s1, s2;
        for (auto &i : g[v1]) {
          if (valid[i]) s1.insert(get_root(i));
        }
        for (auto &i : g[v2]) {
          if (valid[i]) s2.insert(get_root(i));
        }
        bool good = 0;

        for (auto &guy : s1) {
          if (s2.count(guy)) {
            good = 1;
          }
        }
        for (int i = 1; i <= n; i++) {
          dist[i] = INF;
          par[i] = 0;
        }
        dist[root] = -1;
        for (auto &v : g[root]) {
          if (v != v1 && v != v2) {
            dist[v] = -1;
          }
        }
        queue<int> q;
        dist[v1] = 0;
        q.push(v1);
        while (!q.empty()) {
          int a = q.front();
          q.pop();
          for (auto &b : g[a]) {
            if (dist[b] == INF) {
              dist[b] = 1 + dist[a];
              par[b] = a;
              q.push(b);
            }
          }
        }

        if (dist[v2] == 1) continue;
        if (dist[v2] != -1 && dist[v2] != INF) {
          vector<int> path;
          int currentVertex = v2;
          while (currentVertex != v1) {
            path.push_back(currentVertex);
            currentVertex = par[currentVertex];
          }
          assert(currentVertex == v1);
          path.push_back(currentVertex);

          reverse(path.begin(), path.end());
          assert((int) path.size() >= 3);

          vector<int> valids;
          for (int j = 0; j < (int) path.size(); j++) {
            if (valid[path[j]]) {
              valids.push_back(path[j]);
              assert(1 <= j && j <= (int) path.size() - 2);
            } else {
              assert(j == 0 || j == (int) path.size() - 1);
            }
          }
          assert(get_root(path[1]) == get_root(path[(int) path.size() - 2]));

          while (v2 != v1) {
            cout << v2 << " ";
            v2 = par[v2];
          }
          cout << v1 << " " << root << "\n";
          exit(0);
        }
      }
    }
  }
  cout << "no\n";
  exit(0);
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:86:14: warning: variable 'good' set but not used [-Wunused-but-set-variable]
   86 |         bool good = 0;
      |              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 109 ms 400 KB Output is correct
2 Runtime error 2 ms 596 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 9 ms 664 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 419 ms 1664 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 72 ms 1140 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 84 ms 2520 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -