結果

問題 No.2316 Freight Train
ユーザー shobonvipshobonvip
提出日時 2023-05-26 21:26:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 846 bytes
コンパイル時間 3,872 ms
コンパイル使用メモリ 260,356 KB
実行使用メモリ 4,788 KB
最終ジャッジ日時 2023-08-26 10:08:35
合計ジャッジ時間 14,793 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 392 ms
4,788 KB
testcase_04 AC 213 ms
4,380 KB
testcase_05 AC 149 ms
4,384 KB
testcase_06 AC 50 ms
4,376 KB
testcase_07 AC 343 ms
4,376 KB
testcase_08 AC 242 ms
4,608 KB
testcase_09 AC 287 ms
4,380 KB
testcase_10 AC 326 ms
4,376 KB
testcase_11 AC 274 ms
4,376 KB
testcase_12 AC 335 ms
4,448 KB
testcase_13 AC 395 ms
4,672 KB
testcase_14 AC 389 ms
4,648 KB
testcase_15 AC 402 ms
4,648 KB
testcase_16 AC 398 ms
4,600 KB
testcase_17 AC 409 ms
4,628 KB
testcase_18 AC 405 ms
4,664 KB
testcase_19 AC 394 ms
4,612 KB
testcase_20 AC 402 ms
4,628 KB
testcase_21 AC 404 ms
4,656 KB
testcase_22 AC 406 ms
4,608 KB
testcase_23 AC 389 ms
4,632 KB
testcase_24 AC 402 ms
4,708 KB
testcase_25 AC 378 ms
4,600 KB
testcase_26 AC 363 ms
4,584 KB
testcase_27 AC 296 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint998244353 mint;
typedef long long ll;

struct UnionFind{
	int n;
	vector<int> par;

	explicit UnionFind(int v): n(v), par(v,-1) {}

	void merge(int a, int b){
		int x = find(a);
		int y = find(b);
		if (x == y) return;
		if (-par[x] < -par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return;
	}

	int find(int x){
		if (par[x] < 0) return x;
		return par[x] = find(par[x]);
	}
};

int main(){
	int n, q; cin >> n >> q;
	UnionFind uf(n);
	vector<int> p(n);
	for (int i=0; i<n; i++){
		cin >> p[i];
	}
	for (int i=0; i<n; i++){
		if (p[i] != -1){
			uf.merge(p[i]-1, i);
		}
	}
	for (int i=0; i<q; i++){
		int a, b; cin >> a >> b;
		a--; b--;
		if (uf.find(a) == uf.find(b)){
			cout << "Yes\n";
		}else{
			cout << "No\n";
		}
	}
}
0