結果

問題 No.1194 Replace
ユーザー chocopuuchocopuu
提出日時 2020-08-22 16:55:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 553 ms / 2,000 ms
コード長 4,167 bytes
コンパイル時間 2,533 ms
コンパイル使用メモリ 218,216 KB
実行使用メモリ 94,568 KB
最終ジャッジ日時 2023-08-05 13:50:58
合計ジャッジ時間 12,511 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 358 ms
61,852 KB
testcase_01 AC 391 ms
65,572 KB
testcase_02 AC 301 ms
55,140 KB
testcase_03 AC 256 ms
46,624 KB
testcase_04 AC 389 ms
65,320 KB
testcase_05 AC 348 ms
61,492 KB
testcase_06 AC 327 ms
57,288 KB
testcase_07 AC 532 ms
93,588 KB
testcase_08 AC 528 ms
94,048 KB
testcase_09 AC 529 ms
94,568 KB
testcase_10 AC 529 ms
94,076 KB
testcase_11 AC 528 ms
94,412 KB
testcase_12 AC 553 ms
93,800 KB
testcase_13 AC 340 ms
40,028 KB
testcase_14 AC 287 ms
32,604 KB
testcase_15 AC 237 ms
35,952 KB
testcase_16 AC 319 ms
39,456 KB
testcase_17 AC 217 ms
32,568 KB
testcase_18 AC 218 ms
29,016 KB
testcase_19 AC 318 ms
41,304 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 89 ms
17,792 KB
testcase_24 AC 28 ms
9,304 KB
testcase_25 AC 14 ms
4,700 KB
testcase_26 AC 111 ms
15,160 KB
testcase_27 AC 21 ms
5,172 KB
testcase_28 AC 52 ms
9,284 KB
testcase_29 AC 6 ms
4,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>istream&operator >>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
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 INF = 1e18;

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
	to = x;
	return *this;
  }

  operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
	for(int i = 0; i < g.size(); i++) {
	  for(auto e : g[i]) {
		gg[i].emplace_back((int) e);
		rg[(int) e].emplace_back(i);
	  }
	}
  }

  int operator[](int k) {
	return comp[k];
  }

  void dfs(int idx) {
	if(used[idx]) return;
	used[idx] = true;
	for(int to : gg[idx]) dfs(to);
	order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
	if(comp[idx] != -1) return;
	comp[idx] = cnt;
	for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
	for(int i = 0; i < gg.size(); i++) dfs(i);
	reverse(begin(order), end(order));
	int ptr = 0;
	for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

	t.resize(ptr);
	for(int i = 0; i < g.size(); i++) {
	  for(auto &to : g[i]) {
		int x = comp[i], y = comp[to];
		if(x == y) continue;
		t[x].push_back(y);
	  }
	}
  }
};
template< typename T >
struct Compress {
  vector< T > xs;

  Compress() = default;

  Compress(const vector< T > &vs) {
	add(vs);
  }

  Compress(const initializer_list< vector< T > > &vs) {
	for(auto &p : vs) add(p);
  }

  void add(const vector< T > &vs) {
	copy(begin(vs), end(vs), back_inserter(xs));
  }

  void add(const T &x) {
	xs.emplace_back(x);
  }

  void build() {
	sort(begin(xs), end(xs));
	xs.erase(unique(begin(xs), end(xs)), end(xs));
  }

  vector< int > get(const vector< T > &vs) const {
	vector< int > ret;
	transform(begin(vs), end(vs), back_inserter(ret), [&](const T &x) {
	  return lower_bound(begin(xs), end(xs), x) - begin(xs);
	});
	return ret;
  }

  int get(const T &x) const {
	return lower_bound(begin(xs), end(xs), x) - begin(xs);
  }

  const T &operator[](int k) const {
	return xs[k];
  }
};

signed main(){
	int N, M;
	cin >> N >> M;
	int n = N;
	vector<int>a(M), b(M);
	REP(i, M) {
		cin >> a[i] >> b[i];
	}
	Compress<int> c;
	c.add(a);
	c.add(b);
	c.build();
	a = c.get(a);
	b = c.get(b);
	N = c.xs.size();
	vector<vector<int>>g(N);
	REP(i, M) {
		g[a[i]].emplace_back(b[i]);
	}
	StronglyConnectedComponents scc(g);
	vector<vector<int>>buff;
	scc.build(buff);
	vector<int>ma(N);
	vector<int>cnt(N);
	REP(i, N) {
		CHMAX(ma[scc[i]], c[i]);
		++cnt[scc[i]];
	}
	vector<int>dp(buff.size());
	REP(i, N) dp[scc[i]] = ma[scc[i]];
	vector<int>visit(buff.size());
	{
		auto dfs = [&](auto&&f, int now) -> int {
			visit[now] = 1;
			for(auto e: buff[now]) {
				if(visit[e]) {
					CHMAX(dp[now], dp[e]);
				} else {
					CHMAX(dp[now], f(f, e));
				}
			}
			return dp[now];
		};
		REP(i, buff.size()) {
			if(visit[i] == 0) dfs(dfs, i);
		}
	}

	int ans = n * (n + 1) / 2;
	REP(i, N) ans -= c.xs[i];
	REP(i, buff.size()) {
		ans += dp[i] * cnt[i];
	}
	out(ans);
}
0