結果

問題 No.1194 Replace
ユーザー やむなくやむなく
提出日時 2020-08-22 14:37:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,527 bytes
コンパイル時間 2,515 ms
コンパイル使用メモリ 187,112 KB
実行使用メモリ 139,372 KB
最終ジャッジ日時 2024-04-23 08:58:47
合計ジャッジ時間 24,076 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,150 ms
88,036 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 722 ms
66,064 KB
testcase_04 AC 1,254 ms
93,144 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,871 ms
139,080 KB
testcase_08 AC 1,863 ms
139,372 KB
testcase_09 AC 1,850 ms
139,176 KB
testcase_10 AC 1,855 ms
139,164 KB
testcase_11 AC 1,907 ms
139,332 KB
testcase_12 AC 1,844 ms
139,340 KB
testcase_13 AC 840 ms
48,308 KB
testcase_14 AC 654 ms
38,876 KB
testcase_15 WA -
testcase_16 AC 803 ms
48,928 KB
testcase_17 WA -
testcase_18 AC 504 ms
35,456 KB
testcase_19 AC 808 ms
51,664 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 208 ms
23,424 KB
testcase_24 AC 60 ms
12,416 KB
testcase_25 AC 17 ms
6,944 KB
testcase_26 AC 223 ms
17,632 KB
testcase_27 AC 29 ms
6,948 KB
testcase_28 AC 89 ms
10,368 KB
testcase_29 AC 10 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;
map<int, vector<int>> e;
map<int, vector<int>> 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);
        }
    }
}

int main(){
    CFS;
    ll n;
    int m;
    cin >> n >> m;
    vi b(m), c(m);
    rep(i,m){
        cin >> 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);
    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;
    for(auto &p:e){
        if(id[p.first] == -1){
            dfs(p.first, sz);
        }
    }
    vi a(sz);
    vi ma(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);
    }
    mti ee(sz);
    rep(i, m){
        ee[col[name[b[i]]]].push_back(col[name[c[i]]]);
    }
    set<int> f;
    rep(i, sz){
        int cc = col[a[i]];
        if(f.find(cc) != f.end()) continue;
        f.insert(cc);
        for(auto &nx : ee[cc]){
            ma[cc] = max(ma[cc], ma[nx]);
        }
    }
    rep(i, sz){
        ans += ma[col[a[i]]];
    }
    cout << ans << endl;
    return 0;
}
0