AtCoder Beginner Contest 183 F-Confluence
Problem Statement
(N) students are about to go to school. Student (i) belongs to Class (C_i).
After leaving home, each student will head to school while repeatedly joining up with a group of other students. Once students join up with each other, they will not separate.
You will be given (Q) queries that should be processed in order. There are two kinds of queries, in the following formats, that mean the following:
- (1 a b): The group containing Student (a) and the group containing Student (b) merges. (If they are already in the same group, nothing happens.)
- (2 x y): You are asked to find the number of students belonging to Class (y) who are already in the same group as Student (x) (including Student (x)) at the time of this query.
Constraints
- (1leq N leq 2 imes 10^5)
- (1 leq Q leq 2 imes 10^5)
- (1 leq C_i,a,b,x,y leq N)
- In a query in the format (1 a b),(a eq b).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
(N Q)
(C_1 ... C_N)
(Query_1)
(.)
(.)
(.)
(Query_Q)
Output
Print the answers to the queries in the format (2 x y), each in its own line, in order.
Example
input1
5 5
1 2 3 2 1
1 1 2
1 2 5
2 1 1
1 3 4
2 3 4
output1
2
0
Solution
并查集处理学生的合并,用map记录每颗树上所包含对应班级人数。
Code
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 211111;
int fa[maxn], n, q;
unordered_map<int, int> c[maxn];
int find(int x)
{
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx == fy) return;
if(c[fx].size() > c[fy].size()) {
fa[fy] = fx;
for(auto k : c[fy]) {
c[fx][k.first] += k.second;
}
}
else {
fa[fx] = fy;
for(auto k : c[fx]) {
c[fy][k.first] += k.second;
}
}
return;
}
int main()
{
//freopen("in.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
int x; cin >> x;
c[i][x]++;
fa[i] = i;
}
while(q--) {
int d, a, b;
cin >> d >> a >> b;
if(d == 1) {
merge(a, b);
}
else {
int k = find(a);
cout << c[k][b] << endl;
}
}
return 0;
}