結果
| 問題 |
No.1865 Make Cycle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-03-04 21:05:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,162 bytes |
| コンパイル時間 | 2,060 ms |
| コンパイル使用メモリ | 203,192 KB |
| 最終ジャッジ日時 | 2025-01-28 04:41:49 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 1 TLE * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class GraphCycle {
private:
const vector<vector<int>>& G;
std::vector<bool> visited, seen;
bool result;
bool dfs(int v) {
if (seen[v]) return true;
if (visited[v]) return false;
visited[v] = seen[v] = true;
for (const auto& e : G[v]) {
if (dfs(e)) return true;
}
seen[v] = false;
return false;
}
void init() {
const int N = G.size();
visited.assign(N, false);
seen.assign(N, false);
result = false;
for (int i = 0; i < N; ++i) {
if (dfs(i)) {
result = true;
break;
}
}
}
public:
GraphCycle(const vector<vector<int>>& G) : G(G) { init(); }
bool has_cycle() const { return result; }
};
int main() {
int N, Q; cin >> N >> Q;
vector<vector<int>> G(N);
for (int i = 0; i < Q; ++i) {
int a, b; cin >> a >> b;
G[a - 1].push_back(b - 1);
GraphCycle C(G);
if (C.has_cycle()) {
cout << i + 1 << endl;
return 0;
}
}
puts("-1");
}