結果

問題 No.1553 Lovely City
ユーザー milanis48663220milanis48663220
提出日時 2021-06-18 22:47:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 466 ms / 2,000 ms
コード長 3,896 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 134,532 KB
実行使用メモリ 46,164 KB
最終ジャッジ日時 2023-09-04 23:54:12
合計ジャッジ時間 14,300 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 54 ms
36,580 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 285 ms
27,952 KB
testcase_09 AC 317 ms
29,620 KB
testcase_10 AC 340 ms
38,504 KB
testcase_11 AC 238 ms
26,032 KB
testcase_12 AC 358 ms
41,104 KB
testcase_13 AC 261 ms
24,972 KB
testcase_14 AC 279 ms
28,608 KB
testcase_15 AC 269 ms
28,828 KB
testcase_16 AC 348 ms
30,812 KB
testcase_17 AC 251 ms
29,588 KB
testcase_18 AC 466 ms
45,972 KB
testcase_19 AC 447 ms
45,476 KB
testcase_20 AC 445 ms
46,164 KB
testcase_21 AC 453 ms
46,020 KB
testcase_22 AC 440 ms
45,968 KB
testcase_23 AC 436 ms
45,048 KB
testcase_24 AC 451 ms
45,896 KB
testcase_25 AC 442 ms
45,168 KB
testcase_26 AC 461 ms
44,952 KB
testcase_27 AC 446 ms
45,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

class Scc{
    public:
        int N;
        vector<vector<int>> G;
        vector<vector<int>> G_inv;
        vector<int> idx;

        Scc(int n){
            N = n;
            G = vector<vector<int>>(n, vector<int>());
            G_inv = vector<vector<int>>(n, vector<int>());
            used = vector<bool>(n, false);
            idx = vector<int>(n);
        }

        void add_edge(int from, int to){
            G[from].push_back(to);
            G_inv[to].push_back(from);
        }

        vector<vector<int>> scc(){
            vector<vector<int>> ans;
            for(int i = 0; i < N; i++){
                if(!used[i]) dfs1(i);
            }
            clear();
            int cur = 0;
            for(int i = vs.size()-1; i >= 0; i--){
                if(!used[vs[i]]) {
                    dfs2(vs[i], cur);
                    cur++;
                    ans.push_back(buf);
                    buf.clear();
                }
            }
            return ans;
        }

    private:
        vector<bool> used;
        vector<int> vs;
        vector<int> buf;
        void clear(){
            for(int i = 0; i < N; i++) used[i] = false;
        }

        void dfs1(int v){
            used[v] = true;
            for(int i = 0; i < G[v].size(); i++){
                if(!used[G[v][i]]) dfs1(G[v][i]);
            }
            vs.push_back(v);
        }

        void dfs2(int v, int k){
            used[v] = true;
            idx[v] = k;
            for(int i = 0; i < G_inv[v].size(); i++){
                if(!used[G_inv[v][i]]) dfs2(G_inv[v][i], k);
            }
            buf.push_back(v);
        }
};

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

using P = pair<int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<int> u(m), v(m);
    Scc scc(n);
    UnionFind uf(n);
    for(int i = 0; i < m; i++){
        cin >> u[i] >> v[i]; u[i]--; v[i]--;
        scc.add_edge(u[i], v[i]);
        uf.unionSet(u[i], v[i]);
    }
    auto v_scc = scc.scc();
    vector<bool> dag(n, true);
    vector<vector<int>> components(n);
    for(auto u: v_scc){
        if(u.size() > 1) dag[uf.root(u[0])] = false;
        for(int x : u){
            components[uf.root(x)].push_back(x);
        }
    }
    vector<P> ans;
    for(int i = 0; i < n; i++){
        if(components[i].size() <= 1) continue;
        int sz = components[i].size();
        for(int j = 0; j < sz-1; j++){
            ans.push_back(P(components[i][j], components[i][j+1]));
        }
        if(!dag[i]){
            ans.push_back(P(components[i][sz-1], components[i][0]));
        }
    }
    cout << ans.size() << endl;
    for(auto [a, b]: ans) cout << a+1 << ' ' << b+1 << endl;
}
0