#include<bits/stdc++.h>
#include <bitset>
using namespace std;
const int N = 4000;
string S[2];
vector<pair<int,int>>Graf1[N*N]; //czy rozne kolory, wierzcholek
int idx[N*N], c=1;
bitset<N*N> c1, c2;
vector<int>Graf[N*N];
void DFS1(int n, int v){
c1[n] = c, idx[n] = v;
for(auto& elm : Graf1[n]){
if(c1[elm.second] == 0 && elm.first == 0){
DFS1(elm.second, v);
}
}
}
int makeTree(int n, int m){
int v = 0, d = 0;
for(int i=0;i<n*m;i++){
if(c1[i] == 0)
DFS1(i, v++);
}
for(int i=0;i<n*m;i++){
for(auto& elm : Graf1[i]){
if(idx[elm.second] != idx[i])
Graf[idx[i]].push_back(idx[elm.second]);
}
Graf1[i].clear();
}
queue<pair<int,int>>Q;
Q.push({0, 1});
while(!Q.empty()){
auto now = Q.front();
Q.pop();
if(c2[now.first] == c)
continue;
c2[now.first] = c, d = max(d, now.second);
for(auto& elm : Graf[now.first]){
if(c2[elm] != c){
Q.push({elm, now.second+1});
}
}
}
return d;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
S[0] = S[1];
cin>>S[1];
for(int j=0;j<m;j++){
if(S[1][j] == '.')
continue;
if(i != 0){
if(S[0][j] == S[1][j]){
Graf1[i*m+j].push_back({0, (i-1)*m+j}), Graf1[(i-1)*m+j].push_back({0, i*m+j});
}
else if(S[0][j] != '.'){
Graf1[i*m+j].push_back({1, (i-1)*m+j}), Graf1[(i-1)*m+j].push_back({1, i*m+j});
}
}
if(j != 0){
if(S[1][j-1] == S[1][j]){
Graf1[i*m+j-1].push_back({0, i*m+j}), Graf1[i*m+j].push_back({0, i*m+j-1});
}
else if(S[1][j-1] != '.'){
Graf1[i*m+j-1].push_back({1, i*m+j}), Graf1[i*m+j].push_back({1, i*m+j-1});
}
}
}
}
cout<<makeTree(n, m)<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
767056 KB |
Output is correct |
2 |
Correct |
320 ms |
751820 KB |
Output is correct |
3 |
Correct |
338 ms |
751828 KB |
Output is correct |
4 |
Correct |
383 ms |
761536 KB |
Output is correct |
5 |
Correct |
303 ms |
753324 KB |
Output is correct |
6 |
Correct |
306 ms |
751700 KB |
Output is correct |
7 |
Correct |
301 ms |
751956 KB |
Output is correct |
8 |
Correct |
308 ms |
752000 KB |
Output is correct |
9 |
Correct |
322 ms |
752044 KB |
Output is correct |
10 |
Correct |
312 ms |
753492 KB |
Output is correct |
11 |
Correct |
306 ms |
754508 KB |
Output is correct |
12 |
Correct |
339 ms |
757076 KB |
Output is correct |
13 |
Correct |
328 ms |
753236 KB |
Output is correct |
14 |
Correct |
349 ms |
753236 KB |
Output is correct |
15 |
Correct |
360 ms |
763484 KB |
Output is correct |
16 |
Correct |
352 ms |
767056 KB |
Output is correct |
17 |
Correct |
367 ms |
758700 KB |
Output is correct |
18 |
Correct |
327 ms |
761752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
752892 KB |
Output is correct |
2 |
Correct |
446 ms |
795472 KB |
Output is correct |
3 |
Correct |
1055 ms |
1024964 KB |
Output is correct |
4 |
Correct |
574 ms |
796496 KB |
Output is correct |
5 |
Runtime error |
758 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Runtime error |
752 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
293 ms |
752724 KB |
Output is correct |
8 |
Correct |
299 ms |
752724 KB |
Output is correct |
9 |
Correct |
322 ms |
754004 KB |
Output is correct |
10 |
Correct |
292 ms |
752452 KB |
Output is correct |
11 |
Correct |
281 ms |
752624 KB |
Output is correct |
12 |
Correct |
282 ms |
752724 KB |
Output is correct |
13 |
Correct |
472 ms |
795472 KB |
Output is correct |
14 |
Correct |
428 ms |
777216 KB |
Output is correct |
15 |
Correct |
411 ms |
769364 KB |
Output is correct |
16 |
Correct |
356 ms |
774996 KB |
Output is correct |
17 |
Correct |
605 ms |
862276 KB |
Output is correct |
18 |
Correct |
609 ms |
820532 KB |
Output is correct |
19 |
Correct |
518 ms |
796500 KB |
Output is correct |
20 |
Correct |
505 ms |
814684 KB |
Output is correct |
21 |
Correct |
733 ms |
914000 KB |
Output is correct |
22 |
Runtime error |
800 ms |
1048576 KB |
Execution killed with signal 9 |
23 |
Correct |
1002 ms |
968528 KB |
Output is correct |
24 |
Correct |
747 ms |
930644 KB |
Output is correct |
25 |
Correct |
1920 ms |
1032536 KB |
Output is correct |
26 |
Runtime error |
769 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
770 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
775 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
772 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
741 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Runtime error |
795 ms |
1048576 KB |
Execution killed with signal 9 |
32 |
Runtime error |
755 ms |
1048576 KB |
Execution killed with signal 9 |