結果

問題 No.1194 Replace
ユーザー definedefine
提出日時 2020-08-02 12:25:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 2,222 ms
コンパイル使用メモリ 207,340 KB
実行使用メモリ 116,292 KB
最終ジャッジ日時 2023-09-25 13:51:37
合計ジャッジ時間 14,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 382 ms
106,212 KB
testcase_01 AC 411 ms
106,504 KB
testcase_02 AC 323 ms
104,024 KB
testcase_03 AC 268 ms
98,340 KB
testcase_04 AC 414 ms
107,676 KB
testcase_05 AC 381 ms
105,836 KB
testcase_06 AC 355 ms
104,884 KB
testcase_07 AC 531 ms
116,048 KB
testcase_08 AC 528 ms
116,076 KB
testcase_09 AC 534 ms
116,292 KB
testcase_10 AC 527 ms
116,148 KB
testcase_11 AC 527 ms
116,248 KB
testcase_12 AC 529 ms
116,064 KB
testcase_13 AC 359 ms
97,348 KB
testcase_14 AC 301 ms
96,212 KB
testcase_15 AC 260 ms
94,216 KB
testcase_16 AC 340 ms
97,120 KB
testcase_17 AC 237 ms
93,084 KB
testcase_18 AC 239 ms
93,276 KB
testcase_19 AC 336 ms
97,380 KB
testcase_20 AC 31 ms
83,324 KB
testcase_21 AC 31 ms
83,224 KB
testcase_22 AC 31 ms
83,372 KB
testcase_23 AC 114 ms
87,892 KB
testcase_24 AC 55 ms
84,936 KB
testcase_25 AC 44 ms
83,824 KB
testcase_26 AC 139 ms
89,684 KB
testcase_27 AC 51 ms
84,692 KB
testcase_28 AC 80 ms
85,852 KB
testcase_29 AC 34 ms
83,536 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>>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