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;
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |