結果

問題 No.1553 Lovely City
ユーザー snow39snow39
提出日時 2021-06-18 22:36:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 2,931 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 122,368 KB
実行使用メモリ 32,308 KB
最終ジャッジ日時 2023-09-04 23:46:38
合計ジャッジ時間 8,936 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 42 ms
26,544 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 84 ms
20,556 KB
testcase_09 AC 91 ms
21,724 KB
testcase_10 AC 109 ms
26,028 KB
testcase_11 AC 70 ms
18,224 KB
testcase_12 AC 120 ms
29,064 KB
testcase_13 AC 76 ms
18,484 KB
testcase_14 AC 90 ms
21,196 KB
testcase_15 AC 80 ms
20,372 KB
testcase_16 AC 94 ms
22,620 KB
testcase_17 AC 83 ms
20,836 KB
testcase_18 AC 149 ms
32,192 KB
testcase_19 AC 142 ms
32,192 KB
testcase_20 AC 145 ms
32,236 KB
testcase_21 AC 146 ms
31,264 KB
testcase_22 AC 144 ms
32,192 KB
testcase_23 AC 152 ms
32,008 KB
testcase_24 AC 152 ms
32,176 KB
testcase_25 AC 149 ms
31,900 KB
testcase_26 AC 145 ms
32,308 KB
testcase_27 AC 147 ms
32,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

class DisjointSet {
public:
    int N;
    vector<int> rank, p;
    vector<int> sz;

    DisjointSet() {}
    DisjointSet(int n) : N(n), rank(n), p(n), sz(n) {
        for (int i = 0; i < N; i++) makeSet(i);
    }

    void makeSet(int x) {
        p[x] = x;
        rank[x] = 0;
        sz[x] = 1;
    }

    bool same(int x, int y) {
        return leader(x) == leader(y);
    }

    void unite(int x, int y) {
        if (same(x, y)) return;
        link(leader(x), leader(y));
    }

    void link(int x, int y) {
        if (rank[x] > rank[y]) swap(x, y);

        p[x] = y;
        sz[y] += sz[x];
        if (rank[x] == rank[y]) {
            ++rank[y];
        }
    }

    int leader(int x) {
        if (x != p[x]) p[x] = leader(p[x]);
        return p[x];
    }

    int size(int x) {
        return sz[leader(x)];
    }
};

void solve() {
    int N, M;
    cin >> N >> M;
    vector<int> U(M), V(M);
    rep(i, M) cin >> U[i] >> V[i];

    rep(i, M) {
        U[i]--;
        V[i]--;
    }

    vector<vector<int>> g(N);
    rep(i, M) g[U[i]].push_back(V[i]);

    DisjointSet us(N);
    rep(i, M) us.unite(U[i], V[i]);

    vector<vector<int>> nodes(N);
    rep(i, N) nodes[us.leader(i)].push_back(i);
    vector<vector<int>> e_idxs(N);
    rep(i, M) e_idxs[us.leader(U[i])].push_back(i);

    vector<int> in(N, 0);
    vector<pair<int, int>> output;
    for (int k = 0; k < N; k++) {
        if (nodes[k].empty()) continue;

        for (auto ed : e_idxs[k]) in[V[ed]]++;

        queue<int> Q;
        for (auto id : nodes[k])
            if (in[id] == 0) Q.push(id);
        vector<int> order;
        while (!Q.empty()) {
            auto now = Q.front();
            Q.pop();

            order.push_back(now);

            for (auto nex : g[now]) {
                in[nex]--;
                if (in[nex] == 0) Q.push(nex);
            }
        }
        if (order.size() == nodes[k].size()) {
            for (int i = 0; i + 1 < order.size(); i++) output.emplace_back(order[i], order[i + 1]);
        } else {
            rep(i, nodes[k].size())
                output.emplace_back(nodes[k][i], nodes[k][(i + 1) % (int)nodes[k].size()]);
        }
    }

    cout << output.size() << "\n";
    for (auto [a, b] : output) cout << a + 1 << " " << b + 1 << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0