結果

問題 No.1553 Lovely City
ユーザー chocoruskchocorusk
提出日時 2021-06-18 22:03:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 691 ms / 2,000 ms
コード長 2,745 bytes
コンパイル時間 4,416 ms
コンパイル使用メモリ 203,812 KB
実行使用メモリ 53,320 KB
最終ジャッジ日時 2023-09-04 23:07:51
合計ジャッジ時間 20,685 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 27 ms
23,632 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 431 ms
34,072 KB
testcase_09 AC 475 ms
36,040 KB
testcase_10 AC 501 ms
42,208 KB
testcase_11 AC 355 ms
31,104 KB
testcase_12 AC 510 ms
44,384 KB
testcase_13 AC 382 ms
30,356 KB
testcase_14 AC 438 ms
34,572 KB
testcase_15 AC 387 ms
33,820 KB
testcase_16 AC 485 ms
38,304 KB
testcase_17 AC 363 ms
32,996 KB
testcase_18 AC 668 ms
53,232 KB
testcase_19 AC 674 ms
53,092 KB
testcase_20 AC 668 ms
53,068 KB
testcase_21 AC 661 ms
53,320 KB
testcase_22 AC 668 ms
53,092 KB
testcase_23 AC 689 ms
53,208 KB
testcase_24 AC 686 ms
53,172 KB
testcase_25 AC 677 ms
53,284 KB
testcase_26 AC 691 ms
53,220 KB
testcase_27 AC 675 ms
53,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct SCC{
    vector<vector<int>> g, gr;
    int n, k;
    vector<int> cmp, vs;
    vector<bool> used;
    void dfs(int x){
        used[x]=1;
        for(auto y:g[x]){
            if(!used[y]) dfs(y);
        }
        vs.push_back(x);
    }
    void rdfs(int v, int k){
        used[v]=1;
        cmp[v]=k;
        for(auto y:gr[v]){
            if(!used[y]) rdfs(y, k);
        }
    }
    SCC(const vector<vector<int>> &g):g(g), n(g.size()), cmp(n), used(n){
        gr.resize(n);
        for(int x=0; x<n; x++) for(auto y:g[x]) gr[y].push_back(x);
        for(int i=0; i<n; i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        k=0;
        for(int i=vs.size()-1; i>=0; i--){
            if(!used[vs[i]]) rdfs(vs[i], k++);
        }
    }
};
int main()
{
    int n, m;
    cin>>n>>m;
    vector<vector<int>> g(n), g1(n);
    for(int i=0; i<m; i++){
        int u, v;
        cin>>u>>v;
        u--; v--;
        g[u].push_back(v);
        g1[u].push_back(v);
        g1[v].push_back(u);
    }
    SCC scc(g);
    vector<int> w;
    bool used[200020]={};
    auto dfs=[&](auto dfs, int x)->void{
        used[x]=1;
        w.push_back(x);
        for(auto y:g1[x]){
            if(used[y]) continue;
            dfs(dfs, y);
        }
    };
    vector<P> ans;
    for(int i=0; i<n; i++){
        if(used[i]) continue;
        w.clear();
        dfs(dfs, i);
        map<int, int> mp;
        for(auto x:w){
            mp[scc.cmp[x]]++;
        }
        bool myon=0;
        for(auto p:mp){
            if(p.second!=1){
                myon=1;
            }
        }
        if(myon){
            for(int j=0; j<(int)w.size()-1; j++){
                ans.push_back({w[j], w[j+1]});
            }
            ans.push_back({w.back(), w[0]});
        }else{
            sort(w.begin(), w.end(), [&](int x, int y){ return scc.cmp[x]<scc.cmp[y];});
            for(int j=0; j<(int)w.size()-1; j++){
                ans.push_back({w[j], w[j+1]});
            }
        }
    }
    cout<<ans.size()<<endl;
    for(auto p:ans){
        cout<<p.first+1<<" "<<p.second+1<<endl;
    }
    return 0;
}
0