結果
| 問題 | 
                            No.1194 Replace
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-08-24 23:14:29 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 649 bytes | 
| コンパイル時間 | 3,016 ms | 
| コンパイル使用メモリ | 208,292 KB | 
| 最終ジャッジ日時 | 2025-01-13 13:38:39 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 8 WA * 19 | 
ソースコード
#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;
}