結果

問題 No.1194 Replace
ユーザー やむなくやむなく
提出日時 2020-08-22 14:45:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,594 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 180,860 KB
実行使用メモリ 78,624 KB
最終ジャッジ日時 2024-04-23 09:09:13
合計ジャッジ時間 22,043 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
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 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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]){
        if(visited[nx])continue;
        dfs3(nx);
        ma[x] = max(ma[x], ma[nx]);
    }
}

int main(){
    CFS;
    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);
    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[a[i]]];
    }
    cout << ans << endl;
    return 0;
}
0