結果

問題 No.2464 To DAG
ユーザー t98slidert98slider
提出日時 2023-09-08 22:13:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 4,426 bytes
コンパイル時間 2,960 ms
コンパイル使用メモリ 186,272 KB
実行使用メモリ 77,972 KB
最終ジャッジ日時 2023-09-08 22:14:15
合計ジャッジ時間 17,932 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 316 ms
77,972 KB
testcase_03 AC 292 ms
42,480 KB
testcase_04 AC 286 ms
41,628 KB
testcase_05 AC 271 ms
41,936 KB
testcase_06 AC 270 ms
41,968 KB
testcase_07 AC 183 ms
25,588 KB
testcase_08 AC 201 ms
25,244 KB
testcase_09 AC 188 ms
22,528 KB
testcase_10 AC 177 ms
21,808 KB
testcase_11 AC 113 ms
16,408 KB
testcase_12 AC 87 ms
14,124 KB
testcase_13 AC 104 ms
15,484 KB
testcase_14 AC 172 ms
28,504 KB
testcase_15 AC 242 ms
32,760 KB
testcase_16 AC 119 ms
19,308 KB
testcase_17 AC 14 ms
5,960 KB
testcase_18 AC 66 ms
14,712 KB
testcase_19 AC 1 ms
4,996 KB
testcase_20 AC 2 ms
5,056 KB
testcase_21 AC 1 ms
5,044 KB
testcase_22 AC 75 ms
10,824 KB
testcase_23 AC 79 ms
10,396 KB
testcase_24 AC 76 ms
11,396 KB
testcase_25 AC 79 ms
11,464 KB
testcase_26 AC 42 ms
10,900 KB
testcase_27 AC 293 ms
43,492 KB
testcase_28 AC 299 ms
43,588 KB
testcase_29 AC 288 ms
42,684 KB
testcase_30 AC 186 ms
22,020 KB
testcase_31 AC 151 ms
18,432 KB
testcase_32 AC 135 ms
16,700 KB
testcase_33 AC 243 ms
25,916 KB
testcase_34 AC 111 ms
14,716 KB
testcase_35 AC 64 ms
9,936 KB
testcase_36 AC 63 ms
10,244 KB
testcase_37 AC 58 ms
10,532 KB
testcase_38 AC 57 ms
10,448 KB
testcase_39 AC 48 ms
30,008 KB
testcase_40 AC 49 ms
30,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

namespace internal_scc{
template <class E> struct csr {
    std::vector<int> start;
    std::vector<E> elist;
    csr(int n, const std::vector<std::pair<int, E>>& edges)
        : start(n + 1), elist(edges.size()) {
        for (auto e : edges) {
            start[e.first + 1]++;
        }
        for (int i = 1; i <= n; i++) {
            start[i] += start[i - 1];
        }
        auto counter = start;
        for (auto e : edges) {
            elist[counter[e.first]++] = e.second;
        }
    }
};
struct scc_graph {
  public:
    scc_graph(int n) : _n(n) {}
    int num_vertices() { return _n; }
    void add_edge(int from, int to) { edges.push_back({from, {to}}); }
    std::pair<int, std::vector<int>> scc_ids() {
        auto g = csr<edge>(_n, edges);
        int now_ord = 0, group_num = 0;
        std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);
        visited.reserve(_n);
        auto dfs = [&](auto self, int v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.push_back(v);
            for (int i = g.start[v]; i < g.start[v + 1]; i++) {
                auto to = g.elist[i].to;
                if (ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if (low[v] == ord[v]) {
                while (true) {
                    int u = visited.back();
                    visited.pop_back();
                    ord[u] = _n;
                    ids[u] = group_num;
                    if (u == v) break;
                }
                group_num++;
            }
        };
        for (int i = 0; i < _n; i++) {
            if (ord[i] == -1) dfs(dfs, i);
        }
        for (auto& x : ids) {
            x = group_num - 1 - x;
        }
        return {group_num, ids};
    }

    std::vector<std::vector<int>> scc() {
        auto ids = scc_ids();
        int group_num = ids.first;
        std::vector<int> counts(group_num);
        for (auto x : ids.second) counts[x]++;
        std::vector<std::vector<int>> groups(ids.first);
        for (int i = 0; i < group_num; i++) {
            groups[i].reserve(counts[i]);
        }
        for (int i = 0; i < _n; i++) {
            groups[ids.second[i]].push_back(i);
        }
        return groups;
    }

  private:
    int _n;
    struct edge {
        int to;
    };
    std::vector<std::pair<int, edge>> edges;
};
}

struct scc_graph {
  public:
    scc_graph() : internal(0) {}
    scc_graph(int n) : internal(n) {}
    void add_edge(int from, int to) {
        int n = internal.num_vertices();
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        internal.add_edge(from, to);
    }
    std::vector<std::vector<int>> scc() { return internal.scc(); }
  private:
    internal_scc::scc_graph internal;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int,int>>> g(n);
    scc_graph g2(n);
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].emplace_back(v, 0);
        g2.add_edge(u, v);
    }

    vector<int> idx(n);
    vector<bool> used(n), visited(n);

    auto dfs = [&](auto dfs, int v) -> int {
        if(used[v]) return -1;
        if(visited[v]) return v;
        visited[v] = true;
        for(int &j = idx[v]; j < g[v].size(); j++){
            int u, c;
            tie(u, c) = g[v][j];
            if(c == 1) continue;
            int res = dfs(dfs, u);
            if(res != -1){
                g[v][j] = make_pair(u, 1);
                if(res != v){
                    visited[v] = false;
                    return res;
                }
            }
        }
        visited[v] = false;
        used[v] = true;
        return -1;
    };

    auto G = g2.scc();
    for(auto &&vec : G){
        for(auto &&v : vec){
            dfs(dfs, v);
        }
    }

    vector<pair<int,int>> edge;
    for(int v = 0; v < n; v++){
        for(auto &&pa : g[v]){
            if(pa.second) continue;
            edge.emplace_back(v, pa.first);
        }
    }
    cout << n << " " << edge.size() << '\n';
    for(auto &&pa : edge){
        cout << pa.first + 1 << ' ' << pa.second + 1 << '\n';
    }
}
0