結果
問題 | No.2464 To DAG |
ユーザー | t98slider |
提出日時 | 2023-09-08 22:13:55 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 273 ms / 2,000 ms |
コード長 | 4,426 bytes |
コンパイル時間 | 2,484 ms |
コンパイル使用メモリ | 189,276 KB |
実行使用メモリ | 78,180 KB |
最終ジャッジ日時 | 2024-06-26 15:19:18 |
合計ジャッジ時間 | 14,893 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,376 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 269 ms
78,180 KB |
testcase_03 | AC | 252 ms
43,004 KB |
testcase_04 | AC | 244 ms
41,788 KB |
testcase_05 | AC | 236 ms
42,204 KB |
testcase_06 | AC | 222 ms
42,292 KB |
testcase_07 | AC | 164 ms
26,036 KB |
testcase_08 | AC | 175 ms
25,416 KB |
testcase_09 | AC | 149 ms
22,576 KB |
testcase_10 | AC | 148 ms
21,784 KB |
testcase_11 | AC | 105 ms
16,652 KB |
testcase_12 | AC | 84 ms
14,316 KB |
testcase_13 | AC | 101 ms
15,652 KB |
testcase_14 | AC | 164 ms
28,536 KB |
testcase_15 | AC | 237 ms
33,064 KB |
testcase_16 | AC | 114 ms
19,572 KB |
testcase_17 | AC | 14 ms
6,144 KB |
testcase_18 | AC | 65 ms
14,776 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 80 ms
11,092 KB |
testcase_23 | AC | 79 ms
10,672 KB |
testcase_24 | AC | 79 ms
11,588 KB |
testcase_25 | AC | 78 ms
11,660 KB |
testcase_26 | AC | 42 ms
11,164 KB |
testcase_27 | AC | 273 ms
43,748 KB |
testcase_28 | AC | 271 ms
43,864 KB |
testcase_29 | AC | 264 ms
43,128 KB |
testcase_30 | AC | 161 ms
22,440 KB |
testcase_31 | AC | 141 ms
18,712 KB |
testcase_32 | AC | 124 ms
16,888 KB |
testcase_33 | AC | 202 ms
26,036 KB |
testcase_34 | AC | 90 ms
14,988 KB |
testcase_35 | AC | 63 ms
10,220 KB |
testcase_36 | AC | 67 ms
10,324 KB |
testcase_37 | AC | 56 ms
10,708 KB |
testcase_38 | AC | 56 ms
10,576 KB |
testcase_39 | AC | 44 ms
30,216 KB |
testcase_40 | AC | 43 ms
30,216 KB |
ソースコード
#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'; } }