結果

問題 No.2319 Friends+
ユーザー 00 Sakuda
提出日時 2024-04-04 18:08:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 741 bytes
コンパイル時間 2,116 ms
コンパイル使用メモリ 206,832 KB
最終ジャッジ日時 2025-02-20 20:05:48
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
using namespace atcoder;
int main() {
	int N, M;cin >> N >> M;
	dsu uf(N);
	vector<int> P(N);
	for (int i = 0;i < N;i++) cin >> P[i];
	for (int i = 0;i < M;i++) {
		int a, b;cin >> a >> b;
		a--;b--;
		uf.merge(a, b);
	}
	int Q;cin >> Q;
	vector<multiset<int>> C(N + 1);
	for (int v = 0;v < N;v++) C[P[v]].insert(uf.leader(v));
	while (Q--) {
		int x, y;cin >> x >> y;x--;y--;
		if (P[x] == P[y]) {
			cout << "No" << endl;
			continue;
		}
		int rx = uf.leader(x);
		int px = P[x];int py = P[y];
		if (C[py].find(rx) == C[py].end()) {
			cout << "No" << endl;
			continue;
		}
		C[py].insert(rx);
		C[px].erase(C[px].find(rx));
		P[x] = py;
		cout << "Yes" << endl;
	}
}
0