結果
| 問題 |
No.1123 Afforestation
|
| コンテスト | |
| ユーザー |
snuke
|
| 提出日時 | 2023-07-23 02:38:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,171 ms / 2,500 ms |
| コード長 | 9,026 bytes |
| コンパイル時間 | 2,575 ms |
| コンパイル使用メモリ | 221,340 KB |
| 最終ジャッジ日時 | 2025-02-15 18:20:10 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 90 |
コンパイルメッセージ
main.cpp: In function ‘int geti()’:
main.cpp:26:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
26 | int geti(){int x;scanf("%d",&x);return x;}
| ~~~~~^~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:142:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
142 | scanf("%d%d",&h,&w);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:146:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
146 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rep1(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define newline puts("")
#define vc vector
using namespace std;
template<class T> using vv = vc<vc<T>>;
template<class T> using PQ = priority_queue<T,vc<T>,greater<T>>;
using uint = unsigned; using ull = unsigned long long;
using vi = vc<int>; using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long; using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>;
using P = pair<int,int>; using vp = vc<P>; using vvp = vv<P>;
int geti(){int x;scanf("%d",&x);return x;}
vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;}
template<class T1,class T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<class T1,class T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<class T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<class T>string join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}
template<class T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;}
template<class T>void vin(vv<T>& a){int n,m;cin>>n>>m;a=vv<T>(n,vc<T>(m));cin>>a;}
template<class T>void vin(vc<T>& a){int n;cin>>n;a=vc<T>(n);cin>>a;}
template<class T1,class T2>void operator--(pair<T1,T2>&a,int){a.fi--;a.se--;}
template<class T1,class T2>void operator++(pair<T1,T2>&a,int){a.fi++;a.se++;}
template<class T>void operator--(vc<T>&a,int){for(T&x:a)x--;}
template<class T>void operator++(vc<T>&a,int){for(T&x:a)x++;}
template<class T>void operator+=(vc<T>&a,T b){for(T&x:a)x+=b;}
template<class T>void operator-=(vc<T>&a,T b){for(T&x:a)x-=b;}
template<class T>void operator*=(vc<T>&a,T b){for(T&x:a)x*=b;}
template<class T>void operator/=(vc<T>&a,T b){for(T&x:a)x/=b;}
template<class T>void operator+=(vc<T>&a,const vc<T>&b){a.insert(a.end(),rng(b));}
template<class T1,class T2>bool mins(T1& x,const T2&y){if(y<x){x=y;return true;}else return false;}
template<class T1,class T2>bool maxs(T1& x,const T2&y){if(x<y){x=y;return true;}else return false;}
template<class T>T min(const vector<T>& a){return *min_element(rng(a));}
template<class T>T max(const vector<T>& a){return *max_element(rng(a));}
template<class Tx,class Ty>Tx dup(Tx x, Ty y){return (x+y-1)/y;}
template<class T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
template<class T>ll suma(const vv<T>&a){ll res(0);for(auto&&x:a)res+=suma(x);return res;}
template<class T>void uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
template<class T>void prepend(vc<T>&a,const T&x){a.insert(a.begin(),x);}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts(":("); return 0;}
#define yes { puts("Yes"); return 0;}
#define no { puts("No"); return 0;}
#define rtn(x) { cout<<(x)<<endl; return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
using vs = vc<string>;
void vin(vs& s){int n,m;cin>>n>>m;s=vs(n);cin>>s;}
// Max flow
// !! Be care of double and INF !!
struct Maxflow {
typedef int TT;
int n;
vi to, next, head, dist, it;
vector<TT> lim;
Maxflow(){}
Maxflow(int n):n(n),head(n,-1),it(n){}
void add(int a, int b, TT c=1) {
next.pb(head[a]); head[a] = sz(to); to.pb(b); lim.pb(c);
next.pb(head[b]); head[b] = sz(to); to.pb(a); lim.pb(0);
}
void bfs(int sv) {
dist = vi(n,INF);
queue<int> q;
dist[sv] = 0; q.push(sv);
while (!q.empty()){
int v = q.front(); q.pop();
for (int i = head[v]; i != -1; i = next[i]) {
if (lim[i] && dist[to[i]] == INF) { // double !!
dist[to[i]] = dist[v]+1; q.push(to[i]);
}
}
}
}
TT dfs(int v, int tv, TT nf=INF) { // INF !!
if (v == tv) return nf;
for (; it[v] != -1; it[v] = next[it[v]]) {
int u = to[it[v]]; TT f;
if (!lim[it[v]] || dist[v] >= dist[u]) continue; // double !!
if (f = dfs(u, tv, min(nf, lim[it[v]])), f) { // double !!
lim[it[v]] -= f;
lim[it[v]^1] += f;
return f;
}
}
return 0;
}
TT solve(int sv, int tv) {
TT flow = 0, f;
while (1) {
bfs(sv);
if (dist[tv] == INF) return flow;
rep(i,n) it[i] = head[i];
while (f = dfs(sv,tv), f) flow += f;
}
}
};
//
// coordinate compression
struct X {
typedef int T;
vector<T> d;
X() {}
// X(vector<T>& a): d(a) {
// init();
// for (T& na : a) na = (*this)(na);
// }
void add(const T& x) { d.pb(x);}
void init() {
sort(rng(d));
d.erase(unique(rng(d)), d.end());
}
int size() const { return sz(d);}
T operator[](int i) const { return d[i];}
int operator()(const T& x) const { return upper_bound(rng(d),x)-d.begin()-1;}
bool in(const T& x) const { return binary_search(rng(d), x);}
};
//
int main() {
int h,w;
scanf("%d%d",&h,&w);
vi a(h), b(w);
cin>>a>>b;
int n;
scanf("%d",&n);
vp p(n); cin>>p;
rep(i,n) p[i].fi--;
rep(i,n) p[i].se--;
// X xs, ys;
// rep(i,n) xs.add(p[i].fi);
// rep(i,n) ys.add(p[i].se);
// xs.init();
// ys.init();
// int eh = sz(xs), dh = h-eh;
// int ew = sz(ys), dw = w-ew;
vs ans(h,string(w,'.'));
rep(i,n) ans[p[i].fi][p[i].se] = 'x';
// int sv = h+w, tv = sv+1;
// Maxflow g(tv+1);
// rep(ei,eh) {
// int i = xs[ei];
// rep(j,w) {
// if (ys.in(j)) continue;
// g.add(i,h+j);
// }
// }
// rep(ej,ew) {
// int j = ys[ej];
// rep(i,h) {
// if (xs.in(i)) continue;
// g.add(i,h+j);
// }
// }
// rep(i,h) {
// if (xs.in(i)) g.add(sv,i,a[i]);
// else {
// int now = max(0,a[i]-dw);
// g.add(sv,i,now);
// }
// }
// rep(j,w) {
// if (ys.in(j)) g.add(h+j,tv,b[j]);
// else {
// int now = max(0,b[j]-dh);
// g.add(h+j,tv,now);
// }
// }
// g.solve(sv,tv);
// vi ta = a, tb = b;
// int e = 0;
// rep(ei,eh) {
// int i = xs[ei];
// rep(j,w) {
// if (ys.in(j)) continue;
// if (!g.lim[e]) tb[j]--;
// e += 2;
// }
// }
// rep(ej,ew) {
// int j = ys[ej];
// rep(i,h) {
// if (xs.in(i)) continue;
// if (!g.lim[e]) ta[i]--;
// e += 2;
// }
// }
// rep(i,h) if (!xs.in(i) && ta[i] > dw) dame;
// rep(j,w) if (!ys.in(j) && tb[j] > dh) dame;
// int ne = sz(g.lim);
// rep(ei,eh) {
// int i = xs[ei];
// rep(ej,ew) {
// int j = ys[ej];
// if (ans[i][j] == 'x') continue;
// g.add(i,h+j);
// }
// }
// rep(i,h) {
// if (xs.in(i)) continue;
// g.add(sv,i, min(a[i],dw));
// }
// rep(j,w) {
// if (ys.in(j)) continue;
// g.add(h+j,tv, min(b[j],dh));
// }
// g.solve(sv,tv);
auto add = [&](int i, int j) {
ans[i][j] = 'o';
a[i]--;
b[j]--;
};
// e = 0;
// rep(ei,eh) {
// int i = xs[ei];
// rep(j,w) {
// if (ys.in(j)) continue;
// if (!g.lim[e]) add(i,j);
// e += 2;
// }
// }
// rep(ej,ew) {
// int j = ys[ej];
// rep(i,h) {
// if (xs.in(i)) continue;
// if (!g.lim[e]) add(i,j);
// e += 2;
// }
// }
// e = ne;
// rep(ei,eh) {
// int i = xs[ei];
// rep(ej,ew) {
// int j = ys[ej];
// if (ans[i][j] == 'x') continue;
// if (!g.lim[e]) add(i,j);
// e += 2;
// }
// }
// rep(ei,eh) if (a[xs[ei]]) dame;
// rep(ej,ew) if (b[ys[ej]]) dame;
// vi is, js;
// rep(i,h) if (!xs.in(i)) is.pb(i);
// rep(j,w) if (!ys.in(j)) js.pb(j);
// for (int i : is) {
// vp nj;
// for (int j : js) nj.eb(b[j],j);
// sort(rrng(nj));
// int na = a[i];
// rep(k,na) add(i,nj[k].se);
// }
int sv = h+w, tv = sv+1;
Maxflow g(tv+1);
vi oa = a, ob = b;
vvi ng(h,vi(w));
rep(i,h) {
vp nj;
rep(j,w) nj.eb(b[j],j);
sort(rrng(nj));
rep(k,a[i]) {
int j = nj[k].se;
ng[i][j] = 1;
b[j]--;
}
a[i] = 0;
}
rep(j,w) if (b[j]) dame;
int e = 0;
rep(i,h)rep(j,w) {
if (ans[i][j] == 'x') {
if (!ng[i][j]) continue;
a[i]++; b[j]++;
} else {
g.add(i,h+j);
if (ng[i][j]) {
g.lim[e] = 0;
g.lim[e^1] = 1;
}
e += 2;
}
}
rep(i,h) if (a[i]) g.add(sv,i,a[i]);
rep(j,w) if (b[j]) g.add(h+j,tv,b[j]);
g.solve(sv,tv);
a = oa; b = ob;
e = 0;
rep(i,h)rep(j,w) {
if (ans[i][j] == 'x') continue;
if (!g.lim[e]) add(i,j);
e += 2;
}
rep(i,h) if (a[i]) dame;
rep(j,w) if (b[j]) dame;
cout<<"Yay!"<<endl;
rep(i,h) cout<<ans[i]<<endl;
return 0;
}
snuke