結果

問題 No.1194 Replace
ユーザー furafura
提出日時 2020-10-05 17:40:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 1,989 bytes
コンパイル時間 2,678 ms
コンパイル使用メモリ 216,272 KB
実行使用メモリ 95,372 KB
最終ジャッジ日時 2023-09-27 04:05:04
合計ジャッジ時間 13,612 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 284 ms
60,448 KB
testcase_01 AC 310 ms
63,384 KB
testcase_02 AC 250 ms
53,512 KB
testcase_03 AC 190 ms
46,672 KB
testcase_04 AC 315 ms
63,816 KB
testcase_05 AC 286 ms
60,020 KB
testcase_06 AC 266 ms
56,784 KB
testcase_07 AC 424 ms
95,324 KB
testcase_08 AC 409 ms
95,216 KB
testcase_09 AC 411 ms
95,372 KB
testcase_10 AC 408 ms
95,328 KB
testcase_11 AC 395 ms
95,216 KB
testcase_12 AC 393 ms
95,272 KB
testcase_13 AC 218 ms
31,956 KB
testcase_14 AC 183 ms
24,744 KB
testcase_15 AC 176 ms
33,980 KB
testcase_16 AC 214 ms
32,968 KB
testcase_17 AC 161 ms
30,672 KB
testcase_18 AC 149 ms
24,212 KB
testcase_19 AC 225 ms
35,524 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 70 ms
17,560 KB
testcase_24 AC 23 ms
9,360 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 73 ms
11,164 KB
testcase_27 AC 14 ms
4,512 KB
testcase_28 AC 35 ms
7,156 KB
testcase_29 AC 5 ms
4,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

using graph=vector<vector<int>>;

void add_directed_edge(graph& G,int u,int v){
	G[u].emplace_back(v);
}

class strongly_connected_components{
	int idx;
	vector<int> top,id;
	const graph& G;
	graph G_rev;

	vector<vector<int>> Comp;
	graph D;

	void dfs1(int u){
		id[u]=0;
		for(int v:G[u]) if(id[v]==-1) dfs1(v);
		top[idx++]=u;
	}

	void dfs2(int u){
		id[u]=idx;
		for(int v:G_rev[u]) if(id[v]==-1) dfs2(v);
	}

public:
	strongly_connected_components(const graph& G):G(G){
		int n=G.size();
		G_rev.resize(n);
		rep(u,n) for(int v:G[u]) G_rev[v].emplace_back(u);

		idx=0;
		id.assign(n,-1);
		top.resize(n);
		rep(u,n) if(id[u]==-1) dfs1(u);

		reverse(top.begin(),top.end());

		idx=0;
		id.assign(n,-1);
		for(int u:top) if(id[u]==-1) dfs2(u), idx++;

		Comp.resize(idx);
		D.resize(idx);
		rep(u,n){
			Comp[id[u]].emplace_back(u);
			for(int v:G[u]) if(id[u]!=id[v]) D[id[u]].emplace_back(id[v]);
		}
	}

	int operator[](int i)const{ return id[i]; }

	const vector<int>& component(int i)const{ return Comp[i]; }
	const graph& DAG()const{ return D; }
};

int main(){
	int N,m; scanf("%d%d",&N,&m);
	vector<int> a(m),b(m);
	vector<int> X;
	rep(i,m){
		scanf("%d%d",&a[i],&b[i]);
		X.emplace_back(a[i]);
		X.emplace_back(b[i]);
	}

	sort(X.begin(),X.end());
	X.erase(unique(X.begin(),X.end()),X.end());

	int n=X.size();
	graph G(n);
	rep(i,m){
		int u=lower_bound(X.begin(),X.end(),a[i])-X.begin();
		int v=lower_bound(X.begin(),X.end(),b[i])-X.begin();
		add_directed_edge(G,u,v);
	}

	strongly_connected_components SCC(G);
	auto D=SCC.DAG();
	int k=D.size();
	vector<int> dp(k);
	for(int i=k-1;i>=0;i--){
		for(int u:SCC.component(i)) dp[i]=max(dp[i],X[u]);
		for(int j:D[i]){
			dp[i]=max(dp[i],dp[j]);
		}
	}

	lint ans=lint(N)*(N+1)/2;
	rep(i,k){
		ans+=SCC.component(i).size()*dp[i];
		for(int u:SCC.component(i)) ans-=X[u];
	}
	printf("%lld\n",ans);

	return 0;
}
0