結果

問題 No.1194 Replace
ユーザー MayimgMayimg
提出日時 2020-08-24 23:14:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 649 bytes
コンパイル時間 3,227 ms
コンパイル使用メモリ 210,204 KB
実行使用メモリ 78,592 KB
最終ジャッジ日時 2024-04-24 04:00:34
合計ジャッジ時間 21,100 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,318 ms
78,464 KB
testcase_08 AC 1,301 ms
78,464 KB
testcase_09 AC 1,310 ms
78,464 KB
testcase_10 AC 1,293 ms
78,592 KB
testcase_11 AC 1,337 ms
78,464 KB
testcase_12 AC 1,376 ms
78,336 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 43 ms
8,448 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
map<int, vector<int>> g;
map<int, bool> vis;
map<int, int> to;
int dfs (int cur) {
  if (vis[cur]) return to[cur];
  vis[cur] = true;
  int res = cur;
  for (int& nxt : g[cur]) {
    res = max(res, dfs(nxt));
  }
  return to[cur] = res;
}
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  long long ans = 1LL * n * (n + 1) / 2;
  for (auto& p : g) {
    int get = dfs(p.first);
    ans += get - p.first;
  }
  cout << ans << endl;
  return 0;
}
0