結果
問題 | No.1865 Make Cycle |
ユーザー | 👑 emthrm |
提出日時 | 2022-03-04 21:28:11 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 186 ms / 3,000 ms |
コード長 | 2,165 bytes |
コンパイル時間 | 2,144 ms |
コンパイル使用メモリ | 206,832 KB |
実行使用メモリ | 8,512 KB |
最終ジャッジ日時 | 2023-09-25 23:53:07 |
合計ジャッジ時間 | 6,355 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 127 ms
6,688 KB |
testcase_01 | AC | 71 ms
5,820 KB |
testcase_02 | AC | 139 ms
6,716 KB |
testcase_03 | AC | 34 ms
6,112 KB |
testcase_04 | AC | 85 ms
7,008 KB |
testcase_05 | AC | 136 ms
7,156 KB |
testcase_06 | AC | 125 ms
6,964 KB |
testcase_07 | AC | 106 ms
6,392 KB |
testcase_08 | AC | 130 ms
7,688 KB |
testcase_09 | AC | 131 ms
7,160 KB |
testcase_10 | AC | 134 ms
7,192 KB |
testcase_11 | AC | 130 ms
7,256 KB |
testcase_12 | AC | 100 ms
6,584 KB |
testcase_13 | AC | 114 ms
6,496 KB |
testcase_14 | AC | 86 ms
6,244 KB |
testcase_15 | AC | 151 ms
7,060 KB |
testcase_16 | AC | 153 ms
7,604 KB |
testcase_17 | AC | 129 ms
6,368 KB |
testcase_18 | AC | 136 ms
7,532 KB |
testcase_19 | AC | 186 ms
8,512 KB |
testcase_20 | AC | 2 ms
4,376 KB |
testcase_21 | AC | 2 ms
4,380 KB |
testcase_22 | AC | 2 ms
4,380 KB |
testcase_23 | AC | 1 ms
4,376 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; std::vector<int> detect_directed_cycle( const std::vector<std::vector<int>>& graph) { const int n = graph.size(); std::vector<int> is_visited(n, 0), cycle; const std::function<bool(int)> dfs = [&graph, &is_visited, &cycle, &dfs](const int ver) -> bool { is_visited[ver] = 1; cycle.emplace_back(ver); for (const int e : graph[ver]) { if (is_visited[e] == 1) { cycle.erase(cycle.begin(), std::find(cycle.begin(), cycle.end(), e)); cycle.emplace_back(e); return true; } else if (is_visited[e] == 0) { if (dfs(e)) return true; } } cycle.pop_back(); is_visited[ver] = 2; return false; }; for (int i = 0; i < n; ++i) { if (is_visited[i] == 0 && dfs(i)) break; } return cycle; } int main() { int n, q; cin >> n >> q; vector<int> a(q), b(q); REP(i, q) cin >> a[i] >> b[i], --a[i], --b[i]; int lb = -1, ub = q; while (ub - lb > 1) { const int mid = (lb + ub) / 2; vector<vector<int>> graph(n); REP(i, mid + 1) graph[a[i]].emplace_back(b[i]); (detect_directed_cycle(graph).empty() ? lb : ub) = mid; } cout << (ub == q ? -1 : ub + 1) << '\n'; return 0; }