# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45427 | choikiwon | Young Zebra (KRIII5_YZ) | C++17 | 71 ms | 7148 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;
typedef pair<int, int> pii;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int N, M;
char G[402][402];
int vis[402][402];
pii dist[402][402];
int sz[1000010];
int main() {
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++) {
scanf("\n");
for(int j = 0; j < M; j++) {
scanf("%c", &G[i][j]);
}
}
memset(vis, -1, sizeof(vis));
int ccnt = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(vis[i][j] != -1) continue;
queue<int> q;
q.push(i * M + j);
vis[i][j] = ccnt;
int inf = 0;
int cnt = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
int r = u / M;
int c = u % M;
for(int k = 0; k < 4; k++) {
int nr = (r + dy[k] + N) % N;
int nc = (c + dx[k] + M) % M;
pii nd = dist[r][c];
if(r + dy[k] < 0) nd.first--;
if(r + dy[k] >= N) nd.first++;
if(c + dx[k] < 0) nd.second--;
if(c + dx[k] >= M) nd.second++;
if(G[nr][nc] != G[r][c]) continue;
if(vis[nr][nc] != -1) {
if(dist[nr][nc] != nd) {
inf = 1;
}
continue;
}
cnt++;
q.push(nr * M + nc);
vis[nr][nc] = ccnt;
dist[nr][nc] = nd;
}
}
sz[ccnt] = inf? -1 : cnt;
ccnt++;
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
printf("%d ", sz[ vis[i][j] ]);
}
printf("\n");
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |