大家好,我是小码匠,今天分享上周末AtCoder线上赛-ABC329的F题。
离自己的既定目标:
官方原题:
6 5
1 1 1 2 2 3
1 2
6 4
5 1
3 6
4 6
1
2
1
1
3
5 3
2 4 2 4 2
3 1
2 5
3 2
1
2
0
里面的数现在要合并到
里面,合并操作完成后,
里面包含了
的数,但
里面的数仍然存在,所以还要清除
合并到
里面,当
里面的数太多时,容易超时,所以我们一定是将数少的一方合并到数多的一方
里的数合并到
里面,但现在数都在
里,所以我们还要在实行一步交换
#include <bits/stdc++.h>
using namespace std;
//bitset<200001> g[200001];
void best_coder() {
int n, q;
cin >> n >> q;
vector<set<int>> g(n);
vector<int> l(n, 1);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
g[i].insert(a);
}
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
if (l[x]) {
g[y].insert(g[x].begin(), g[x].end());
g[x].erase(g[x].begin(), g[x].end());
l[x] = 0;
l[y] = g[y].size();
}
cout << l[y] << endl;
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void best_coder() {
int n, q;
cin >> n >> q;
vector<set<int>> g(n);
vector<int> l(n, 1);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
g[i].insert(a);
}
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
if (l[x] < l[y]) {
g[y].insert(g[x].begin(), g[x].end());
g[x].clear();
l[x] = 0;
l[y] = g[y].size();
} else {
g[x].insert(g[y].begin(), g[y].end());
g[y].clear();
l[x] = 0;
l[y] = g[x].size();
swap(g[x], g[y]);
}
cout << l[y] << '\n';
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
return 0;
}
END