#include <bits/stdc++.h>
#define N 4001
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
int n, m, ans, cnt, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int id[N][N], pai[N*N], dist[N*N];
bool ok[N][N];
char mat[N][N], T;
vector<int> grafo[N*N];
inline void bfs(int x, int y, int &p)
{
queue< pii > fila;
fila.push({x, y});
while(!fila.empty())
{
int x = fila.front().f, y = fila.front().s;
fila.pop();
ok[x][y] = 1;
pai[id[x][y]] = p;
for(int pp = 0; pp < 4; pp++)
{
int a = x + dx[pp], b = y + dy[pp];
if(!a or !b or a > n or b > m or mat[a][b] != mat[x][y] or ok[a][b]) continue;
ok[a][b] = 1;
fila.push({a, b});
}
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>mat[i][j];
id[i][j] = ++cnt;
pai[id[i][j]] = id[i][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(mat[i][j] == '.' or ok[i][j]) continue;
bfs(i, j, id[i][j]);
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(mat[i][j] == '.') continue;
for(int p = 0; p < 4; p++)
{
int a = i + dx[p], b = j + dy[p];
if(!a or !b or a > n or b > m) continue;
if(mat[a][b] != mat[i][j] and mat[a][b] != '.')
{
int u = pai[id[a][b]], v = pai[id[i][j]];
grafo[v].push_back(u);
}
}
}
}
queue<int> fila;
dist[pai[id[1][1]]] = 1;
fila.push(pai[id[1][1]]);
while(!fila.empty())
{
int x = fila.front();
ans = max(ans, dist[x]);
fila.pop();
for(auto v: grafo[x])
{
if(dist[v] == 0)
{
dist[v] = dist[x] + 1;
fila.push(v);
}
}
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
385 ms |
388048 KB |
Output is correct |
2 |
Correct |
348 ms |
388048 KB |
Output is correct |
3 |
Correct |
337 ms |
388048 KB |
Output is correct |
4 |
Correct |
385 ms |
388048 KB |
Output is correct |
5 |
Correct |
354 ms |
388048 KB |
Output is correct |
6 |
Correct |
391 ms |
388048 KB |
Output is correct |
7 |
Correct |
367 ms |
388048 KB |
Output is correct |
8 |
Correct |
376 ms |
388048 KB |
Output is correct |
9 |
Correct |
344 ms |
388048 KB |
Output is correct |
10 |
Correct |
407 ms |
388048 KB |
Output is correct |
11 |
Correct |
362 ms |
388048 KB |
Output is correct |
12 |
Correct |
367 ms |
388048 KB |
Output is correct |
13 |
Correct |
353 ms |
388048 KB |
Output is correct |
14 |
Correct |
353 ms |
388048 KB |
Output is correct |
15 |
Correct |
420 ms |
388928 KB |
Output is correct |
16 |
Correct |
474 ms |
389640 KB |
Output is correct |
17 |
Correct |
387 ms |
389640 KB |
Output is correct |
18 |
Correct |
368 ms |
389640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
424304 KB |
Output is correct |
2 |
Correct |
631 ms |
424304 KB |
Output is correct |
3 |
Correct |
1597 ms |
636216 KB |
Output is correct |
4 |
Correct |
673 ms |
636216 KB |
Output is correct |
5 |
Execution timed out |
2105 ms |
657028 KB |
Time limit exceeded |
6 |
Execution timed out |
2126 ms |
657028 KB |
Time limit exceeded |
7 |
Correct |
390 ms |
657028 KB |
Output is correct |
8 |
Correct |
472 ms |
657028 KB |
Output is correct |
9 |
Correct |
397 ms |
657028 KB |
Output is correct |
10 |
Correct |
344 ms |
657028 KB |
Output is correct |
11 |
Correct |
446 ms |
657028 KB |
Output is correct |
12 |
Correct |
396 ms |
657028 KB |
Output is correct |
13 |
Correct |
602 ms |
657028 KB |
Output is correct |
14 |
Correct |
462 ms |
657028 KB |
Output is correct |
15 |
Correct |
569 ms |
657028 KB |
Output is correct |
16 |
Correct |
431 ms |
657028 KB |
Output is correct |
17 |
Correct |
892 ms |
657028 KB |
Output is correct |
18 |
Correct |
1025 ms |
657028 KB |
Output is correct |
19 |
Correct |
769 ms |
657028 KB |
Output is correct |
20 |
Correct |
675 ms |
657028 KB |
Output is correct |
21 |
Correct |
1099 ms |
657028 KB |
Output is correct |
22 |
Execution timed out |
2092 ms |
694600 KB |
Time limit exceeded |
23 |
Correct |
1328 ms |
694600 KB |
Output is correct |
24 |
Correct |
1183 ms |
694600 KB |
Output is correct |
25 |
Execution timed out |
2086 ms |
694600 KB |
Time limit exceeded |
26 |
Execution timed out |
2082 ms |
694600 KB |
Time limit exceeded |
27 |
Execution timed out |
2080 ms |
694600 KB |
Time limit exceeded |
28 |
Execution timed out |
2115 ms |
694600 KB |
Time limit exceeded |
29 |
Execution timed out |
2124 ms |
694600 KB |
Time limit exceeded |
30 |
Execution timed out |
2095 ms |
694600 KB |
Time limit exceeded |
31 |
Execution timed out |
2093 ms |
694600 KB |
Time limit exceeded |
32 |
Execution timed out |
2114 ms |
694600 KB |
Time limit exceeded |