結果
問題 | No.1023 Cyclic Tour |
ユーザー |
![]() |
提出日時 | 2023-06-12 13:35:51 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,502 bytes |
コンパイル時間 | 1,240 ms |
コンパイル使用メモリ | 130,844 KB |
実行使用メモリ | 37,504 KB |
最終ジャッジ日時 | 2024-06-11 15:44:26 |
合計ジャッジ時間 | 31,534 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 28 WA * 21 |
ソースコード
#include <iostream> #include <stack> #include <string.h> #include <vector> #include <map> using namespace std; typedef vector <vector<int>> Graph; const int MAX_N = 100000; int root; bool visited[MAX_N]; bool finished[MAX_N]; void dfs(int u, int parent, const Graph &G, stack<int> &path) { visited[u] = true; path.push(u); for (int v : G[u]) { if (v == parent) continue; if (finished[v]) continue; if (visited[v] && !finished[v]) { root = v; return; } dfs(v, u, G, path); if (root != -1) return; } path.pop(); finished[u] = true; } vector<int> cycle_detection(int start, const Graph &G) { root = -1; memset(visited, false, sizeof(visited)); memset(finished, false, sizeof(finished)); stack<int> path; dfs(start, -1, G, path); vector<int> res; while (!path.empty()) { res.push_back(path.top()); path.pop(); } return res; } int main() { int N, M; cin >> N >> M; Graph G(N + 1); map<int, map<int, int>> E; bool ok = false; int a, b, c; for (int i = 0; i < M; ++i) { cin >> a >> b >> c; G[a].push_back(b); if (c == 1) { G[b].push_back(a); E[a][b] += 1; E[b][a] += 1; } else { E[a][b] += 2; } if (E[a][b] >= 2 && E[b][a] >= 2) { ok = true; } } vector<int> path = cycle_detection(1, G); if (ok) { cout << "Yes" << endl; } else if (path.empty()) { cout << "No" << endl; } else { cout << "Yes" << endl; } return 0; }