結果

問題 No.1865 Make Cycle
ユーザー yudedakoyudedako
提出日時 2022-03-16 00:13:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 3,000 ms
コード長 1,537 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 143,024 KB
実行使用メモリ 8,328 KB
最終ジャッジ日時 2023-10-23 23:37:49
合計ジャッジ時間 5,891 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
6,728 KB
testcase_01 AC 94 ms
5,936 KB
testcase_02 AC 171 ms
6,788 KB
testcase_03 AC 61 ms
6,248 KB
testcase_04 AC 113 ms
7,012 KB
testcase_05 AC 148 ms
7,052 KB
testcase_06 AC 140 ms
6,844 KB
testcase_07 AC 132 ms
6,552 KB
testcase_08 AC 188 ms
7,664 KB
testcase_09 AC 140 ms
6,996 KB
testcase_10 AC 155 ms
7,340 KB
testcase_11 AC 152 ms
7,012 KB
testcase_12 AC 124 ms
6,732 KB
testcase_13 AC 142 ms
6,768 KB
testcase_14 AC 103 ms
6,136 KB
testcase_15 AC 171 ms
7,216 KB
testcase_16 AC 179 ms
7,448 KB
testcase_17 AC 142 ms
6,524 KB
testcase_18 AC 146 ms
7,284 KB
testcase_19 AC 230 ms
8,328 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 2 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;
		std::vector<std::vector<int>> graph(n);
		for (int e = 0; e < mid; ++e) {
			const auto [a, b] = edges[e];
			graph[a].push_back(b);
		}
		if (is_dag(graph)) {
			min = mid + 1;
		}
		else {
			max = mid;
		}
	}
	if (max <= q) {
		std::cout << max << '\n';
	}
	else {
		std::cout << "-1\n";
	}
}
0