結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 313 ms
105,640 KB
testcase_01 AC 337 ms
107,292 KB
testcase_02 AC 263 ms
103,288 KB
testcase_03 AC 212 ms
98,020 KB
testcase_04 AC 329 ms
107,684 KB
testcase_05 AC 308 ms
105,552 KB
testcase_06 AC 278 ms
104,424 KB
testcase_07 AC 423 ms
117,304 KB
testcase_08 AC 421 ms
116,004 KB
testcase_09 AC 418 ms
116,008 KB
testcase_10 AC 431 ms
116,104 KB
testcase_11 AC 423 ms
116,208 KB
testcase_12 AC 424 ms
117,072 KB
testcase_13 AC 285 ms
97,736 KB
testcase_14 AC 239 ms
96,228 KB
testcase_15 AC 210 ms
94,204 KB
testcase_16 AC 277 ms
97,132 KB
testcase_17 AC 188 ms
93,156 KB
testcase_18 AC 185 ms
93,780 KB
testcase_19 AC 273 ms
97,380 KB
testcase_20 AC 30 ms
83,220 KB
testcase_21 AC 31 ms
83,224 KB
testcase_22 AC 32 ms
83,220 KB
testcase_23 AC 95 ms
87,868 KB
testcase_24 AC 49 ms
84,856 KB
testcase_25 AC 39 ms
83,848 KB
testcase_26 AC 112 ms
89,480 KB
testcase_27 AC 45 ms
84,544 KB
testcase_28 AC 65 ms
85,704 KB
testcase_29 AC 33 ms
83,624 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