# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
134068 | wilwxk | Tracks in the Snow (BOI13_tracks) | C++14 | 2120 ms | 697540 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e3+5;
const int MAXNN=16000005;
vector<int> g[MAXNN];
short cor[MAXNN];
short marc[MAXNN];
short ori[MAXN][MAXN];
char c[MAXN];
int rep[MAXNN], tam[MAXNN], dist[MAXNN];
vector<int> qu;
int dx[]={0, 1, 0, -1};
int dy[]={1, 0, -1, 0};
int n, m, respf;
int find(int cur) {
if(rep[cur]==cur) return cur;
return rep[cur]=find(rep[cur]);
}
void une(int a, int b) {
a=find(a); b=find(b);
if(a==b) return;
if(tam[a]<tam[b]) swap(a, b);
rep[a]=b;
}
void liga(int a, int b) {
a=find(a); b=find(b);
g[a].push_back(b);
g[b].push_back(a);
// printf("liga %d %d\n", a, b);
}
void bfs() {
qu.push_back(find(0));
dist[find(0)]=1;
int ind=0;
while(ind<qu.size()) {
int cur=qu[ind++];
cur=find(cur);
if(marc[cur]==2) continue; marc[cur]=2;
respf=max(respf, dist[cur]);
for(auto viz : g[cur]) {
if(marc[find(viz)]!=0) continue;
qu.push_back(find(viz));
dist[viz]=dist[cur]+1;
marc[find(viz)]=1;
}
}
}
int main() {
scanf("%d %d", &n, &m);
srand(time(0));
for(int i=0; i<n; i++) for(int j=0; j<m; j++) rep[i*m+j]=tam[i*m+j]=i*m+j;
random_shuffle(tam, tam+(n*m));
for(int i=0; i<n; i++) {
scanf(" %s", c);
for(int j=0; j<m; j++) {
if(c[j]=='.') ori[i][j]=0;
else if(c[j]=='F') ori[i][j]=1;
else ori[i][j]=2;
cor[i*m+j]=ori[i][j];
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int cur=i*m+j;
if(!cor[cur]) continue;
for(int k=0; k<4; k++) {
int ii=i+dx[k]; int jj=j+dy[k];
if(ii<0||jj<0||ii>=n||jj>=m) continue;
int viz=ii*m+jj;
if(!cor[viz]) continue;
if(ori[i][j]==ori[ii][jj]) une(cur, viz);
}
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int cur=i*m+j;
if(!cor[cur]) continue;
for(int k=0; k<4; k++) {
int ii=i+dx[k]; int jj=j+dy[k];
if(ii<0||jj<0||ii>=n||jj>=m) continue;
int viz=ii*m+jj;
if(!cor[viz]) continue;
if(cor[cur]!=cor[viz]) liga(cur, viz);
}
}
}
n*=m;
bfs();
printf("%d\n", respf);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |