結果

問題 No.1123 Afforestation
ユーザー chocoruskchocorusk
提出日時 2020-08-01 17:35:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 827 ms / 2,500 ms
コード長 3,252 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 142,280 KB
実行使用メモリ 220,032 KB
最終ジャッジ日時 2023-09-22 11:16:13
合計ジャッジ時間 17,540 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,940 KB
testcase_04 AC 18 ms
13,936 KB
testcase_05 AC 149 ms
62,444 KB
testcase_06 AC 665 ms
220,032 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,488 KB
testcase_11 AC 10 ms
7,288 KB
testcase_12 AC 76 ms
34,756 KB
testcase_13 AC 808 ms
220,016 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 4 ms
4,700 KB
testcase_19 AC 20 ms
13,948 KB
testcase_20 AC 199 ms
62,144 KB
testcase_21 AC 823 ms
219,508 KB
testcase_22 AC 827 ms
219,564 KB
testcase_23 AC 822 ms
219,916 KB
testcase_24 AC 820 ms
219,824 KB
testcase_25 AC 814 ms
219,748 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,704 KB
testcase_35 AC 3 ms
4,800 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 4 ms
4,776 KB
testcase_40 AC 4 ms
4,716 KB
testcase_41 AC 4 ms
4,852 KB
testcase_42 AC 10 ms
7,036 KB
testcase_43 AC 20 ms
13,876 KB
testcase_44 AC 37 ms
18,320 KB
testcase_45 AC 36 ms
18,216 KB
testcase_46 AC 45 ms
19,744 KB
testcase_47 AC 46 ms
19,708 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 4 ms
4,752 KB
testcase_51 AC 4 ms
4,740 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 4 ms
4,700 KB
testcase_56 AC 4 ms
4,816 KB
testcase_57 AC 3 ms
4,860 KB
testcase_58 AC 10 ms
7,032 KB
testcase_59 AC 20 ms
13,956 KB
testcase_60 AC 37 ms
18,328 KB
testcase_61 AC 36 ms
18,228 KB
testcase_62 AC 44 ms
19,816 KB
testcase_63 AC 44 ms
19,764 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,384 KB
testcase_66 AC 2 ms
4,376 KB
testcase_67 AC 3 ms
4,380 KB
testcase_68 AC 4 ms
4,700 KB
testcase_69 AC 7 ms
6,056 KB
testcase_70 AC 10 ms
6,976 KB
testcase_71 AC 20 ms
13,784 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 1 ms
4,380 KB
testcase_75 AC 2 ms
4,380 KB
testcase_76 AC 2 ms
4,376 KB
testcase_77 AC 2 ms
4,380 KB
testcase_78 AC 2 ms
4,376 KB
testcase_79 AC 2 ms
4,376 KB
testcase_80 AC 2 ms
4,376 KB
testcase_81 AC 2 ms
4,380 KB
testcase_82 AC 2 ms
4,376 KB
testcase_83 AC 2 ms
4,380 KB
testcase_84 AC 436 ms
219,836 KB
testcase_85 AC 530 ms
219,868 KB
testcase_86 AC 1 ms
4,376 KB
testcase_87 AC 2 ms
4,376 KB
testcase_88 AC 421 ms
204,016 KB
testcase_89 AC 433 ms
204,052 KB
testcase_90 AC 2 ms
4,376 KB
testcase_91 AC 1 ms
4,376 KB
testcase_92 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll INF=1e18;
struct edge {int to; ll cap; int rev;} ;

vector<edge> G[4040];
int level[4040];
int iter[4040];

void add_edge(int from, int to, ll cap, ll myon=0){
	edge e;
	e.to=to, e.cap=cap-myon, e.rev=G[to].size();
	G[from].push_back(e);
	e.to=from, e.cap=myon, e.rev=G[from].size()-1;
	G[to].push_back(e);
}

void bfs(int s){
	memset(level, -1, sizeof(level));
	queue<int> que;
	level[s]=0;
	que.push(s);
	while(!que.empty()){
		int v=que.front();
		que.pop();
		for(int i=0; i<G[v].size(); i++){
			edge e=G[v][i];
			if(e.cap>0 && level[e.to]<0){
				level[e.to]=level[v]+1;
				que.push(e.to);
			}
		}
	}
}

ll dfs(int v, int t, ll f){
	if(v==t) return f;
	for(int &i=iter[v]; i<G[v].size(); i++){
		edge &e=G[v][i];
		if(e.cap>0 && level[v]<level[e.to]){
			ll d=dfs(e.to, t, min(f, e.cap));
			if(d>0){
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
	}
	return 0;
}

ll max_flow(int s, int t){
	ll flow=0;
	while(1){
		bfs(s);
		if(level[t]<0) return flow;
		memset(iter, 0, sizeof(iter));
		ll f;
		while((f=dfs(s, t, INF))>0){
			flow+=f;
		}
	}
}
int z[2020][2020];
int main()
{
	int h, w; cin>>h>>w;
	int a[2020], b[2020];
	vector<P> va(h);
	int sa=0, sb=0;
	for(int i=0; i<h; i++){
		cin>>a[i];
		sa+=a[i];
		va[i]=P(a[i], i);
	}
	for(int i=0; i<w; i++){
		cin>>b[i];
		sb+=b[i];
	}
	if(sa!=sb){
		cout<<":("<<endl;
		return 0;
	}
	sort(va.begin(), va.end(), greater<P>());
	int cnt[2020]={};
	bool dame=0;
	for(auto p:va){
		int i=p.second;
		vector<P> vb(w);
		for(int j=0; j<w; j++) vb[j]=P(b[j]-cnt[j], j);
		sort(vb.begin(), vb.end(), greater<P>());
		for(int j=0; j<p.first; j++){
			if(vb[j].first==0){
				dame=1; break;
			}
			z[i][vb[j].second]=1;
			cnt[vb[j].second]++;
		}
		if(dame) break;
	}
	if(dame){
		cout<<":("<<endl;
		return 0;
	}
	string ans[2020];
	for(int i=0; i<h; i++) for(int j=0; j<w; j++) ans[i]+='.';
	int k; cin>>k;
	int x[20], y[20];
	for(int i=0; i<k; i++){
		cin>>x[i]>>y[i];
		x[i]--; y[i]--;
		ans[x[i]][y[i]]='x';
	}
	int a1[2020]={}, b1[2020]={};
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			if(z[i][j] && ans[i][j]!='x'){
				a1[i]++, b1[j]++;
			}
		}
	}
	int s=h+w, t=s+1;
	int sa1=0;
	for(int i=0; i<h; i++){
		add_edge(s, i, a[i], a1[i]);
		sa1+=a1[i];
	}
	for(int i=0; i<w; i++){
		add_edge(i+h, t, b[i], b1[i]);
	}
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			if(ans[i][j]=='x') continue;
			add_edge(i, j+h, 1, z[i][j]);
		}
	}
	int f=max_flow(s, t);
	if(f<sa-sa1){
		cout<<":("<<endl;
		return 0;
	}
	cout<<"Yay!"<<endl;
	for(int i=0; i<h; i++){
		for(auto e:G[i]){
			if(e.to==s) continue;
			if(e.cap==0){
				ans[i][e.to-h]='o';
			}
		}
	}
	for(int i=0; i<h; i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
0