結果

問題 No.1194 Replace
ユーザー definedefine
提出日時 2020-08-04 07:10:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 1,776 bytes
コンパイル時間 3,085 ms
コンパイル使用メモリ 205,840 KB
実行使用メモリ 70,220 KB
最終ジャッジ日時 2023-10-12 04:35:12
合計ジャッジ時間 11,987 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 254 ms
60,592 KB
testcase_01 AC 278 ms
61,912 KB
testcase_02 AC 212 ms
58,252 KB
testcase_03 AC 171 ms
55,512 KB
testcase_04 AC 268 ms
62,340 KB
testcase_05 AC 250 ms
60,376 KB
testcase_06 AC 221 ms
59,668 KB
testcase_07 AC 358 ms
70,220 KB
testcase_08 AC 351 ms
69,552 KB
testcase_09 AC 345 ms
69,248 KB
testcase_10 AC 344 ms
69,608 KB
testcase_11 AC 345 ms
69,276 KB
testcase_12 AC 347 ms
69,092 KB
testcase_13 AC 227 ms
56,712 KB
testcase_14 AC 192 ms
55,140 KB
testcase_15 AC 165 ms
53,332 KB
testcase_16 AC 228 ms
56,136 KB
testcase_17 AC 150 ms
52,156 KB
testcase_18 AC 149 ms
53,252 KB
testcase_19 AC 212 ms
57,164 KB
testcase_20 AC 14 ms
40,240 KB
testcase_21 AC 14 ms
40,232 KB
testcase_22 AC 13 ms
40,280 KB
testcase_23 AC 74 ms
47,020 KB
testcase_24 AC 30 ms
43,936 KB
testcase_25 AC 22 ms
40,956 KB
testcase_26 AC 86 ms
48,556 KB
testcase_27 AC 27 ms
41,532 KB
testcase_28 AC 46 ms
44,772 KB
testcase_29 AC 16 ms
42,632 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[200005],C[200005],cnt[400005];
vector<int>xx,G[400005],rG[400005];
bool used[400005];
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[400005],mx[400005];
vector<int>newrG[400005];
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