#include "bits/stdc++.h"
using namespace std;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
int h, w;
void dfs(int x, int y, int comp, vector<vector<int>> &visited, vector<vector<char>> &v, set<char> &s, int &animal) {
if (x < 0 || x >= h || y < 0 || y >= w || visited[x][y] || v[x][y] == '.') {
return;
}
visited[x][y] = comp;
s.insert(v[x][y]);
animal = max(animal, (int)s.size());
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
dfs(nx, ny, comp, visited, v, s, animal);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> h >> w;
vector<vector<int>> visited(h, vector<int>(w, 0));
vector<vector<char>> v(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> v[i][j];
}
}
int comp = 0;
int ans = 0; // To store the total sum of distinct animals in all components
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!visited[i][j] && v[i][j] != '.') {
set<char> s;
int animal = 0;
dfs(i, j, ++comp, visited, v, s, animal);
ans += animal; // Summing up the distinct animals in each component
}
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
9564 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
15 ms |
15708 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
1368 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
2140 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
4468 KB |
Output isn't correct |
12 |
Incorrect |
6 ms |
3164 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
1372 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
1372 KB |
Output isn't correct |
15 |
Incorrect |
13 ms |
12152 KB |
Output isn't correct |
16 |
Incorrect |
14 ms |
9564 KB |
Output isn't correct |
17 |
Incorrect |
11 ms |
6236 KB |
Output isn't correct |
18 |
Incorrect |
14 ms |
15708 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2140 KB |
Output isn't correct |
2 |
Incorrect |
58 ms |
42052 KB |
Output isn't correct |
3 |
Incorrect |
438 ms |
351268 KB |
Output isn't correct |
4 |
Incorrect |
95 ms |
42320 KB |
Output isn't correct |
5 |
Incorrect |
479 ms |
476240 KB |
Output isn't correct |
6 |
Runtime error |
745 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Incorrect |
2 ms |
1628 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
2140 KB |
Output isn't correct |
9 |
Incorrect |
3 ms |
2272 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
1372 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
13 |
Incorrect |
68 ms |
42100 KB |
Output isn't correct |
14 |
Incorrect |
33 ms |
24668 KB |
Output isn't correct |
15 |
Incorrect |
42 ms |
33104 KB |
Output isn't correct |
16 |
Incorrect |
32 ms |
22360 KB |
Output isn't correct |
17 |
Incorrect |
149 ms |
102692 KB |
Output isn't correct |
18 |
Incorrect |
173 ms |
127056 KB |
Output isn't correct |
19 |
Incorrect |
96 ms |
41700 KB |
Output isn't correct |
20 |
Incorrect |
100 ms |
77652 KB |
Output isn't correct |
21 |
Incorrect |
251 ms |
204208 KB |
Output isn't correct |
22 |
Incorrect |
458 ms |
474104 KB |
Output isn't correct |
23 |
Incorrect |
281 ms |
209488 KB |
Output isn't correct |
24 |
Incorrect |
341 ms |
333508 KB |
Output isn't correct |
25 |
Incorrect |
745 ms |
519192 KB |
Output isn't correct |
26 |
Runtime error |
704 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
766 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
718 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
736 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
778 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Incorrect |
589 ms |
377252 KB |
Output isn't correct |
32 |
Incorrect |
840 ms |
454152 KB |
Output isn't correct |