結果
問題 | No.2316 Freight Train |
ユーザー |
|
提出日時 | 2023-06-01 15:18:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 580 ms / 2,000 ms |
コード長 | 822 bytes |
コンパイル時間 | 1,938 ms |
コンパイル使用メモリ | 174,376 KB |
実行使用メモリ | 28,160 KB |
最終ジャッジ日時 | 2024-12-28 14:59:47 |
合計ジャッジ時間 | 15,260 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h>using namespace std;int main(){int n, q;cin >> n >> q;vector<int> tp;vector<vector<int>> g(n);for (int i = 0; i < n; i++){int j;cin >> j;if (j == -1)tp.push_back(i);elseg[j - 1].push_back(i);}vector<int> tin(n), tout(n);int timer = 0;function<void(int)> dfs = [&](int v){tin[v] = timer++;for (int to : g[v])dfs(to);tout[v] = timer++;};for (int v : tp)dfs(v);while (q--){int u, v;cin >> u >> v;u--, v--;if ((tin[u] <= tin[v] && tout[u] >= tout[v]) || (tin[v] <= tin[u] && tout[v] >= tout[u]))cout << "Yes\n";elsecout << "No\n";}}