結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-13 12:14:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,284 bytes
コンパイル時間 4,303 ms
コンパイル使用メモリ 264,420 KB
実行使用メモリ 21,212 KB
最終ジャッジ日時 2023-09-29 14:47:53
合計ジャッジ時間 8,741 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 17 ms
14,088 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 27 ms
7,184 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 17 ms
14,212 KB
testcase_15 AC 157 ms
18,080 KB
testcase_16 AC 161 ms
18,060 KB
testcase_17 AC 154 ms
21,212 KB
testcase_18 AC 161 ms
18,092 KB
testcase_19 AC 157 ms
18,180 KB
testcase_20 AC 160 ms
18,176 KB
testcase_21 AC 157 ms
18,108 KB
testcase_22 AC 156 ms
18,144 KB
testcase_23 AC 157 ms
18,188 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 16 ms
5,732 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 103 ms
13,936 KB
testcase_28 AC 138 ms
14,868 KB
testcase_29 AC 124 ms
14,816 KB
testcase_30 AC 118 ms
14,136 KB
testcase_31 AC 147 ms
17,404 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 6 ms
4,376 KB
testcase_35 AC 19 ms
4,380 KB
testcase_36 AC 4 ms
4,380 KB
testcase_37 AC 42 ms
6,132 KB
testcase_38 AC 18 ms
10,128 KB
testcase_39 AC 95 ms
14,096 KB
testcase_40 AC 5 ms
4,376 KB
testcase_41 AC 6 ms
4,376 KB
testcase_42 AC 20 ms
4,396 KB
testcase_43 AC 12 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
using namespace std;
using namespace atcoder;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;


int bool_sum(V<bool>&a) {
	int res = 0;
	rep(i, 0, a.size()) {
		if (a[i]) {
			res++;
		}
	}
	return res;
}


VV<int> SCC_to_DAG(int n, VV<int>&route, VV<int>&scc) {
	VV<int> dag(scc.size(), V<int>(0));
	V<int> group(n);
	rep(i, 0, scc.size()) {
		rep(j, 0, scc[i].size()) {
			group[scc[i][j]] = i;
		}
	}

	rep(i, 0, n) {
		rep(j, 0, route[i].size()) {
			int a = group[i];	int b = group[route[i][j]];
			if (a != b) {
				dag[a].push_back(b);
			}
		}
	}

	return dag;
}


int main(void) {
	int n, m;	cin >> n >> m;

	VV<int> route(n, V<int>(0));
	scc_graph ss(n);
	rep(i, 0, m) {
		int a, b;   cin >> a >> b;
		a--;	b--;
		ss.add_edge(a, b);
		route[a].push_back(b);
	}

	VV<int> scc = ss.scc();
	if (scc.size() <= 1) {
		cout << 0 << endl;
		return 0;
	}

	VV<int> dag = SCC_to_DAG(n, route, scc);
	int ds = dag.size();
	V<bool> source(ds, true);	V<bool> sink(ds, true);
	rep(i, 0, ds) {
		rep(j, 0, dag[i].size()) {
			sink[i] = source[dag[i][j]] = false;
		}
	}
	int ans = max(bool_sum(source), bool_sum(sink));

	cout << ans << endl;
	return 0;
}
0