結果

問題 No.1553 Lovely City
ユーザー 👑 NachiaNachia
提出日時 2021-06-18 21:52:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 1,000 ms
コンパイル使用メモリ 90,244 KB
実行使用メモリ 24,940 KB
最終ジャッジ日時 2023-09-04 22:51:30
合計ジャッジ時間 8,296 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 29 ms
22,596 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 93 ms
15,948 KB
testcase_09 AC 102 ms
16,444 KB
testcase_10 AC 107 ms
20,740 KB
testcase_11 AC 75 ms
14,660 KB
testcase_12 AC 117 ms
22,160 KB
testcase_13 AC 80 ms
14,372 KB
testcase_14 AC 90 ms
16,012 KB
testcase_15 AC 78 ms
16,032 KB
testcase_16 AC 107 ms
17,580 KB
testcase_17 AC 82 ms
16,836 KB
testcase_18 AC 147 ms
24,788 KB
testcase_19 AC 147 ms
24,712 KB
testcase_20 AC 148 ms
24,732 KB
testcase_21 AC 153 ms
24,720 KB
testcase_22 AC 149 ms
24,644 KB
testcase_23 AC 154 ms
24,716 KB
testcase_24 AC 151 ms
24,940 KB
testcase_25 AC 154 ms
24,672 KB
testcase_26 AC 154 ms
24,740 KB
testcase_27 AC 149 ms
24,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>
#include <atcoder/dsu>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N,M;
vector<vector<int>> E;
vector<int> inIdx;
vector<int> F;
vector<pair<int,int>> ans;

void solve(vector<int> V){
  vector<int> I;

  for(int i : V) if(inIdx[i] == 0) if(F[i]) I.push_back(i);
  for(int i=0; i<I.size(); i++){
    int p = I[i];
    for(int e : E[p]) if(0 == --inIdx[e]) I.push_back(e);
  }

  bool cy = false;
  for(int i : V) if(inIdx[i] != 0) cy = true;
  
  if(cy){
    I.clear();
    for(int i : V) if(F[i]) I.push_back(i);
    I.push_back(I.front());
  }

  for(int i=0; i+1<I.size(); i++) ans.push_back(make_pair(I[i],I[i+1]));
}

void testcase(){
  cin >> N >> M;
  E.resize(N);
  inIdx.assign(N,0);
  F.assign(N,0);
  atcoder::dsu G(N);
  rep(i,M){
    int u,v; cin >> u >> v; u--; v--;
    E[u].push_back(v);
    inIdx[v]++;
    F[u] = F[v] = 1;
    G.merge(u,v);
  }
  
  for(auto& g : G.groups()) solve(g);


  cout << ans.size() << "\n";
  for(auto a : ans) cout << (a.first+1) << " " << (a.second+1) << "\n";
}

int main(){
  testcase();
  return 0;
}

struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_inst;
0