답안 #729449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
729449 2023-04-24T07:22:48 Z pcc Dango Maker (JOI18_dango_maker) C++14
100 / 100
1967 ms 21088 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
#define _all(a) a.begin(),a.end()
#define pii pair<int,int>
 
const int mxn = 3030;
 
struct Edge{
	int to;
	int cap;
	Edge(){}
	Edge(int tt,int cc){
		to = tt,cap = cc;
	}
};
 
vector<Edge> edges;
bitset<mxn> done[mxn];
vector<vector<int>> paths;
vector<int> level;
string s[mxn];
int st,ed;
 
void addedge(int a,int b){
	paths[a].push_back(edges.size());
	edges.push_back(Edge(b,1));
	paths[b].push_back(edges.size());
	edges.push_back(Edge(a,0));
}
 
bool bfs(){
	queue<int> q;
	fill(level.begin(),level.end(),-1);
	level[0] = 0;
	q.push(0);
	while(!q.empty()){
		auto now = q.front();
		q.pop();
		for(auto &id:paths[now]){
			if(!edges[id].cap)continue;
			auto nxt = edges[id].to;
			if(level[nxt] == -1){
				level[nxt] = level[now]+1;
				q.push(nxt);
			}
		}
	}
	return level[ed] != -1;
}
 
int dfs(int now,int lim){
	if(now == ed)return lim;
	int ans = 0;
	for(auto &id:paths[now]){
		if(!edges[id].cap)continue;
		if(!lim)break;
		auto nxt = edges[id].to;
		if(level[nxt] != level[now]+1)continue;
		auto re = dfs(nxt,min(edges[id].cap,lim));
		if(re == 0)continue;
		edges[id].cap -= re;
		edges[id^1].cap += re;
		ans += re;
		lim -= re;
	}
	return ans;
}
 
vector<pii> righ,down;
int n,m;
 
 
int calc(int x,int y){
	edges.clear();
	//cout<<x<<y<<endl;
	righ.clear();
	down.clear();
	queue<pii> q;
	q.push(make_pair(x,y));
	done[x][y] = true;
	while(!q.empty()){
		auto now = q.front();
		q.pop();
		assert(done[now.fs][now.sc]);
		if(now.sc<m-2&&s[now.fs].substr(now.sc,3)=="RGW"){
			righ.push_back(now);
			pii nxt = make_pair(now.fs-1,now.sc+1);
			if(nxt.fs>=0&&nxt.sc<m&&now.fs<n-2&&!done[nxt.fs][nxt.sc]&&s[nxt.fs][nxt.sc] == 'R'&&
					s[nxt.fs+1][nxt.sc] == 'G'&&s[nxt.fs+2][nxt.sc] == 'W'){
				done[nxt.fs][nxt.sc] = true;
				q.push(nxt);
			}
			nxt = make_pair(now.fs-2,now.sc+2);
			if(nxt.fs>=0&&nxt.sc<m&&now.fs<n-2&&!done[nxt.fs][nxt.sc]&&s[nxt.fs][nxt.sc] == 'R'&&
					s[nxt.fs+1][nxt.sc] == 'G'&&s[nxt.fs+2][nxt.sc] == 'W'){
				done[nxt.fs][nxt.sc] = true;
				q.push(nxt);
			}
		}
		if(now.fs<n-2&&s[now.fs][now.sc] == 'R'&&s[now.fs+1][now.sc] == 'G'&&s[now.fs+2][now.sc] == 'W'){
			down.push_back(now);
			pii nxt = make_pair(now.fs+1,now.sc-1);
			if(nxt.fs<n&&nxt.sc>=0&&nxt.sc<m-2&&s[nxt.fs].substr(nxt.sc,3)=="RGW"&&!done[nxt.fs][nxt.sc]){
				done[nxt.fs][nxt.sc] = true;
				q.push(nxt);
			}
			nxt = make_pair(now.fs+2,now.sc-2);
			if(nxt.fs<n&&nxt.sc>=0&&nxt.sc<m-2&&s[nxt.fs].substr(nxt.sc,3)=="RGW"&&!done[nxt.fs][nxt.sc]){
				done[nxt.fs][nxt.sc] = true;
				q.push(nxt);
			}
		}
	}
	paths = vector<vector<int>>(down.size()+righ.size()+2);
	assert(down.size()+righ.size()<mxn+mxn);
	level = vector<int>(paths.size());
	sort(down.begin(),down.end());
	sort(righ.begin(),righ.end());
	for(int i = 0;i<down.size();i++){
		pii tar = make_pair(down[i].fs,down[i].sc);
		auto pos = lower_bound(_all(righ),tar)-righ.begin();
		if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
		tar = make_pair(down[i].fs+1,down[i].sc-1);
		pos = lower_bound(_all(righ),tar)-righ.begin();
		if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
		tar = make_pair(down[i].fs+2,down[i].sc-2);
		pos = lower_bound(_all(righ),tar)-righ.begin();
		if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
	}
	for(int i = 1;i<=down.size();i++)addedge(0,i);
	for(int i = down.size()+1;i<paths.size()-1;i++)addedge(i,paths.size()-1);
	int ans = 0;
	//cout<<paths.size()<<endl<<endl;
	/*
	for(int i = 0;i<paths.size();i++){
		for(auto &id:paths[i]){
			cout<<i<<','<<edges[id].to<<' ';
		}
	}
 
   */
	ed = paths.size()-1;
	st = 0;
	while(bfs()){
		int re = 0;
		while(re = dfs(0,mxn*mxn))ans+= re;
		//cout<<re<<endl;
	}
	//cout<<"HI"<<endl;
	//for(auto &i:down)cout<<i.fs<<' '<<i.sc<<',';cout<<endl;
	//for(auto &i:righ)cout<<i.fs<<' '<<i.sc<<',';cout<<endl;
	//cout<<endl;
	return (int)down.size()+righ.size()-ans;
}
 
void solve(){
	cin>>n>>m;
	int ans = 0;
	for(int i = 0;i<n;i++)cin>>s[i];
	for(int i = 0;i<n;i++){
		for(int j = 0;j<m;j++){
			if(done[i][j])continue;
			//cout<<i<<' '<<j<<endl;
			if(j<m-2&&s[i].substr(j,3)=="RGW")ans += calc(i,j);
			else if(i<n-2&&s[i][j] == 'R'&&s[i+1][j] == 'G'&&s[i+2][j] == 'W')ans += calc(i,j);
		}
	}
	cout<<ans;
	return;
}
 
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	solve();
}

Compilation message

dango_maker.cpp: In function 'int calc(int, int)':
dango_maker.cpp:124:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |  for(int i = 0;i<down.size();i++){
      |                ~^~~~~~~~~~~~
dango_maker.cpp:127:10: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |   if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
      |      ~~~~^~~~~~~~~~~~~~
dango_maker.cpp:130:10: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |   if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
      |      ~~~~^~~~~~~~~~~~~~
dango_maker.cpp:133:10: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |   if(pos != righ.size()&&righ[pos].fs == tar.fs&&righ[pos].sc == tar.sc)addedge(i+1,pos+down.size()+1);
      |      ~~~~^~~~~~~~~~~~~~
dango_maker.cpp:135:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |  for(int i = 1;i<=down.size();i++)addedge(0,i);
      |                ~^~~~~~~~~~~~~
dango_maker.cpp:136:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |  for(int i = down.size()+1;i<paths.size()-1;i++)addedge(i,paths.size()-1);
      |                            ~^~~~~~~~~~~~~~~
dango_maker.cpp:151:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  151 |   while(re = dfs(0,mxn*mxn))ans+= re;
      |         ~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 724 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 3 ms 1108 KB Output is correct
58 Correct 54 ms 1744 KB Output is correct
59 Correct 456 ms 11220 KB Output is correct
60 Correct 445 ms 11164 KB Output is correct
61 Correct 453 ms 11200 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 429 ms 10840 KB Output is correct
64 Correct 1935 ms 21088 KB Output is correct
65 Correct 1859 ms 21040 KB Output is correct
66 Correct 1967 ms 21048 KB Output is correct
67 Correct 1087 ms 20132 KB Output is correct
68 Correct 1074 ms 20108 KB Output is correct
69 Correct 1008 ms 20012 KB Output is correct
70 Correct 199 ms 3140 KB Output is correct
71 Correct 202 ms 3092 KB Output is correct
72 Correct 200 ms 3140 KB Output is correct
73 Correct 201 ms 3152 KB Output is correct
74 Correct 210 ms 3292 KB Output is correct
75 Correct 210 ms 3092 KB Output is correct
76 Correct 202 ms 3064 KB Output is correct
77 Correct 204 ms 3188 KB Output is correct
78 Correct 237 ms 3148 KB Output is correct
79 Correct 207 ms 3164 KB Output is correct
80 Correct 220 ms 3144 KB Output is correct
81 Correct 208 ms 3220 KB Output is correct
82 Correct 206 ms 3104 KB Output is correct
83 Correct 211 ms 3144 KB Output is correct
84 Correct 203 ms 3204 KB Output is correct
85 Correct 207 ms 3076 KB Output is correct
86 Correct 205 ms 3144 KB Output is correct
87 Correct 203 ms 3124 KB Output is correct
88 Correct 207 ms 3212 KB Output is correct
89 Correct 213 ms 3068 KB Output is correct
90 Correct 203 ms 3156 KB Output is correct