結果

問題 No.1123 Afforestation
ユーザー chocoruskchocorusk
提出日時 2020-08-01 17:08:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 739 ms / 2,500 ms
コード長 2,550 bytes
コンパイル時間 1,569 ms
コンパイル使用メモリ 134,728 KB
実行使用メモリ 204,368 KB
最終ジャッジ日時 2023-09-22 11:02:54
合計ジャッジ時間 21,606 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 23 ms
9,984 KB
testcase_05 AC 295 ms
54,112 KB
testcase_06 AC 681 ms
204,080 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 11 ms
6,144 KB
testcase_12 AC 159 ms
28,700 KB
testcase_13 AC 673 ms
204,368 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 23 ms
10,756 KB
testcase_20 AC 400 ms
53,868 KB
testcase_21 AC 676 ms
204,100 KB
testcase_22 AC 679 ms
203,804 KB
testcase_23 AC 674 ms
204,092 KB
testcase_24 AC 686 ms
204,152 KB
testcase_25 AC 695 ms
204,180 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 3 ms
4,384 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 8 ms
5,992 KB
testcase_43 AC 16 ms
10,560 KB
testcase_44 AC 30 ms
14,928 KB
testcase_45 AC 30 ms
14,660 KB
testcase_46 AC 40 ms
16,096 KB
testcase_47 AC 37 ms
16,080 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 3 ms
4,536 KB
testcase_51 AC 3 ms
4,384 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 4 ms
4,512 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 3 ms
4,380 KB
testcase_58 AC 8 ms
5,936 KB
testcase_59 AC 16 ms
10,600 KB
testcase_60 AC 31 ms
14,644 KB
testcase_61 AC 32 ms
14,676 KB
testcase_62 AC 41 ms
16,092 KB
testcase_63 AC 38 ms
16,160 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 2 ms
4,376 KB
testcase_67 AC 2 ms
4,380 KB
testcase_68 AC 4 ms
4,384 KB
testcase_69 AC 6 ms
5,300 KB
testcase_70 AC 8 ms
6,000 KB
testcase_71 AC 17 ms
10,664 KB
testcase_72 AC 1 ms
4,380 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 1 ms
4,380 KB
testcase_75 AC 1 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 1 ms
4,380 KB
testcase_79 AC 2 ms
4,380 KB
testcase_80 AC 2 ms
4,380 KB
testcase_81 AC 2 ms
4,380 KB
testcase_82 AC 1 ms
4,376 KB
testcase_83 AC 1 ms
4,376 KB
testcase_84 AC 739 ms
204,080 KB
testcase_85 AC 734 ms
204,164 KB
testcase_86 AC 2 ms
4,376 KB
testcase_87 AC 2 ms
4,376 KB
testcase_88 AC 339 ms
204,044 KB
testcase_89 AC 339 ms
204,196 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 2 ms
4,380 KB
testcase_92 AC 2 ms
4,380 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){
	edge e;
	e.to=to, e.cap=cap, e.rev=G[to].size();
	G[from].push_back(e);
	e.to=from, e.cap=0, 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 main()
{
	int h, w; cin>>h>>w;
	int a[2020], b[2020];
	int sa=0, sb=0;
	for(int i=0; i<h; i++){
		cin>>a[i];
		sa+=a[i];
	}
	for(int i=0; i<w; i++){
		cin>>b[i];
		sb+=b[i];
	}
	if(sa!=sb){
		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 s=h+w, t=s+1;
	for(int i=0; i<h; i++){
		add_edge(s, i, a[i]);
	}
	for(int i=0; i<w; i++){
		add_edge(i+h, t, b[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);
		}
	}
	int f=max_flow(s, t);
	if(f<sa){
		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