#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 << comp << " " << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
9560 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
14 ms |
15812 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
1484 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
2140 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
4444 KB |
Output isn't correct |
12 |
Incorrect |
5 ms |
3284 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
1492 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
1484 KB |
Output isn't correct |
15 |
Incorrect |
14 ms |
12232 KB |
Output isn't correct |
16 |
Incorrect |
15 ms |
9560 KB |
Output isn't correct |
17 |
Incorrect |
12 ms |
6232 KB |
Output isn't correct |
18 |
Incorrect |
15 ms |
15708 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2136 KB |
Output isn't correct |
2 |
Incorrect |
59 ms |
42216 KB |
Output isn't correct |
3 |
Incorrect |
432 ms |
351056 KB |
Output isn't correct |
4 |
Incorrect |
104 ms |
42324 KB |
Output isn't correct |
5 |
Incorrect |
480 ms |
475988 KB |
Output isn't correct |
6 |
Runtime error |
824 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Incorrect |
2 ms |
2136 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
2140 KB |
Output isn't correct |
9 |
Incorrect |
3 ms |
2140 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
1312 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
1624 KB |
Output isn't correct |
13 |
Incorrect |
56 ms |
42236 KB |
Output isn't correct |
14 |
Incorrect |
32 ms |
24492 KB |
Output isn't correct |
15 |
Incorrect |
39 ms |
33108 KB |
Output isn't correct |
16 |
Incorrect |
27 ms |
22360 KB |
Output isn't correct |
17 |
Incorrect |
142 ms |
103504 KB |
Output isn't correct |
18 |
Incorrect |
159 ms |
127496 KB |
Output isn't correct |
19 |
Incorrect |
93 ms |
42324 KB |
Output isn't correct |
20 |
Incorrect |
105 ms |
77916 KB |
Output isn't correct |
21 |
Incorrect |
273 ms |
206164 KB |
Output isn't correct |
22 |
Incorrect |
473 ms |
476028 KB |
Output isn't correct |
23 |
Incorrect |
282 ms |
211060 KB |
Output isn't correct |
24 |
Incorrect |
364 ms |
335416 KB |
Output isn't correct |
25 |
Incorrect |
740 ms |
522576 KB |
Output isn't correct |
26 |
Runtime error |
716 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
743 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
777 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
760 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
771 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Incorrect |
590 ms |
379220 KB |
Output isn't correct |
32 |
Incorrect |
829 ms |
457552 KB |
Output isn't correct |