답안 #570024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
570024 2022-05-28T12:47:45 Z 600Mihnea Potemkin cycle (CEOI15_indcyc) C++17
0 / 100
3 ms 436 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], gather[N];

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

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

void join(int a, int b) {
  t[get_root(a)] = get_root(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;
      gather[i].clear();
    }
    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 (int i = 1; i <= n; i++) {
      if (valid[i]) {
        gather[get_root(i)].push_back(i);
      }
    }
    int v1_sol, v2_sol;
    bool found = 0;
    for (int i = 1; i <= n && !found; i++) {
      if (valid[i] && get_root(i) == i) {
        vector<int> verts;
        for (auto &member : gather[i]) {
          for (auto &copil : g[member]) {
            if (copil != root && !valid[copil]) {
              verts.push_back(copil);
            }
          }
        }
        sort(verts.begin(), verts.end());
        verts.resize(unique(verts.begin(), verts.end()) - verts.begin());
        if ((int) verts.size() >= 2) {
          for (auto &v1 : verts) {
            for (auto &v2 : verts) {
              if (found) break;
              if (v1 == v2) break;
              if (is_edge(v1, v2)) continue;

              v1_sol = v1;
              v2_sol = v2;
              found = 1;
            }
          }
        }
      }
    }
    if (!found) continue;
    int v1 = v1_sol, v2 = v2_sol;

    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);
        }
      }
    }
    assert(dist[v1] != -1);
    if (dist[v2] != -1 && dist[v2] != INF) {
      while (v2 != v1) {
        cout << v2 << " ";
        v2 = par[v2];
      }
      cout << v1 << " " << root << "\n";
      exit(0);
    } else {
      assert(0);
    }

  }
  cout << "no\n";
  exit(0);
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:48:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |   freopen ("input.txt", "r", stdin);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 420 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Incorrect 2 ms 340 KB Expected integer, but "no" found
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 436 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -