結果

問題 No.1194 Replace
ユーザー leaf_1415leaf_1415
提出日時 2020-08-22 16:05:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,128 ms / 2,000 ms
コード長 2,114 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 95,916 KB
実行使用メモリ 106,644 KB
最終ジャッジ日時 2024-04-23 10:19:41
合計ジャッジ時間 19,418 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 744 ms
79,036 KB
testcase_01 AC 802 ms
82,040 KB
testcase_02 AC 624 ms
72,576 KB
testcase_03 AC 527 ms
66,680 KB
testcase_04 AC 815 ms
81,972 KB
testcase_05 AC 746 ms
78,412 KB
testcase_06 AC 690 ms
75,376 KB
testcase_07 AC 1,070 ms
106,548 KB
testcase_08 AC 1,062 ms
106,568 KB
testcase_09 AC 1,078 ms
106,644 KB
testcase_10 AC 1,064 ms
106,548 KB
testcase_11 AC 1,128 ms
106,556 KB
testcase_12 AC 1,097 ms
106,640 KB
testcase_13 AC 666 ms
58,840 KB
testcase_14 AC 568 ms
53,604 KB
testcase_15 AC 481 ms
56,456 KB
testcase_16 AC 621 ms
59,000 KB
testcase_17 AC 430 ms
54,004 KB
testcase_18 AC 423 ms
50,712 KB
testcase_19 AC 645 ms
60,272 KB
testcase_20 AC 15 ms
31,564 KB
testcase_21 AC 16 ms
31,360 KB
testcase_22 AC 15 ms
31,488 KB
testcase_23 AC 186 ms
42,880 KB
testcase_24 AC 68 ms
36,608 KB
testcase_25 AC 30 ms
32,512 KB
testcase_26 AC 206 ms
40,572 KB
testcase_27 AC 37 ms
32,896 KB
testcase_28 AC 97 ms
36,096 KB
testcase_29 AC 20 ms
32,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
map<llint, llint> mp, mp2;
vector<llint> G[400005], revG[400005];
vector<llint> g[400005];
vector<int> topo;
bool used[400005];
int scc[400005];
llint dp[400005];

void tpsort(int v)
{
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++){
		if(!used[G[v][i]]) tpsort(G[v][i]);
	}
	topo.push_back(v);
}
void sccdfs(int v, int id)
{
	used[v] = true;
	scc[v] = id;
	for(int i = 0; i < revG[v].size(); i++){
		if(!used[revG[v][i]]) sccdfs(revG[v][i], id);
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	llint u, v, id = 0;
	for(int i = 1; i <= m; i++){
		cin >> u >> v;
		if(mp.count(u) == 0){
			mp[u] = ++id;
			mp2[id] = u;
		}
		if(mp.count(v) == 0){
			mp[v] = ++id;
			mp2[id] = v;
		}
		G[mp[u]].push_back(mp[v]);
		revG[mp[v]].push_back(mp[u]);
	}
	llint N = id;
	
	for(int i = 1; i <= N; i++) if(!used[i]) tpsort(i);
	reverse(topo.begin(), topo.end());
	
	id = 0;
	for(int i = 1; i <= N; i++) used[i] = false;
	for(int i = 0; i < topo.size(); i++) if(!used[topo[i]]) sccdfs(topo[i], ++id);
	
	for(int i = 1; i <= N; i++){
		chmax(dp[scc[i]], mp2[i]);
		for(int j = 0; j < G[i].size(); j++){
			llint v = G[i][j];
			if(scc[v] != scc[i]) g[scc[i]].push_back(scc[v]);
		}
	}
	
	for(int i = id; i >= 1; i--){
		for(int j = 0; j < g[i].size(); j++){
			chmax(dp[i], dp[g[i][j]]);
		}
	}
	
	llint ans = n * (n+1) / 2;
	for(auto it = mp.begin(); it != mp.end(); it++){
		ans += dp[scc[it->second]] - it->first;
	}
	cout << ans << endl;
	
	return 0;
}
0