結果

問題 No.2316 Freight Train
ユーザー ttkkggww
提出日時 2023-05-26 21:31:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 639 bytes
コンパイル時間 5,096 ms
コンパイル使用メモリ 254,164 KB
最終ジャッジ日時 2025-02-13 05:55:52
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int n,q;
vector<int> P;
vector<int> A,B;

void solve(){
	dsu UF(n);
	for(int i = 0;i<n;i++){
		if(P[i]<0)continue;
		UF.merge(i,P[i]);
	}
	for(int i = 0;i<q;i++){
		if(UF.same(A[i],B[i])){
			cout<<"Yes"<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> q;
	P = vector<int>(n);
	for(auto &i:P)cin >> i;
	for(auto &i:P)i--;
	A = B = vector<int>(q);
	for(int i = 0;i<q;i++){
		cin >> A[i] >> B[i];
	}
	for(auto &i:A)i--;
	for(auto &i:B)i--;
	solve();
}
0