#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using namespace std;
#define ALL(x) x.begin(), x.end()
using ll = long long;
struct dsu
{
vector<int> p;
dsu(int n) : p(n, -1) {}
int find(int x) { return p[x] < 0 ? x : find(p[x]); }
bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (-p[x] > -p[y]) swap(x, y); p[y] += p[x]; p[x] = y; return 1; }
bool same(int x, int y) { return find(x) == find(y); }
};
#define N 105
int n, m, dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char s[N][N];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
dsu dsu(99999);
cin >> n >> m;
int cmp=0;
for (int i = 1; i <= n; ++i) cin >> (&s[i][1]);
for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j)
{
if (s[i][j]=='*'){
++cmp;
for (int k=0;k<4;++k)
{
int ni=i+dy[k],nj=j+dx[k];
if (s[ni][nj]=='*') cmp-=dsu.unite(i*N+j, ni*N+nj);
}
}
}
cout << cmp;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
832 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
860 KB |
Output is correct |