結果

問題 No.1865 Make Cycle
ユーザー yudedakoyudedako
提出日時 2022-03-16 00:12:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 3,000 ms
コード長 1,615 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 143,124 KB
実行使用メモリ 8,304 KB
最終ジャッジ日時 2023-10-23 23:37:08
合計ジャッジ時間 7,116 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
6,792 KB
testcase_01 AC 75 ms
6,000 KB
testcase_02 AC 122 ms
7,056 KB
testcase_03 AC 59 ms
6,264 KB
testcase_04 AC 88 ms
7,028 KB
testcase_05 AC 84 ms
7,236 KB
testcase_06 AC 96 ms
6,900 KB
testcase_07 AC 96 ms
6,792 KB
testcase_08 AC 136 ms
7,584 KB
testcase_09 AC 83 ms
7,204 KB
testcase_10 AC 123 ms
7,320 KB
testcase_11 AC 116 ms
7,212 KB
testcase_12 AC 101 ms
6,528 KB
testcase_13 AC 117 ms
6,792 KB
testcase_14 AC 82 ms
6,264 KB
testcase_15 AC 123 ms
7,156 KB
testcase_16 AC 130 ms
7,584 KB
testcase_17 AC 83 ms
6,404 KB
testcase_18 AC 86 ms
7,392 KB
testcase_19 AC 182 ms
8,304 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <bitset>
#include <fstream>
#include <chrono>


bool is_dag(const std::vector<std::vector<int>>& graph) {
	const int n = graph.size();
	std::vector<bool> visit(n, false), out(n, false);
	std::stack<int> stack;
	for (auto i = 0; i < n; ++i) {
		if (visit[i]) continue;
		stack.push(i);
		while (!stack.empty()) {
			const auto current = stack.top(); stack.pop();
			if (current >= 0) {
				if (visit[current]) {
					if (!out[current]) return false;
					continue;
				}
				visit[current] = true;
				stack.push(-1 - current);
				for (const auto next : graph[current]) {
					stack.push(next);
				}
			}
			else {
				out[-1 - current] = true;
			}
		}
	}
	return true;
}
int main() {
	int n, q; std::cin >> n >> q;
	std::vector<std::pair<int, int>> edges(q);
	for (auto& [a, b] : edges) {
		std::cin >> a >> b; --a; --b;
	}
	int min = 1;
	int max = q + 1;
	while (min < max) {
		auto mid = (min + max) >> 1;
		int e = 0;
		std::vector<std::vector<int>> graph(n);
		while (e < mid && min < max) {
			for (; e < mid; ++e) {
				const auto [a, b] = edges[e];
				graph[a].push_back(b);
			}
			if (is_dag(graph)) {
				min = mid + 1;
			}
			else {
				max = mid;
			}
			mid = (min + max) >> 1;
		}
	}
	if (max <= q) {
		std::cout << max << '\n';
	}
	else {
		std::cout << "-1\n";
	}
}
0