結果

問題 No.1194 Replace
ユーザー やむなくやむなく
提出日時 2020-08-22 15:08:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 944 ms / 2,000 ms
コード長 2,621 bytes
コンパイル時間 1,863 ms
コンパイル使用メモリ 181,712 KB
実行使用メモリ 78,496 KB
最終ジャッジ日時 2024-04-23 09:31:19
合計ジャッジ時間 16,033 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 586 ms
51,092 KB
testcase_01 AC 631 ms
53,924 KB
testcase_02 AC 461 ms
44,728 KB
testcase_03 AC 374 ms
38,504 KB
testcase_04 AC 611 ms
54,020 KB
testcase_05 AC 579 ms
50,404 KB
testcase_06 AC 527 ms
47,436 KB
testcase_07 AC 909 ms
78,320 KB
testcase_08 AC 888 ms
78,356 KB
testcase_09 AC 889 ms
78,496 KB
testcase_10 AC 944 ms
78,296 KB
testcase_11 AC 905 ms
78,348 KB
testcase_12 AC 902 ms
78,468 KB
testcase_13 AC 491 ms
30,896 KB
testcase_14 AC 395 ms
25,880 KB
testcase_15 AC 332 ms
28,496 KB
testcase_16 AC 465 ms
31,092 KB
testcase_17 AC 297 ms
26,016 KB
testcase_18 AC 287 ms
22,912 KB
testcase_19 AC 479 ms
32,392 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 113 ms
14,848 KB
testcase_24 AC 33 ms
8,448 KB
testcase_25 AC 13 ms
6,944 KB
testcase_26 AC 134 ms
12,672 KB
testcase_27 AC 19 ms
6,940 KB
testcase_28 AC 54 ms
7,936 KB
testcase_29 AC 5 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by yamunaku on 2020/08/22.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

map<int, int> name;
mti e;
mti ne;
vi id;
vi col;

void dfs(int x, int &num){
    id[x] = -2;
    for(auto &nx : e[x]){
        if(id[nx] == -1){
            dfs(nx, num);
        }
    }
    id[x] = num++;
}

void dfs2(int x, int c){
    col[x] = c;
    for(auto &nx : ne[x]){
        if(col[nx] == -1){
            dfs2(nx, c);
        }
    }
}

vi ma;
mti ee;
vi visited;

void dfs3(int x){
    visited[x] = true;
    for(auto &nx : ee[x]){
        ma[x] = max(ma[x], ma[nx]);
        if(visited[nx])continue;
        dfs3(nx);
        ma[x] = max(ma[x], ma[nx]);
    }
}

int main(){
    ll n;
    int m;
    scanf("%lld %d", &n, &m);
    vi b(m), c(m);
    rep(i, m){
        scanf("%d %d", &b[i], &c[i]);
        name[b[i]] = name[c[i]] = -1;
    }
    ll ans = n * (n + 1) / 2;
    int tmp = 0;
    for(auto &p : name){
        p.second = tmp++;
        ans -= p.first;
    }
    id = col = vi(tmp, -1);
    e = ne = mti(tmp);
    rep(i, m){
        int u = name[b[i]], v = name[c[i]];
        e[u].push_back(v);
        ne[v].push_back(u);
    }
    int sz = 0;
    rep(i, tmp){
        if(id[i] == -1){
            dfs(i, sz);
        }
    }
    vi a(sz);
    ma = vi(sz, 0);
    rep(i, tmp){
        a[id[i]] = i;
    }
    per(i, sz){
        if(col[a[i]] == -1){
            dfs2(a[i], a[i]);
        }
    }
    for(auto &p : name){
        ma[col[p.second]] = max(ma[col[p.second]], p.first);
    }
    ee = mti(sz);
    rep(i, m){
        ee[col[name[b[i]]]].push_back(col[name[c[i]]]);
    }
    visited = vi(sz);
    rep(i, sz){
        if(visited[i]) continue;
        dfs3(i);
    }
    rep(i, sz){
        ans += ma[col[i]];
    }
    cout << ans << endl;
    return 0;
}
0