結果

問題 No.1194 Replace
ユーザー definedefine
提出日時 2020-08-04 07:11:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 390 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 205,832 KB
実行使用メモリ 59,584 KB
最終ジャッジ日時 2023-10-12 04:37:59
合計ジャッジ時間 9,935 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 270 ms
52,120 KB
testcase_01 AC 287 ms
53,160 KB
testcase_02 AC 225 ms
50,012 KB
testcase_03 AC 183 ms
47,812 KB
testcase_04 AC 291 ms
53,172 KB
testcase_05 AC 262 ms
51,780 KB
testcase_06 AC 240 ms
50,856 KB
testcase_07 AC 390 ms
59,584 KB
testcase_08 AC 377 ms
59,564 KB
testcase_09 AC 371 ms
59,512 KB
testcase_10 AC 378 ms
59,560 KB
testcase_11 AC 377 ms
59,584 KB
testcase_12 AC 380 ms
59,564 KB
testcase_13 AC 237 ms
48,348 KB
testcase_14 AC 192 ms
46,552 KB
testcase_15 AC 167 ms
46,036 KB
testcase_16 AC 225 ms
48,092 KB
testcase_17 AC 160 ms
45,268 KB
testcase_18 AC 150 ms
44,648 KB
testcase_19 AC 228 ms
48,348 KB
testcase_20 AC 15 ms
36,152 KB
testcase_21 AC 14 ms
36,152 KB
testcase_22 AC 15 ms
36,136 KB
testcase_23 AC 73 ms
40,596 KB
testcase_24 AC 33 ms
37,776 KB
testcase_25 AC 22 ms
36,728 KB
testcase_26 AC 84 ms
40,992 KB
testcase_27 AC 26 ms
37,136 KB
testcase_28 AC 46 ms
38,324 KB
testcase_29 AC 17 ms
36,460 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;
long long 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=(long long)(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+=(long long)mx[i]*cnt[i];
        for(int j:newrG[i])chmax(mx[j],mx[i]);
    }
    cout<<Ans<<endl;
}
0