#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using ll = long long int;
using ull = unsigned long long int;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = LONG_LONG_MAX;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string moves = "URDL";
int main()
{
//tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> q;
/*freopen("problemname.in", "r", stdin);
freopen("problemname.out", "w", stdout);*/
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
char a[n][m];
for(int i = 0;i<n;++i) {
for(int j = 0;j<m;++j) {
cin >> a[i][j];
}
}
bool vis[n][m];
int cc[n][m], c = 0;
memset(cc, 0, sizeof cc);
memset(vis, false, sizeof vis);
for(int i = 0;i<n;++i) {
for(int j = 0;j<m;++j) {
if (!vis[i][j] && a[i][j] != '.') {
queue<pii> q;
q.push({i, j});
++c;
vis[i][j] = true;
cc[i][j] = c;
while(!q.empty()) {
pii node = q.front();
q.pop();
for(int k = 0;k<4;++k) {
int nx = node.fi + dx[k], ny = node.se + dy[k];
if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
if (a[nx][ny] == a[i][j] && !vis[nx][ny]) {
q.push({nx, ny});
vis[nx][ny] = true;
cc[nx][ny] = c;
}
}
}
}
}
}
}
memset(vis, false, sizeof vis);
set<int> adj[c + 1];
int d[c + 1];
for(int i = 0;i<n;++i) {
for(int j = 0;j<m;++j) {
if (!vis[i][j] && a[i][j] != '.') {
queue<pii> q;
q.push({i, j});
vis[i][j] = true;
while(!q.empty()) {
pii node = q.front();
q.pop();
for(int k = 0;k<4;++k) {
int nx = node.fi + dx[k], ny = node.se + dy[k];
if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
if (a[nx][ny] == a[i][j] && !vis[nx][ny]) {
q.push({nx, ny});
vis[nx][ny] = true;
}
if (cc[nx][ny] != cc[i][j] && cc[nx][ny] != 0) {
adj[cc[i][j]].insert(cc[nx][ny]);
adj[cc[nx][ny]].insert(cc[i][j]);
}
}
}
}
}
}
}
queue<int> q;
q.push(cc[0][0]);
bool ok[c + 1];
memset(ok, false, sizeof ok);
ok[cc[0][0]] = true;
d[cc[0][0]] = 0;
int ans = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(const int& v:adj[node]) {
if (!ok[v]) {
q.push(v);
ok[v] = true;
d[v] = d[node] + 1;
ans = max(ans, d[v]);
}
}
}
cout << ans + 1 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
8772 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
15 ms |
1732 KB |
Output is correct |
5 |
Correct |
7 ms |
2124 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
6 ms |
2004 KB |
Output is correct |
11 |
Correct |
4 ms |
712 KB |
Output is correct |
12 |
Correct |
22 ms |
3348 KB |
Output is correct |
13 |
Correct |
7 ms |
2124 KB |
Output is correct |
14 |
Correct |
6 ms |
2132 KB |
Output is correct |
15 |
Correct |
40 ms |
8508 KB |
Output is correct |
16 |
Correct |
52 ms |
8872 KB |
Output is correct |
17 |
Correct |
28 ms |
7892 KB |
Output is correct |
18 |
Correct |
15 ms |
1712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Correct |
160 ms |
41888 KB |
Output is correct |
3 |
Correct |
863 ms |
318608 KB |
Output is correct |
4 |
Correct |
210 ms |
61912 KB |
Output is correct |
5 |
Correct |
1340 ms |
708496 KB |
Output is correct |
6 |
Execution timed out |
2077 ms |
166100 KB |
Time limit exceeded |
7 |
Correct |
3 ms |
1176 KB |
Output is correct |
8 |
Correct |
3 ms |
1324 KB |
Output is correct |
9 |
Correct |
6 ms |
1668 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
3 ms |
1996 KB |
Output is correct |
13 |
Correct |
155 ms |
41804 KB |
Output is correct |
14 |
Correct |
91 ms |
24524 KB |
Output is correct |
15 |
Correct |
97 ms |
27292 KB |
Output is correct |
16 |
Correct |
99 ms |
19408 KB |
Output is correct |
17 |
Correct |
393 ms |
107596 KB |
Output is correct |
18 |
Correct |
352 ms |
104348 KB |
Output is correct |
19 |
Correct |
210 ms |
61824 KB |
Output is correct |
20 |
Correct |
208 ms |
71688 KB |
Output is correct |
21 |
Correct |
523 ms |
189188 KB |
Output is correct |
22 |
Correct |
1213 ms |
708412 KB |
Output is correct |
23 |
Correct |
778 ms |
207536 KB |
Output is correct |
24 |
Correct |
625 ms |
272364 KB |
Output is correct |
25 |
Correct |
1535 ms |
426916 KB |
Output is correct |
26 |
Correct |
1471 ms |
72820 KB |
Output is correct |
27 |
Correct |
1599 ms |
97096 KB |
Output is correct |
28 |
Execution timed out |
2087 ms |
167412 KB |
Time limit exceeded |
29 |
Execution timed out |
2081 ms |
142976 KB |
Time limit exceeded |
30 |
Correct |
1970 ms |
127100 KB |
Output is correct |
31 |
Execution timed out |
2089 ms |
327840 KB |
Time limit exceeded |
32 |
Correct |
1565 ms |
114728 KB |
Output is correct |