結果

問題 No.1194 Replace
ユーザー milanis48663220
提出日時 2020-08-23 23:10:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 927 ms / 2,000 ms
コード長 828 bytes
コンパイル時間 1,082 ms
コンパイル使用メモリ 99,016 KB
実行使用メモリ 61,036 KB
最終ジャッジ日時 2024-10-15 19:26:28
合計ジャッジ時間 14,640 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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