結果

問題 No.1194 Replace
ユーザー chocopuuchocopuu
提出日時 2020-08-22 16:27:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,110 bytes
コンパイル時間 2,780 ms
コンパイル使用メモリ 206,916 KB
実行使用メモリ 817,152 KB
最終ジャッジ日時 2024-04-23 10:33:46
合計ジャッジ時間 6,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
	  }
	}
  }
};

signed main(){
	int N, M;
	cin >> N >> M;
	vector<vector<int>>g(N);
	REP(i, M) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		g[a].emplace_back(b);
	}
	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]], i + 1);
		++cnt[scc[i]];
	}
	vector<int>dp(N);
	REP(i, N) dp[i] = ma[scc[i]];
	vector<int>visit(N);
	{
		auto dfs = [&](auto&&f, int now) -> int {
			visit[now] = 1;
			for(auto e: g[now]) {
				if(visit[e]) {
					CHMAX(dp[now], dp[e]);
				} else {
					CHMAX(dp[now], f(f, e));
				}
			}
			return dp[now];
		};
		REP(i, N) {
			if(visit[i] == 0) dfs(dfs, i);
		}
	}
	//REP(i, N) out(i, dp[i], scc[i]);
	out(accumulate(ALL(dp), 0ll));
}
0