結果

問題 No.1553 Lovely City
ユーザー simansiman
提出日時 2022-12-22 21:21:42
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,529 bytes
コンパイル時間 1,420 ms
コンパイル使用メモリ 131,544 KB
実行使用メモリ 77,712 KB
最終ジャッジ日時 2024-04-29 03:51:29
合計ジャッジ時間 35,714 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1,123 ms
17,920 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1,244 ms
59,392 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 879 ms
46,224 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef vector <vector<int>> Graph;

vector<int> tsort(Graph G, int start, int end) {
  vector<int> ret;

  int degree[end];
  memset(degree, 0, sizeof(degree));

  for (int from = start; from < end; from++) {
    for (int i = 0; i < G[from].size(); i++) {
      int to = G[from][i];
      degree[to]++;
    }
  }

  stack<int> root;

  for (int i = start; i < end; i++) {
    if (degree[i] == 0) {
      root.push(i);
    }
  }

  while (!root.empty()) {
    int from = root.top();
    root.pop();

    ret.push_back(from);

    for (int i = 0; i < G[from].size(); i++) {
      int to = G[from][i];
      degree[to]--;

      if (degree[to] == 0) {
        root.push(to);
      }
    }
  }

  return ret;
}

map<int, set<int>> SL;

void dfs(int v, Graph &BG, bool *visited, bool *checked) {
  visited[v] = true;
  checked[v] = true;

  for (int u : BG[v]) {
    if (checked[u]) {
      fprintf(stderr, "skip: v: %d, u: %d\n", v, u);
      SL[v].insert(u);
    } else if (visited[u]) {
      // NOOP
    } else {
      dfs(u, BG, visited, checked);
    }
  }

  checked[v] = false;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> U;
  vector<int> V;
  Graph BG(N);
  Graph OG(N);
  Graph DG(N);

  int u, v;

  for (int i = 0; i < M; ++i) {
    cin >> u >> v;
    --u;
    --v;

    U.push_back(u);
    V.push_back(v);
    BG[u].push_back(v);
  }

  vector<int> A;
  vector<int> B;

  bool visited[N];
  memset(visited, false, sizeof(visited));
  for (int v = 0; v < N; ++v) {
    if (visited[v]) continue;

    bool checked[N];
    memset(checked, false, sizeof(checked));
    dfs(v, BG, visited, checked);
  }

  for (int i = 0; i < M; ++i) {
    int u = U[i];
    int v = V[i];
    if (SL[u].find(v) != SL[u].end()) {
      A.push_back(u);
      B.push_back(v);
      continue;
    }

    DG[u].push_back(v);
    OG[u].push_back(v);
    OG[v].push_back(u);
  }

  bool checked[N];
  memset(checked, false, sizeof(checked));

  for (int v = 0; v < N; ++v) {
    if (checked[v]) continue;

    queue<int> que;
    que.push(v);
    int id = 0;
    map<int, int> mapping;
    map<int, int> r_mapping;
    vector<int> vs;

    while (not que.empty()) {
      int x = que.front();
      que.pop();

      if (checked[x]) continue;
      checked[x] = true;
      mapping[x] = id;
      r_mapping[id] = x;
      id++;
      vs.push_back(x);

      for (int u : OG[x]) {
        if (checked[u]) continue;
        que.push(u);
      }
    }

    int n = vs.size();
    if (n >= 2) {
      fprintf(stderr, "n: %d\n", n);

      Graph G(n);
      for (int x : vs) {
        int from = mapping[x];

        for (int u : DG[x]) {
          int to = mapping[u];
          G[from].push_back(to);
        }
      }

      vector<int> res = tsort(G, 0, n);

      if (res.size() != n) {
        for (int x : vs) {
          for (int u : G[mapping[x]]) {
            fprintf(stderr, "x: %d, u: %d\n", x + 1, r_mapping[u] + 1);
          }
        }
      }

      int k = res.size() - 1;
      for (int i = 0; i < k; ++i) {
        int a = res[i];
        int b = res[i + 1];
        a = r_mapping[a];
        b = r_mapping[b];
        A.push_back(a);
        B.push_back(b);
      }

      assert(res.size() == n);
    }
  }

  int K = A.size();
  cout << K << endl;
  for (int i = 0; i < K; ++i) {
    cout << A[i] + 1 << " " << B[i] + 1 << endl;
  }

  return 0;
}

0