結果

問題 No.2319 Friends+
ユーザー shobonvipshobonvip
提出日時 2023-05-26 21:58:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 945 ms / 3,000 ms
コード長 1,734 bytes
コンパイル時間 5,554 ms
コンパイル使用メモリ 261,228 KB
実行使用メモリ 59,052 KB
最終ジャッジ日時 2023-08-26 11:31:00
合計ジャッジ時間 29,654 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 945 ms
59,012 KB
testcase_03 AC 940 ms
59,032 KB
testcase_04 AC 858 ms
58,984 KB
testcase_05 AC 857 ms
59,052 KB
testcase_06 AC 850 ms
58,988 KB
testcase_07 AC 439 ms
7,224 KB
testcase_08 AC 460 ms
7,424 KB
testcase_09 AC 458 ms
7,048 KB
testcase_10 AC 452 ms
7,344 KB
testcase_11 AC 483 ms
7,352 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 428 ms
7,604 KB
testcase_19 AC 428 ms
7,604 KB
testcase_20 AC 434 ms
7,276 KB
testcase_21 AC 425 ms
6,984 KB
testcase_22 AC 434 ms
7,360 KB
testcase_23 AC 418 ms
7,320 KB
testcase_24 AC 428 ms
7,292 KB
testcase_25 AC 421 ms
7,220 KB
testcase_26 AC 408 ms
7,264 KB
testcase_27 AC 422 ms
7,240 KB
testcase_28 AC 419 ms
7,244 KB
testcase_29 AC 468 ms
7,312 KB
testcase_30 AC 444 ms
7,580 KB
testcase_31 AC 429 ms
7,228 KB
testcase_32 AC 434 ms
7,312 KB
testcase_33 AC 431 ms
6,960 KB
testcase_34 AC 424 ms
7,220 KB
testcase_35 AC 413 ms
7,292 KB
testcase_36 AC 440 ms
7,160 KB
testcase_37 AC 598 ms
28,932 KB
testcase_38 AC 416 ms
7,272 KB
testcase_39 AC 432 ms
6,972 KB
testcase_40 AC 441 ms
7,312 KB
testcase_41 AC 432 ms
7,288 KB
testcase_42 AC 444 ms
7,288 KB
testcase_43 AC 441 ms
7,420 KB
testcase_44 AC 443 ms
7,236 KB
testcase_45 AC 644 ms
30,136 KB
testcase_46 AC 564 ms
24,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef modint998244353 mint;
typedef long long ll;

int main(){
	int n, m; cin >> n >> m;

	vector<int> p(n);
	for (int i=0; i<n; i++){
		cin >> p[i];
		p[i]--;
	}

	vector<vector<int>> ikeru(n, vector<int>(0));
	for (int i=0; i<m; i++){
		int a, b;
		cin >> a >> b;
		a--; b--;
		ikeru[a].push_back(b);
		ikeru[b].push_back(a);
	}

	int BORDER = 271;

	vector<int> cnt(n,-1);
	vector<vector<int>> hav_friend;
	vector<int> now_world(n);
	vector<vector<int>> koshin(n, vector<int>(0));
	int nowcnt = 0;

	for (int i=0; i<n; i++){
		now_world[i] = p[i];
	}

	for (int i=0; i<n; i++){
		if ((int)ikeru[i].size() >= BORDER){
			cnt[i] = nowcnt;
			hav_friend.emplace_back(vector<int>(n,0));
			for (int j : ikeru[i]){
				hav_friend[nowcnt][now_world[j]]++;
				koshin[j].push_back(nowcnt);
			}
			nowcnt++;
		}
	}

	int q; cin >> q;
	for (int i=0; i<q; i++){
		int x, y;
		cin >> x >> y;
		x--; y--;
		if (now_world[x] == now_world[y]){
			cout << "No\n";
			continue;
		}
		if (cnt[x] == -1){
			bool mode = false;
			for (int j: ikeru[x]){
				if (now_world[j] == now_world[y]){
					mode = true;
				}
			}
			if (mode){
				cout << "Yes\n";
				for (int k:koshin[x]){
					hav_friend[k][now_world[x]]--;
				}
				now_world[x] = now_world[y];
				for (int k:koshin[x]){
					hav_friend[k][now_world[x]]++;
				}
			}else{
				cout << "No\n";
			}
		}else{
			if (hav_friend[cnt[x]][now_world[y]] > 0){
				cout << "Yes\n";
				for (int k:koshin[x]){
					hav_friend[k][now_world[x]]--;
				}
				now_world[x] = now_world[y];
				for (int k:koshin[x]){
					hav_friend[k][now_world[x]]++;
				}
			}else{
				cout << "No\n";
			}
		}
	}
}
0