結果

問題 No.1194 Replace
ユーザー auauaauaua
提出日時 2020-08-22 17:23:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,682 ms / 2,000 ms
コード長 1,669 bytes
コンパイル時間 2,301 ms
コンパイル使用メモリ 185,040 KB
実行使用メモリ 98,664 KB
最終ジャッジ日時 2024-04-23 11:21:06
合計ジャッジ時間 28,640 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,112 ms
65,276 KB
testcase_01 AC 1,209 ms
69,204 KB
testcase_02 AC 877 ms
56,724 KB
testcase_03 AC 718 ms
48,708 KB
testcase_04 AC 1,199 ms
69,520 KB
testcase_05 AC 1,091 ms
64,564 KB
testcase_06 AC 982 ms
60,752 KB
testcase_07 AC 1,672 ms
98,468 KB
testcase_08 AC 1,679 ms
98,664 KB
testcase_09 AC 1,680 ms
98,580 KB
testcase_10 AC 1,670 ms
98,548 KB
testcase_11 AC 1,665 ms
98,500 KB
testcase_12 AC 1,682 ms
98,524 KB
testcase_13 AC 890 ms
43,760 KB
testcase_14 AC 745 ms
37,284 KB
testcase_15 AC 617 ms
37,520 KB
testcase_16 AC 848 ms
43,592 KB
testcase_17 AC 538 ms
34,376 KB
testcase_18 AC 535 ms
31,672 KB
testcase_19 AC 850 ms
44,604 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 204 ms
18,816 KB
testcase_24 AC 61 ms
9,856 KB
testcase_25 AC 26 ms
6,940 KB
testcase_26 AC 255 ms
18,016 KB
testcase_27 AC 38 ms
6,940 KB
testcase_28 AC 113 ms
10,368 KB
testcase_29 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll inf=1e9+7;
const ll mod=998244353;
const int MAX=300010;


signed main(){
    ll n,m;cin>>n>>m;
    map<ll,ll>idx;
    map<ll,ll>inv;
    map<ll,ll>con;
    vector<vector<ll> >G(2*m);
    ll now=0;
    vector<ll>q(m);
    rep(i,m){
        ll b,c;cin>>b>>c; 
        con[b]=b;
        con[c]=c;
        q[i]=c;
        if(idx.find(c)==idx.end()){
            idx[c]=now;
            now++;
        }
        if(idx.find(b)==idx.end()){
            idx[b]=now;
            now++;
        }
        inv[idx[c]]=c;
        inv[idx[b]]=b;
        G[idx[c]].pb(idx[b]);
    }
    sort(rall(q));
    vector<ll>used(2*m);
    rep(i,m){
        if(used[idx[q[i]]])continue;
        ll c=q[i];
        queue<ll>q;
        used[idx[c]]=1;
        q.push(idx[c]);
        while(!q.empty()){
            ll k=q.front();
            q.pop();
            //cout<<inv[k]<<' '<<c<<endl;
            for(auto e:G[k]){
                if(used[e])continue;
                con[inv[e]]=max(con[inv[e]],c);
                used[e]=1;
                q.push(e);
            }
        }
    }
    ll ans=n*(n+1)/2;
    for(auto e:con){
        ans+=e.se-e.fi;
    }
    cout<<ans<<endl;
}
0