結果

問題 No.2316 Freight Train
ユーザー tikutikuwatikutikuwa
提出日時 2023-05-26 22:04:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,020 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 172,656 KB
実行使用メモリ 19,044 KB
最終ジャッジ日時 2023-08-26 11:39:15
合計ジャッジ時間 6,764 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

void dfs(vector<vector<int>> &g, int a, int b, vector<bool> &seen){
    seen[a] = true;

    for(int x: g[a]){
        if(seen[x])continue;
        dfs(g, x, b, seen);
    }
}

int main()
{
    int n, q;
    cin >> n >> q;
    vector<vector<int>> g(n, vector<int>());
    rep(i, n)
    {
        int a;
        cin >> a;
        if (a == -1)
        {
            g[i].push_back(i);
            continue;
        }
        g[i].push_back(a - 1);
        g[a - 1].push_back(i);
    }
    rep(i, q)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        vector<bool> seen(n, false);

        dfs(g, a, b, seen);
        if(seen[a] && seen[b]){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
        
    }
}
0