結果

問題 No.1194 Replace
ユーザー SSRSSSRS
提出日時 2020-08-22 15:19:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 772 ms / 2,000 ms
コード長 2,637 bytes
コンパイル時間 2,188 ms
コンパイル使用メモリ 185,536 KB
実行使用メモリ 112,880 KB
最終ジャッジ日時 2024-04-23 09:41:04
合計ジャッジ時間 14,052 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 477 ms
72,636 KB
testcase_01 AC 516 ms
76,664 KB
testcase_02 AC 495 ms
64,144 KB
testcase_03 AC 334 ms
55,876 KB
testcase_04 AC 495 ms
76,784 KB
testcase_05 AC 482 ms
71,796 KB
testcase_06 AC 428 ms
67,804 KB
testcase_07 AC 757 ms
112,692 KB
testcase_08 AC 772 ms
112,840 KB
testcase_09 AC 727 ms
112,864 KB
testcase_10 AC 736 ms
112,652 KB
testcase_11 AC 734 ms
112,868 KB
testcase_12 AC 740 ms
112,880 KB
testcase_13 AC 377 ms
43,384 KB
testcase_14 AC 323 ms
35,756 KB
testcase_15 AC 282 ms
40,104 KB
testcase_16 AC 377 ms
43,248 KB
testcase_17 AC 262 ms
36,228 KB
testcase_18 AC 256 ms
31,572 KB
testcase_19 AC 366 ms
45,596 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 106 ms
20,096 KB
testcase_24 AC 37 ms
10,684 KB
testcase_25 AC 14 ms
6,940 KB
testcase_26 AC 126 ms
16,472 KB
testcase_27 AC 21 ms
6,940 KB
testcase_28 AC 54 ms
9,868 KB
testcase_29 AC 7 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0