吊打BC
2024-12-04 22:26:29
发布于:广东
42阅读
0回复
0点赞
BC说这题是广搜,要不是我用冰红茶解出来了,我差点就信了
#include <iostream>
#include <cstdio>
using namespace std;
char a[1005][1005];
int father[1000005];
int size[1000005];
int n, m;
int find(int n){
return (father[n] != n ? father[n] = find(father[n]) : n);
}
void merge(int x, int y){//合并
int n = find(x), m = find(y);
if(n != m){
father[max(n, m)] = min(n, m);
size[min(n, m)] += size[max(n, m)];//加上原联通块的大小
}
}
inline int turn(int x, int y){//将行列转换成对应的下标
return x * n + y - n;
}
int main(){
cin.tie(nullptr) -> sync_with_stdio(0);
cout.tie(nullptr) -> sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n * n; i++) father[i] = i, size[i] = 1;
for(int i = 1; i <= n; i++){
cin >> a[i] + 1;
for(int j = 1; j <= n; j++){
if(j != 1 && a[i][j] != a[i][j - 1]) merge(turn(i, j), turn(i, j - 1));
if(i != 1 && a[i][j] != a[i - 1][j]) merge(turn(i, j), turn(i - 1, j));//满足条件就合并
}
}
while(m--){
int x, y;
cin >> x >> y;
cout << size[find(turn(x, y))] << '\n';
}
return 0;
}
这里空空如也
有帮助,赞一个