結果

問題 No.1553 Lovely City
ユーザー SSRSSSRS
提出日時 2021-06-18 22:23:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,038 bytes
コンパイル時間 2,647 ms
コンパイル使用メモリ 183,160 KB
実行使用メモリ 27,800 KB
最終ジャッジ日時 2023-09-04 23:34:53
合計ジャッジ時間 24,996 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 32 ms
15,500 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 3 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 406 ms
23,692 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 290 ms
18,604 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 <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> E(N), E2(N);
  for (int i = 0; i < M; i++){
    int U, V;
    cin >> U >> V;
    U--;
    V--;
    E[U].push_back(V);
    E2[U].push_back(V);
    E2[V].push_back(U);
  }
  vector<int> d(N, 0);
  for (int i = 0; i < N; i++){
    for (int j : E[i]){
      d[j]++;
    }
  }
  queue<int> Q;
  for (int i = 0; i < N; i++){
    if (d[i] == 0){
      Q.push(i);
    }
  }
  vector<int> t;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    t.push_back(v);
    for (int w : E[v]){
      d[w]--;
      if (d[w] == 0){
        Q.push(w);
      }
    }
  }
  int cnt = t.size();
  vector<int> pos(N, -1);
  for (int i = 0; i < cnt; i++){
    pos[t[i]] = i;
  }
  int K = 0;
  vector<int> A, B;
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++){
    if (!used[i]){
      used[i] = true;
      vector<int> id;
      queue<int> Q2;
      Q2.push(i);
      while (!Q2.empty()){
        int v = Q2.front();
        Q2.pop();
        id.push_back(v);
        for (int w : E2[v]){
          if (!used[w]){
            used[w] = true;
            Q2.push(w);
          }
        }
      }
      int cnt2 = id.size();
      bool s = false;
      for (int j = 0; j < cnt2; j++){
        if (pos[id[j]] == -1){
          s = true;
        }
      }
      if (!s){
        vector<pair<int, int>> P(cnt2);
        for (int j = 0; j < cnt2; j++){
          P[j] = make_pair(pos[id[j]], id[j]);
        }
        sort(P.begin(), P.end());
        for (int j = 0; j < cnt2 - 1; j++){
          K++;
          A.push_back(P[j].second);
          B.push_back(P[j + 1].second);
        }
      } else {
        for (int j = 0; j < cnt2; j++){
          int j2 = j + 1;
          if (j2 == cnt2){
            j2--;
          }
          K++;
          A.push_back(id[j]);
          B.push_back(id[j2]);
        }
      }
    }
  }
  cout << K << endl;
  for (int i = 0; i < K; i++){
    cout << A[i] + 1 << ' ' << B[i] + 1 << endl;
  }
}
0