結果

問題 No.1865 Make Cycle
ユーザー kabipoyokabipoyo
提出日時 2022-03-04 22:03:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 3,000 ms
コード長 1,611 bytes
コンパイル時間 4,251 ms
コンパイル使用メモリ 264,768 KB
実行使用メモリ 11,544 KB
最終ジャッジ日時 2023-09-26 01:03:42
合計ジャッジ時間 9,144 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 184 ms
8,676 KB
testcase_01 AC 110 ms
7,908 KB
testcase_02 AC 233 ms
9,416 KB
testcase_03 AC 67 ms
8,416 KB
testcase_04 AC 131 ms
9,776 KB
testcase_05 AC 170 ms
9,884 KB
testcase_06 AC 168 ms
9,184 KB
testcase_07 AC 171 ms
8,496 KB
testcase_08 AC 216 ms
10,784 KB
testcase_09 AC 164 ms
9,896 KB
testcase_10 AC 191 ms
10,292 KB
testcase_11 AC 174 ms
9,812 KB
testcase_12 AC 154 ms
9,196 KB
testcase_13 AC 188 ms
8,588 KB
testcase_14 AC 117 ms
7,800 KB
testcase_15 AC 205 ms
9,948 KB
testcase_16 AC 213 ms
10,236 KB
testcase_17 AC 154 ms
8,436 KB
testcase_18 AC 164 ms
10,484 KB
testcase_19 AC 249 ms
11,544 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;

vi A, B;

vi topo_sort(int N, int M) {
	vector<vector<int>> G(N);
	int x, y;
	rep(i, M) {
		x = A[i], y = B[i];
		G[x].push_back(y);
	}
	vi res;
	vi v(N);
	rep(i, N) { for (auto e : G[i]) v[e]++; }
	queue<int> Q;
	rep(i, N) { if (!v[i]) Q.push(i); }
	while (!Q.empty()) {
		int x = Q.front(); Q.pop();
		res.push_back(x);
		for (auto e : G[x]) {
			v[e]--;
			if (!v[e]) Q.push(e);
		}
	}
	return res;
}

int main() {
	lint N, Q;
	cin >> N >> Q;

	lint a=0, b=Q+1, c=0, x, y, z;
	rep(i, Q) {
		cin >> x >> y;
		A.pb(x-1), B.pb(y-1);
	}

	while (b-a > 1) {
		c = (a+b)/2;
		vi v = topo_sort(N, c);
		if (v.size() == N) a = c;
		else b = c;
	}
	if (b == Q+1) std::cout << -1 << '\n';
	else std::cout << b << '\n';
}
0