結果

問題 No.2316 Freight Train
ユーザー zezerozezero
提出日時 2023-05-26 21:48:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 831 bytes
コンパイル時間 3,006 ms
コンパイル使用メモリ 157,308 KB
実行使用メモリ 813,692 KB
最終ジャッジ日時 2023-08-26 11:08:52
合計ジャッジ時間 6,268 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 MLE -
testcase_03 -- -
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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <cstring>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < int(n);i++)

vector<int> par;
int find(int x){
   if (par[x] == -x){
      return x;
   }
   else{
      return par[x] = find(par[x]); 
   }
}

int main(){
  int n,q;
  cin >> n >> q;
  par.resize(n);
  vector<int> a(n);
  rep(i,n){
    int x;
    cin >> x;
    if (x == -1){
      par[i] = -i;
    }
    else par[i] = x-1;
  }
  
  rep(i,n){
    par[i] = find(i);
  }
 
  rep(i,q){
    int a,b;
    cin >> a >> b;
    if (par[a-1] == par[b-1]){
      cout << "Yes\n";
    }
    else cout << "No\n";
  }
  

  return 0;
}
0