結果
問題 | No.1194 Replace |
ユーザー | SSRS |
提出日時 | 2020-08-22 15:19:53 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 821 ms / 2,000 ms |
コード長 | 2,637 bytes |
コンパイル時間 | 2,101 ms |
コンパイル使用メモリ | 189,416 KB |
実行使用メモリ | 112,744 KB |
最終ジャッジ日時 | 2024-10-15 09:35:26 |
合計ジャッジ時間 | 14,957 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 503 ms
72,648 KB |
testcase_01 | AC | 542 ms
76,828 KB |
testcase_02 | AC | 423 ms
63,472 KB |
testcase_03 | AC | 345 ms
54,464 KB |
testcase_04 | AC | 554 ms
76,768 KB |
testcase_05 | AC | 507 ms
71,744 KB |
testcase_06 | AC | 471 ms
67,348 KB |
testcase_07 | AC | 818 ms
112,648 KB |
testcase_08 | AC | 806 ms
112,716 KB |
testcase_09 | AC | 821 ms
112,736 KB |
testcase_10 | AC | 804 ms
112,744 KB |
testcase_11 | AC | 809 ms
112,744 KB |
testcase_12 | AC | 801 ms
112,716 KB |
testcase_13 | AC | 419 ms
43,196 KB |
testcase_14 | AC | 338 ms
35,632 KB |
testcase_15 | AC | 306 ms
39,920 KB |
testcase_16 | AC | 387 ms
43,248 KB |
testcase_17 | AC | 271 ms
36,228 KB |
testcase_18 | AC | 272 ms
31,444 KB |
testcase_19 | AC | 394 ms
45,316 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 1 ms
5,248 KB |
testcase_23 | AC | 119 ms
20,096 KB |
testcase_24 | AC | 39 ms
10,552 KB |
testcase_25 | AC | 15 ms
5,248 KB |
testcase_26 | AC | 137 ms
16,464 KB |
testcase_27 | AC | 23 ms
5,248 KB |
testcase_28 | AC | 60 ms
9,992 KB |
testcase_29 | AC | 8 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; struct strongly_connected_components{ vector<vector<int>> scc; vector<int> c; void dfs1(vector<vector<int>> &E, vector<bool> &used, vector<int> &t, int v){ for (int w : E[v]){ if (!used[w]){ used[w] = true; dfs1(E, used, t, w); } } t.push_back(v); } void dfs2(vector<vector<int>> &E, vector<bool> &used, int v){ scc.back().push_back(v); for (int w : E[v]){ if (!used[w]){ used[w] = true; dfs2(E, used, w); } } } strongly_connected_components(vector<vector<int>> G){ int V = G.size(); vector<vector<int>> E1(V), E2(V); for (int i = 0; i < V; i++){ for (int j : G[i]){ E1[i].push_back(j); E2[j].push_back(i); } } vector<bool> used(V, false); vector<int> t; for (int i = 0; i < V; i++){ if (!used[i]){ used[i] = true; dfs1(E1, used, t, i); } } reverse(t.begin(), t.end()); vector<bool> used2(V, false); for (int i = 0; i < V; i++){ if (!used2[t[i]]){ used2[t[i]] = true; scc.push_back(vector<int>()); dfs2(E2, used2, t[i]); } } c = vector<int>(V); int sz = scc.size(); for (int i = 0; i < sz; i++){ for (int j : scc[i]){ c[j] = i; } } } int size(){ return scc.size(); } int operator [](int k){ return c[k]; } }; int main(){ int N, M; cin >> N >> M; vector<int> B(M), C(M); for (int i = 0; i < M; i++){ cin >> B[i] >> C[i]; } vector<int> xval; for (int i = 0; i < M; i++){ xval.push_back(B[i]); xval.push_back(C[i]); } sort(xval.begin(), xval.end()); xval.erase(unique(xval.begin(), xval.end()), xval.end()); int cnt = xval.size(); long long ans = (long long) N * (N + 1) / 2; for (int x : xval){ ans -= x; } map<int, int> mp; for (int i = 0; i < cnt; i++){ mp[xval[i]] = i; } for (int i = 0; i < M; i++){ B[i] = mp[B[i]]; C[i] = mp[C[i]]; } vector<vector<int>> E(cnt); for (int i = 0; i < M; i++){ E[B[i]].push_back(C[i]); } strongly_connected_components S(E); int cnt2 = S.size(); vector<int> mx(cnt2); for (int i = 0; i < cnt; i++){ mx[S[i]] = max(mx[S[i]], xval[i]); } vector<vector<int>> f(cnt2); for (int i = 0; i < M; i++){ int x = S[B[i]]; int y = S[C[i]]; if (x != y){ f[x].push_back(y); } } for (int i = cnt2 - 1; i >= 0; i--){ for (int j : f[i]){ mx[i] = max(mx[i], mx[j]); } } for (int i = 0; i < cnt; i++){ ans += mx[S[i]]; } cout << ans << endl; }