結果
| 問題 |
No.2316 Freight Train
|
| コンテスト | |
| ユーザー |
hatsuka_iwa
|
| 提出日時 | 2023-07-02 09:53:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 469 ms / 2,000 ms |
| コード長 | 603 bytes |
| コンパイル時間 | 1,806 ms |
| コンパイル使用メモリ | 169,804 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-16 06:17:25 |
| 合計ジャッジ時間 | 15,756 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, Q; cin >> N >> Q;
vector<int> P(N + 1), root(N + 1, -1);
auto dfs = [&](auto dfs, int x) -> int {
if (root.at(x) != -1) return root.at(x);
return root.at(x) = dfs(dfs, P.at(x));
};
for (int i = 1; i <= N; i++) {
cin >> P.at(i);
if (P.at(i) == -1) root.at(i) = i;
}
for (int i = 1; i <= N; i++) {
if (root.at(i) != -1) continue;
root.at(i) = dfs(dfs, P.at(i));
}
for (int i = 0; i < Q; i++) {
int A, B; cin >> A >> B;
cout << (root.at(A) == root.at(B) ? "Yes" : "No") << endl;
}
}
hatsuka_iwa