#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include <iostream>
#include <stack>
#include <fstream>
#include <cstdio>
#include <numeric>
#include <time.h>
#include <sys/timeb.h>
#define double long double
#define vi vector<int>
#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair<int,int>
#define sz(a) (int)a.size()
#define revall(a) a.rbegin(), a.rend()
#define all(a) a.begin(),a.end()
#define int_max INT64_MAX/2
#define int_min INT64_MIN/2
#define multierase (a, x) a.erase(a.find(x))
#define pq priority_queue
#define pqr priority_queue<int, vector<int>, greater<int>>
#define contains (a, x) a.find(x)!=a.end()
#define MOD 1000000007
#define SMOD 998244353
#define ODDMOD 100000000
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void setIO(string name = ""){
ios_base::sync_with_stdio(0);
cin.tie(0);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void read(vi& arr, int n) {
for(int i=0;i<n;i++){
int a;
cin>>a;
arr.pb(a);
}
}
void print(pii p) {
cout<<"[ "<<p.first<<" "<<p.second<<" ]"<<endl;
}
void print(vi arr) {
for(int i : arr) {
if(i==int_max)
cout<<"i ";
else if(i==int_min)
cout<<"-i ";
else
cout<<i<<" ";
}
cout<<endl;
}
int ceil_div(int a, int b) {
if(a%b==0) {
return a/b;
}else {
return (a/b)+1;
}
}
int h,w;
vector<string> grid;
vector<vi> rep;
void ff(int x, int y, int crep, char gval) {
if(x<0||y<0||x>=w||y>=h) return;
if(grid[y][x]!=gval) return;
if(rep[y][x]!=0) return;
rep[y][x]=crep;
ff(x-1, y, crep,gval);
ff(x+1, y, crep,gval);
ff(x,y+1,crep,gval);
ff(x,y-1,crep,gval);
}
void solve() {
cin>>h>>w;
grid.clear();
for(int i=0;i<h;i++) {
string s;
cin>>s;
grid.pb(s);
}
rep=vector<vi>(h, vi(w, 0));
int crep=1;
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
if(rep[i][j]==0&&grid[i][j]!='.') {
ff(j,i,crep, grid[i][j]);
crep++;
}
}
}
vector<vi> adj;
adj.resize(crep);
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
if(i!=(h-1)&&rep[i][j]!=0&&rep[i+1][j]!=0) {
adj[rep[i][j]].pb(rep[i+1][j]);
adj[rep[i+1][j]].pb(rep[i][j]);
}
if(j!=(w-1)&&rep[i][j]!=0&&rep[i][j+1]!=0) {
adj[rep[i][j]].pb(rep[i][j+1]);
adj[rep[i][j+1]].pb(rep[i][j]);
}
}
}
vi dist (crep, INT32_MAX);
vi vis (crep, 0);
priority_queue<pii> Q;
Q.push({0, 1});
dist[1]=0;
while(!Q.empty()) {
pii c=Q.top();
Q.pop();
if(vis[c.se]) continue;
vis[c.se]=1;
for(int i:adj[c.se]) {
if(dist[i]>dist[c.se]+1) {
dist[i]=dist[c.se]+1;
Q.push({-dist[i], i});
}
}
}
int res=0;
for(int i=1;i<crep;i++) {
res=max(res, dist[i]);
}
cout<<res+1<<endl;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
// int x;
// cin>>x;
// while(x>0) {
solve();
// x--;
// }
}
Compilation message
tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
8272 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
11 ms |
4424 KB |
Output is correct |
5 |
Correct |
5 ms |
1360 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
4 ms |
1616 KB |
Output is correct |
11 |
Correct |
4 ms |
1756 KB |
Output is correct |
12 |
Correct |
14 ms |
3152 KB |
Output is correct |
13 |
Correct |
5 ms |
1360 KB |
Output is correct |
14 |
Correct |
5 ms |
1360 KB |
Output is correct |
15 |
Correct |
29 ms |
6880 KB |
Output is correct |
16 |
Correct |
26 ms |
8272 KB |
Output is correct |
17 |
Correct |
17 ms |
4944 KB |
Output is correct |
18 |
Correct |
11 ms |
4488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1360 KB |
Output is correct |
2 |
Correct |
129 ms |
28500 KB |
Output is correct |
3 |
Correct |
600 ms |
203112 KB |
Output is correct |
4 |
Correct |
162 ms |
38204 KB |
Output is correct |
5 |
Correct |
671 ms |
326332 KB |
Output is correct |
6 |
Correct |
1328 ms |
442268 KB |
Output is correct |
7 |
Correct |
3 ms |
1360 KB |
Output is correct |
8 |
Correct |
3 ms |
1360 KB |
Output is correct |
9 |
Correct |
4 ms |
1616 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
1104 KB |
Output is correct |
12 |
Correct |
2 ms |
1184 KB |
Output is correct |
13 |
Correct |
110 ms |
28488 KB |
Output is correct |
14 |
Correct |
67 ms |
16720 KB |
Output is correct |
15 |
Correct |
54 ms |
15272 KB |
Output is correct |
16 |
Correct |
62 ms |
14160 KB |
Output is correct |
17 |
Correct |
312 ms |
72008 KB |
Output is correct |
18 |
Correct |
263 ms |
58188 KB |
Output is correct |
19 |
Correct |
157 ms |
38216 KB |
Output is correct |
20 |
Correct |
140 ms |
46152 KB |
Output is correct |
21 |
Correct |
357 ms |
120392 KB |
Output is correct |
22 |
Correct |
681 ms |
326412 KB |
Output is correct |
23 |
Correct |
536 ms |
139488 KB |
Output is correct |
24 |
Correct |
364 ms |
148416 KB |
Output is correct |
25 |
Correct |
1141 ms |
235924 KB |
Output is correct |
26 |
Correct |
1425 ms |
898708 KB |
Output is correct |
27 |
Correct |
1217 ms |
621184 KB |
Output is correct |
28 |
Correct |
1348 ms |
446828 KB |
Output is correct |
29 |
Correct |
1263 ms |
401332 KB |
Output is correct |
30 |
Correct |
1220 ms |
441104 KB |
Output is correct |
31 |
Correct |
1507 ms |
332560 KB |
Output is correct |
32 |
Correct |
1043 ms |
522928 KB |
Output is correct |