結果

問題 No.1553 Lovely City
ユーザー snow39snow39
提出日時 2021-06-18 22:32:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,903 bytes
コンパイル時間 1,415 ms
コンパイル使用メモリ 123,136 KB
実行使用メモリ 32,484 KB
最終ジャッジ日時 2023-09-04 23:43:14
合計ジャッジ時間 9,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 42 ms
26,448 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 104 ms
26,008 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
20,828 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 <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, order.size()) output.emplace_back(order[i], order[(i + 1) % (int)order.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