結果
問題 | No.2319 Friends+ |
ユーザー |
![]() |
提出日時 | 2023-05-26 21:58:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,169 ms / 3,000 ms |
コード長 | 1,734 bytes |
コンパイル時間 | 4,782 ms |
コンパイル使用メモリ | 254,232 KB |
最終ジャッジ日時 | 2025-02-13 06:39:09 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 45 |
ソースコード
#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"; } } } }