結果

問題 No.1194 Replace
ユーザー cn_449cn_449
提出日時 2020-08-22 15:04:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,248 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 1,704 ms
コンパイル使用メモリ 149,604 KB
実行使用メモリ 59,856 KB
最終ジャッジ日時 2023-08-05 12:17:47
合計ジャッジ時間 21,448 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 699 ms
38,272 KB
testcase_01 AC 794 ms
40,404 KB
testcase_02 AC 580 ms
33,564 KB
testcase_03 AC 448 ms
29,168 KB
testcase_04 AC 776 ms
40,520 KB
testcase_05 AC 724 ms
38,012 KB
testcase_06 AC 647 ms
35,676 KB
testcase_07 AC 1,248 ms
59,592 KB
testcase_08 AC 1,230 ms
59,580 KB
testcase_09 AC 1,223 ms
59,640 KB
testcase_10 AC 1,238 ms
59,596 KB
testcase_11 AC 1,199 ms
59,556 KB
testcase_12 AC 1,220 ms
59,856 KB
testcase_13 AC 591 ms
21,944 KB
testcase_14 AC 468 ms
18,148 KB
testcase_15 AC 421 ms
20,972 KB
testcase_16 AC 551 ms
22,344 KB
testcase_17 AC 353 ms
19,092 KB
testcase_18 AC 346 ms
16,660 KB
testcase_19 AC 552 ms
23,252 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 126 ms
11,440 KB
testcase_24 AC 35 ms
7,068 KB
testcase_25 AC 11 ms
4,376 KB
testcase_26 AC 127 ms
9,288 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 52 ms
6,376 KB
testcase_29 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	ll n, m;
	cin >> n >> m;
	map<int, vector<int>> graph;
	set<int> st;
	rep(i, m) {
		int b, c;
		cin >> b >> c;
		graph[c].pb(b);
		st.insert(c);
	}
	map<int, int> val;
	while (st.size() > 0) {
		int s = *st.rbegin();
		queue<int> que;
		que.push(s);
		val[s] = s;
		st.erase(s);
		while (!que.empty()) {
			int p = que.front(); que.pop();
			for (int i : graph[p]) {
				if (val.count(i) == 0) {
					val[i] = s;
					que.push(i);
					st.erase(i);
				}
			}
		}
	}
	ll ans = n * (n + 1) / 2;
	for (auto i : val) {
		ll x = i.first;
		ll y = i.second;
		if (x < y) ans += y - x;
	}
	cout << ans << '\n';
}
0