結果

問題 No.1194 Replace
ユーザー definedefine
提出日時 2020-08-02 14:00:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,783 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 207,060 KB
実行使用メモリ 117,516 KB
最終ジャッジ日時 2023-09-25 15:55:28
合計ジャッジ時間 11,945 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 318 ms
102,060 KB
testcase_01 AC 345 ms
103,408 KB
testcase_02 AC 270 ms
99,920 KB
testcase_03 AC 218 ms
98,028 KB
testcase_04 AC 340 ms
103,740 KB
testcase_05 AC 312 ms
105,200 KB
testcase_06 AC 291 ms
104,668 KB
testcase_07 AC 441 ms
116,100 KB
testcase_08 AC 438 ms
117,096 KB
testcase_09 AC 440 ms
116,100 KB
testcase_10 AC 431 ms
115,948 KB
testcase_11 AC 432 ms
116,268 KB
testcase_12 AC 436 ms
117,516 KB
testcase_13 AC 290 ms
97,456 KB
testcase_14 AC 246 ms
96,056 KB
testcase_15 AC 212 ms
94,212 KB
testcase_16 AC 277 ms
97,172 KB
testcase_17 AC 202 ms
93,260 KB
testcase_18 AC 195 ms
93,648 KB
testcase_19 AC 283 ms
98,276 KB
testcase_20 AC 32 ms
83,200 KB
testcase_21 AC 31 ms
83,236 KB
testcase_22 AC 31 ms
83,316 KB
testcase_23 AC 98 ms
87,976 KB
testcase_24 AC 50 ms
84,904 KB
testcase_25 AC 40 ms
83,820 KB
testcase_26 AC 111 ms
89,544 KB
testcase_27 AC 46 ms
84,592 KB
testcase_28 AC 66 ms
85,784 KB
testcase_29 AC 34 ms
83,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()

template<class T> inline bool chmin(T &a, T b){
	if(a>b){a=b;return true;}
	return false;
}
template<class T> inline bool chmax(T &a, T b){
	if(a<b){a=b;return true;}
	return false;
}
constexpr int mod = 1e9+7;
constexpr int inf = 3e18;

int N,M,Ans;
int B[500005],C[500005],cnt[1000005];
vector<int>xx,G[1000005],rG[1000005];
bool used[1000005];
vector<int>vs;
void dfs(int x){
    used[x]=true;
    for(int i:G[x])if(!used[i])dfs(i);
    vs.push_back(x);
}
int cmp[1000005],mx[1000005];
vector<int>newrG[1000005];
void rdfs(int v,int k){
    used[v]=true;
    cmp[v]=k;
    cnt[k]++;chmax(mx[k],xx[v]);
    for(int i:rG[v])if(!used[i])rdfs(i,k);
}
int scc(){
    rep(i,N)if(!used[i])dfs(i);
    memset(used,0,sizeof(used));
    int k=0;
    rev(i,len(vs))if(!used[vs[i]])rdfs(vs[i],k++);
    rep(i,N){
        for(int j:G[i]){
            newrG[cmp[j]].push_back(cmp[i]);
        }
    }
    return k;
}
signed main(){
    cin.tie(0);ios::sync_with_stdio(false);
    cin>>N>>M;
    rep(i,M){
        cin>>B[i]>>C[i];
        xx.push_back(B[i]);
        xx.push_back(C[i]);
    }
    sort(all(xx));xx.erase(unique(all(xx)),xx.end());
    Ans=(1+N)*N/2;
    for(int i:xx)Ans-=i;
    rep(i,M){
        B[i]=lower_bound(all(xx),B[i])-xx.begin();
        C[i]=lower_bound(all(xx),C[i])-xx.begin();
        G[B[i]].push_back(C[i]);
        rG[C[i]].push_back(B[i]);
    }
    N=len(xx);
    N=scc();
    rev(i,N){
        Ans+=mx[i]*cnt[i];
        for(int j:newrG[i])chmax(mx[j],mx[i]);
    }
    cout<<Ans<<endl;
}
0