結果

問題 No.1553 Lovely City
ユーザー parukiparuki
提出日時 2021-07-02 19:19:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 2,963 bytes
コンパイル時間 2,671 ms
コンパイル使用メモリ 217,264 KB
実行使用メモリ 39,340 KB
最終ジャッジ日時 2023-09-11 12:43:33
合計ジャッジ時間 16,243 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 71 ms
35,208 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 305 ms
25,976 KB
testcase_09 AC 325 ms
27,900 KB
testcase_10 AC 353 ms
31,552 KB
testcase_11 AC 253 ms
23,116 KB
testcase_12 AC 365 ms
32,444 KB
testcase_13 AC 267 ms
23,932 KB
testcase_14 AC 315 ms
26,340 KB
testcase_15 AC 286 ms
25,116 KB
testcase_16 AC 367 ms
29,056 KB
testcase_17 AC 272 ms
25,216 KB
testcase_18 AC 488 ms
39,016 KB
testcase_19 AC 486 ms
39,096 KB
testcase_20 AC 481 ms
39,124 KB
testcase_21 AC 483 ms
39,144 KB
testcase_22 AC 480 ms
39,052 KB
testcase_23 AC 489 ms
39,128 KB
testcase_24 AC 491 ms
39,084 KB
testcase_25 AC 489 ms
39,340 KB
testcase_26 AC 492 ms
39,116 KB
testcase_27 AC 489 ms
39,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

vector<int> topologicalSort(const vector<vector<int> > &graph) {
    int V = (int)graph.size(), cur = 0;
    vector<int> deg(V), res(V);
    stack<int> stk;
    for (int i = 0; i < V; ++i)
        for (int v : graph[i])deg[v]++;
    for (int i = 0; i < V; ++i)if (deg[i] == 0)stk.push(i);
    while (!stk.empty()) {
        int v = stk.top(); stk.pop();
        res[cur++] = v;
        for (int w : graph[v])if (!--deg[w])stk.push(w);
    }
    return cur == V ? res : vector<int>();
}

class ConnectedComponents {
public:
    vector<vector<int>> operator()(const vector<vi>& G) {
        int n = (int)G.size();
        visited = vector<int>(n);
        for (int i = 0; i < n; ++i) {
            if (!visited[i]) {
                result.push_back(vector<int>());
                dfs(G, i, -1);
            }
        }
        return result;
    }

private:
    vector<int> visited;
    vector<vector<int>> result;

    void dfs(const vector<vi>& G, int vertex, int prev) {
        if (visited[vertex]++) return;
        result.back().push_back(vertex);
        for (auto edge : G[vertex]) {
            int to = edge;
            if (to != prev) {
                dfs(G, to, prev);
            }
        }
    }
};


void solve() {
    int N, M;
    cin >> N >> M;
    vector<vi> G(N), H(N);
    vi U(M), V(M);
    rep(i, M) {
        cin >> U[i] >> V[i];
        --U[i];
        --V[i];
        G[U[i]].push_back(V[i]);
        H[U[i]].push_back(V[i]);
        H[V[i]].push_back(U[i]);
    }
    // weakly connected components
    auto CC = ConnectedComponents()(H);

    vector<pii> ans;
    vi A(N, -1); // 頂点番号を振り直す
    each(C, CC) {
        int n = sz(C);
        rep(i, n) {
            A[C[i]] = i;
        }
        vector<vi> Z(n);
        each(u, C) each(v, G[u]) {
            Z[A[u]].push_back(A[v]);
        }
        auto TS = topologicalSort(Z);
        if (TS.empty()) {
            rep(i, n) {
                ans.emplace_back(C[i], C[(i + 1) % n]);
            }
        } else {
            rep(i, n - 1) {
                ans.emplace_back(C[TS[i]], C[TS[(i + 1) % n]]);
            }
        }
    }

    cout << sz(ans) << endl;
    each(a, ans) {
        cout << a.first + 1 << ' ' << a.second + 1 << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}
0