結果

問題 No.1194 Replace
ユーザー milanis48663220milanis48663220
提出日時 2020-08-23 23:10:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 711 ms / 2,000 ms
コード長 828 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 98,380 KB
実行使用メモリ 61,156 KB
最終ジャッジ日時 2024-04-23 18:40:22
合計ジャッジ時間 11,539 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 421 ms
39,752 KB
testcase_01 AC 436 ms
42,136 KB
testcase_02 AC 331 ms
34,728 KB
testcase_03 AC 275 ms
30,080 KB
testcase_04 AC 435 ms
42,288 KB
testcase_05 AC 403 ms
39,212 KB
testcase_06 AC 358 ms
36,996 KB
testcase_07 AC 696 ms
61,152 KB
testcase_08 AC 709 ms
61,024 KB
testcase_09 AC 711 ms
61,032 KB
testcase_10 AC 693 ms
61,156 KB
testcase_11 AC 708 ms
61,156 KB
testcase_12 AC 694 ms
61,032 KB
testcase_13 AC 291 ms
24,844 KB
testcase_14 AC 244 ms
21,096 KB
testcase_15 AC 219 ms
22,152 KB
testcase_16 AC 300 ms
24,748 KB
testcase_17 AC 196 ms
20,184 KB
testcase_18 AC 179 ms
18,396 KB
testcase_19 AC 296 ms
25,640 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 79 ms
11,788 KB
testcase_24 AC 26 ms
7,552 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 87 ms
11,076 KB
testcase_27 AC 13 ms
5,376 KB
testcase_28 AC 38 ms
7,196 KB
testcase_29 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

ll N, M;
map<ll, vector<ll>> G;
set<ll> used;
ll ans;

bool is_used(ll m){
    return used.count(m) > 0;
}

void dfs(ll v, ll r){
    used.insert(v);
    if(r > v) ans += r-v;
    for(ll to : G[v]){
        if(!is_used(to)) dfs(to, r);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> M;
    vector<ll> v;
    for(int i = 0; i < M; i++){
        ll b, c; cin >> b >> c;
        G[c].push_back(b);
        v.push_back(c);
    }
    ans = (N*(N+1))/2;
    sort(v.begin(), v.end(), greater<ll>());
    for(ll i : v) {
        if(!is_used(i)) dfs(i, i);
    }
    cout << ans << endl;
}
0