이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int inf=1e9;
int n, m;
vector<int>Sz, Mn, P;
vector<vector<int>>A, B;
void pre(){
Sz.clear(), Mn.clear(), A.clear(), B.clear();
Sz.resize((n+2)*(m+3)+m+2), P.resize((n+2)*(m+3)+m+3), Mn.resize((n+1)*(m+2)+m+3, inf), A.resize(n+5, vector<int>(m+5)), B.resize(n+5, vector<int>(m+5));
for(int i=1;i<P.size();i++){
P[i]=i;
}
}
int trans(int i, int j){
return i*(m+3)+j;
}
int find(int ind){
if(ind==P[ind])
return ind;
return P[ind]=find(P[ind]);
}
bool merge(int a, int b){
int ga=find(a), gb=find(b);
if(ga==gb)
return 0;
if(rand()&1)
swap(ga, gb);
P[ga]=gb;
Sz[gb]+=Sz[ga];
Mn[gb]=min(Mn[gb], Mn[ga]);
return 1;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
pre();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>A[i][j];
}
}
for(int i=1;i<=n;i++){
B[i][0]=B[i][m+1]=1;
}
for(int j=1;j<=m;j++){
B[0][j]=B[n+1][j]=1;
}
int q;
cin>>q;
while(q--){
int i, j;
cin>>i>>j;
i++, j++;
B[i][j]+=inf;
B[i+1][j]++, B[i-1][j]++;
B[i][j+1]++, B[i][j-1]++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(B[i][j]<inf)
continue;
int cur=trans(i, j);
Sz[cur]=1;
Mn[cur]=min(Mn[cur], A[i+1][j]);
Mn[cur]=min(Mn[cur], A[i-1][j]);
Mn[cur]=min(Mn[cur], A[i][j+1]);
Mn[cur]=min(Mn[cur], A[i][j-1]);
}
}
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
vector<int>Cur;
if(i!=n+1 && B[i+1][j]>=inf)
Cur.push_back(trans(i+1, j));
if(i && B[i-1][j]>=inf)
Cur.push_back(trans(i-1, j));
if(j!=m+1 && B[i][j+1]>=inf)
Cur.push_back(trans(i, j+1));
if(j && B[i][j-1]>=inf)
Cur.push_back(trans(i, j-1));
if(B[i][j]>=inf)
Cur.push_back(trans(i, j));
for(int l1=0;l1<Cur.size();l1++){
for(int l2=0;l2<Cur.size();l2++){
merge(Cur[l1], Cur[l2]);
}
}
}
}
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
if(B[i][j]>1 && B[i][j]!=inf){
if(B[i][j]>inf)
Sz[find(trans(i, j))]-=B[i][j]-inf;
else if(i && B[i-1][j]>=inf)
Sz[find(trans(i-1, j))]-=B[i][j]-1;
else if(i!=n+1 && B[i+1][j]>=inf)
Sz[find(trans(i+1, j))]-=B[i][j]-1;
else if(j && B[i][j-1]>=inf)
Sz[find(trans(i, j-1))]-=B[i][j]-1;
else
Sz[find(trans(i, j+1))]-=B[i][j]-1;
}
}
}
int ans=0;
for(int i=0;i<P.size();i++){
if(i!=find(i))
continue;
if(Sz[i]<0)
return cout<<"No", 0;
else if(Sz[i]==1){
ans-=Mn[i];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(B[i][j])
ans+=A[i][j];
}
}
cout<<ans;
}
컴파일 시 표준 에러 (stderr) 메시지
covering.cpp: In function 'void pre()':
covering.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=1;i<P.size();i++){
| ~^~~~~~~~~
covering.cpp: In function 'int main()':
covering.cpp:91:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int l1=0;l1<Cur.size();l1++){
| ~~^~~~~~~~~~~
covering.cpp:92:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int l2=0;l2<Cur.size();l2++){
| ~~^~~~~~~~~~~
covering.cpp:115:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int i=0;i<P.size();i++){
| ~^~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |