# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
153468 | mhy908 | Dango Maker (JOI18_dango_maker) | C++14 | 899 ms | 262148 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define INF 987654321
using namespace std;
typedef long long LL;
int n, m, x;
vector<char> str[3010];
int col[3010][3010][2], ans;
short re[3010][3010];
vector<int> link[6000001], id;
int par[6000001];
vector<bool> used;
vector<int> dist, A, B;
vector<vector<int> > vc;
map<int, int> mp;
int findpar(int num)
{
if(num==par[num])return num;
return par[num]=findpar(par[num]);
}
void mergepar(int a, int b)
{
a=findpar(a);
b=findpar(b);
par[a]=par[b];
}
void bfs(int p)
{
queue<int> Q;
for(int i=0; i<vc[p].size(); i++){
if(!used[i]){
dist[i]=0;
Q.push(i);
}
else dist[i]=INF;
}
while(!Q.empty()){
int a=Q.front();
Q.pop();
for(int b: link[vc[p][a]]){
b=mp[b];
if(B[b]!=-1&&dist[B[b]]==INF){
dist[B[b]]=dist[a]+1;
Q.push(B[b]);
}
}
}
}
bool dfs(int p, int a){
for(int b: link[vc[p][a]]){
b=mp[b];
if(B[b]==-1||dist[B[b]]==dist[a]+1&&dfs(p, B[b])){
used[a]=true;
A[a]=b;
B[b]=a;
return true;
}
}
return false;
}
int hopcroft_carp(int p)
{
int match=0;
while(1){
bfs(p);
int flow=0;
for(int i=0; i<vc[p].size(); i++)
if(!used[i]&&dfs(p, i))flow++;
if(!flow)break;
match+=flow;
}
return match/2;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++){
str[i].pb(0);
for(int j=1; j<=m; j++){
char y;
scanf(" %c", &y);
str[i].pb(y);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(j+2<=m)
if(str[i][j]=='R'&&str[i][j+1]=='G'&&str[i][j+2]=='W'){
++x;
col[i][j][re[i][j]++]=x;
col[i][j+1][re[i][j+1]++]=x;
col[i][j+2][re[i][j+2]++]=x;
}
if(i+2<=n)
if(str[i][j]=='R'&&str[i+1][j]=='G'&&str[i+2][j]=='W'){
++x;
col[i][j][re[i][j]++]=x;
col[i+1][j][re[i+1][j]++]=x;
col[i+2][j][re[i+2][j]++]=x;
}
}
}
for(int i=1; i<=n; i++)str[i].resize(0);
for(int i=1; i<=x; i++)par[i]=i;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(re[i][j]==2){
mergepar(col[i][j][1], col[i][j][0]);
link[col[i][j][1]].pb(col[i][j][0]);
link[col[i][j][0]].pb(col[i][j][1]);
}
}
}
for(int i=1; i<=x; i++){
id.pb(findpar(i));
}
sort(id.begin(), id.end());
id.erase(unique(id.begin(), id.end()), id.end());
int maxpar=1;
for(int i=1; i<=x; i++){
par[i]=lower_bound(id.begin(), id.end(), par[i])-id.begin();
maxpar=max(maxpar, par[i]+1);
}
id.resize(0);
vc.resize(maxpar);
for(int i=1; i<=x; i++){
vc[par[i]].pb(i);
}
for(int i=0; i<maxpar; i++){
if(vc[i].size()==1)ans++;
else{
for(int j=0; j<vc[i].size(); j++)mp[vc[i][j]]=j;
used.resize(vc[i].size()+2);
dist.resize(vc[i].size()+2);
A.resize(vc[i].size()+2, -1);
B.resize(vc[i].size()+2, -1);
ans+=vc[i].size()-hopcroft_carp(i);
mp.clear();
dist.resize(0);
A.resize(0);
B.resize(0);
used.resize(0);
}
}
printf("%d", ans);
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |