#include<bits/stdc++.h>
using namespace std;
int n, m, org[405][405];
int par[160005], cnt[160005], inf[160005], cc;
char a[405][405];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<pair<pair<int,int>,int> > edg;
bool valid (int X, int Y) {
return (1<=X && X<=n && 1<=Y && Y<=m);
}
int Find (int X) {
if(par[X] == X) return X;
return par[X] = Find(par[X]);
}
int dfs (int X, int Y) {
org[X][Y] = cc;
int ret = 1;
for(int i=0;i<4;i++) {
int nx = X + dx[i], ny = Y + dy[i];
if(valid(nx, ny) && a[X][Y] == a[nx][ny] && !org[nx][ny]) {
ret += dfs(nx, ny);
}
}
return ret;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) {
scanf("%s",a[i]+1);
}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) {
if(org[i][j]) continue;
cc++;
par[cc] = cc;
cnt[cc] = dfs(i, j);
}
for(int i=1;i<=n;i++) {
if(a[i][1] != a[i][m]) continue;
int A = org[i][1], B = org[i][m];
if(A > B) swap(A, B);
edg.push_back({{A, B}, 0});
}
for(int i=1;i<=m;i++) {
if(a[1][i] != a[n][i]) continue;
int A = org[1][i], B = org[n][i];
if(A > B) swap(A, B);
edg.push_back({{A, B}, 1});
}
sort(edg.begin(), edg.end());
edg.erase(unique(edg.begin(), edg.end()), edg.end());
for(auto &I : edg) {
int A = I.first.first, B = I.first.second;
A = Find(A); B = Find(B);
if(A == B) inf[A] = 1;
else {
inf[A] |= inf[B];
cnt[A] += cnt[B];
par[B] = A;
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
int tmp = Find(org[i][j]);
printf("%d ", cnt[tmp]);
}
puts("");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:36:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
YZ.cpp:38:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",a[i]+1);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
19 ms |
6552 KB |
Output is partially correct |
2 |
Partially correct |
13 ms |
5360 KB |
Output is partially correct |
3 |
Partially correct |
13 ms |
5360 KB |
Output is partially correct |
4 |
Partially correct |
19 ms |
5128 KB |
Output is partially correct |
5 |
Partially correct |
13 ms |
5016 KB |
Output is partially correct |
6 |
Partially correct |
16 ms |
5016 KB |
Output is partially correct |
7 |
Partially correct |
16 ms |
4700 KB |
Output is partially correct |
8 |
Partially correct |
13 ms |
4700 KB |
Output is partially correct |
9 |
Partially correct |
19 ms |
4700 KB |
Output is partially correct |
10 |
Partially correct |
19 ms |
4700 KB |
Output is partially correct |
11 |
Partially correct |
16 ms |
7636 KB |
Output is partially correct |
12 |
Partially correct |
23 ms |
11596 KB |
Output is partially correct |
13 |
Partially correct |
19 ms |
4700 KB |
Output is partially correct |
14 |
Partially correct |
16 ms |
4700 KB |
Output is partially correct |
15 |
Partially correct |
9 ms |
4700 KB |
Output is partially correct |
16 |
Partially correct |
19 ms |
4700 KB |
Output is partially correct |
17 |
Partially correct |
16 ms |
4704 KB |
Output is partially correct |
18 |
Partially correct |
16 ms |
4700 KB |
Output is partially correct |
19 |
Correct |
19 ms |
4700 KB |
Output is correct |
20 |
Partially correct |
16 ms |
7300 KB |
Output is partially correct |
21 |
Partially correct |
16 ms |
4700 KB |
Output is partially correct |
22 |
Partially correct |
16 ms |
4700 KB |
Output is partially correct |
23 |
Partially correct |
19 ms |
7052 KB |
Output is partially correct |
24 |
Partially correct |
19 ms |
5604 KB |
Output is partially correct |
25 |
Partially correct |
19 ms |
4700 KB |
Output is partially correct |
26 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
27 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
28 |
Partially correct |
0 ms |
4704 KB |
Output is partially correct |
29 |
Partially correct |
0 ms |
4708 KB |
Output is partially correct |
30 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
31 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
32 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
33 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
34 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
35 |
Partially correct |
0 ms |
4700 KB |
Output is partially correct |
36 |
Partially correct |
0 ms |
5032 KB |
Output is partially correct |
37 |
Partially correct |
0 ms |
4852 KB |
Output is partially correct |
38 |
Correct |
0 ms |
4700 KB |
Output is correct |
39 |
Partially correct |
0 ms |
4860 KB |
Output is partially correct |
40 |
Partially correct |
0 ms |
5040 KB |
Output is partially correct |