#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> g(n*m);
vector<int> cnt(n*m);
for(int i=0; i<=n; i++){
string s;
cin>>s;
if(i==0 || i==n){
for(int j=0; j<m; j++){
if(s[j]=='0') cnt[(i==0?0:n-1)*m+j]++;
}
}
else{
for(int j=0; j<m; j++){
if(s[j]=='0'){
g[(i-1)*m+j].push_back(i*m+j);
g[i*m+j].push_back((i-1)*m+j);
}
}
}
}
for(int i=0; i<n; i++){
string s;
cin>>s;
for(int j=1; j<n; j++){
if(s[j]=='0'){
g[i*m+j].push_back(i*m+j-1);
g[i*m+j-1].push_back(i*m+j);
}
}
}
vector<bool> seen(n*m,0);
vector<int> comp;
for(int i=0; i<n*m; i++){
if(!seen[i] && (g[i].size() || cnt[i])){
int sz=0;
queue<int> q;
q.push(i);
while(!q.empty()){
auto t=q.front();
q.pop();
if(seen[t]) continue;
sz++;
seen[t]=1;
for(int j:g[t]) if(!seen[j]) q.push(j);
}
comp.push_back(sz);
}
}
sort(comp.begin(),comp.end());
int res=0;
int p=-1;
for(int i:comp){
res+=p*i;
p*=-1;
}
cout<<res<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |