結果

問題 No.2316 Freight Train
ユーザー akasinn
提出日時 2023-05-26 22:05:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 871 ms / 2,000 ms
コード長 658 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 176,460 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-12-25 07:07:47
合計ジャッジ時間 21,032 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

//yukicoder390C
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;

int main(){
  int n,q; cin>>n>>q;
  map<int,int> Mp;//前ー後
  vector<int> Head(0);//先頭の番号
  for(int i=1;i<=n;i++){
    int p; cin>>p;
    if(p==-1){
      Head.push_back(i);
    }else{
      Mp[p]=i;
    }
  }
  vector<int> Belong(n+1,0);//先頭の番号を記録
  for(int head:Head){
    int h=head;
    Belong.at(h)=head;
    while(Mp.count(h)){
      h=Mp[h];
      Belong.at(h)=head;
    }
  }
  while(q--){
    int a,b; cin>>a>>b;
    if(Belong.at(a)==Belong.at(b)){
      cout<<"Yes"<<endl;
    }else{
      cout<<"No"<<endl;
    }
    
  }
  return 0;
}
0