이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"bits/stdc++.h"
using namespace std;
using ll = long long;
const int mxN = 4003;
char g[mxN][mxN];
struct Dsu {
int ar[mxN * mxN];
Dsu() {
fill(ar, ar + mxN, -1);
}
int Find(int u) {
if (0 > ar[u]) {
return u;
} else {
return ar[u] = Find(ar[u]);
}
}
bool Unite(int u, int v) {
u = Find(u);
v = Find(v);
if (u == v) {
return false;
}
if (ar[u] > ar[v]) {
swap(u, v);
}
ar[u] += ar[v];
ar[v] = u;
return true;
}
} dsu;
main() {
int H, W;
cin >> H >> W;
set<char> s;
for (int i = 0; i < H; i ++) {
for (int j = 0; j < W; j ++) {
cin >> g[i][j];
s.insert(g[i][j]);
}
}
if (2 == (int)s.size()) {
cout << 1 << endl;
} else {
for (int i = 0; i < H; i ++) {
for (int j = 0; j < W; j ++) {
if (g[i][j] ^ g[0][0]) {
continue;
}
if (i + 1 < H) {
if (g[i + 1][j] == g[0][0]) {
dsu.Unite(0, (i + 1) * W + j);
}
}
if (j + 1 < W) {
if (g[i][j + 1] == g[0][0]) {
dsu.Unite(0, i * W + j + 1);
}
}
}
}
int ans = 2;
for (int i = 0; i < H; i ++) {
for (int j = 0; j < W; j ++) {
if (g[0][0] == g[i][j]) {
if (dsu.Unite(0, i * W + j)) {
ans = 3;
}
}
}
}
cout << ans << endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
tracks.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |