結果

問題 No.1123 Afforestation
ユーザー snukesnuke
提出日時 2023-07-23 02:37:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,027 bytes
コンパイル時間 3,209 ms
コンパイル使用メモリ 229,416 KB
実行使用メモリ 117,344 KB
最終ジャッジ日時 2023-10-24 09:11:15
合計ジャッジ時間 22,851 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 11 ms
6,148 KB
testcase_05 AC 113 ms
32,660 KB
testcase_06 AC 508 ms
117,344 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 9 ms
4,884 KB
testcase_12 AC 150 ms
15,104 KB
testcase_13 AC 1,151 ms
117,344 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 19 ms
6,400 KB
testcase_20 AC 472 ms
31,924 KB
testcase_21 AC 1,165 ms
117,108 KB
testcase_22 AC 1,249 ms
117,112 KB
testcase_23 AC 1,141 ms
117,344 KB
testcase_24 AC 1,015 ms
117,344 KB
testcase_25 AC 1,076 ms
117,344 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 4 ms
4,348 KB
testcase_41 AC 4 ms
4,348 KB
testcase_42 AC 9 ms
4,868 KB
testcase_43 AC 17 ms
6,400 KB
testcase_44 AC 41 ms
9,428 KB
testcase_45 AC 41 ms
9,428 KB
testcase_46 AC 50 ms
10,728 KB
testcase_47 AC 52 ms
10,728 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 4 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 4 ms
4,348 KB
testcase_56 AC 3 ms
4,348 KB
testcase_57 AC 4 ms
4,348 KB
testcase_58 AC 9 ms
4,868 KB
testcase_59 AC 18 ms
6,400 KB
testcase_60 AC 39 ms
9,428 KB
testcase_61 AC 41 ms
9,428 KB
testcase_62 AC 49 ms
10,728 KB
testcase_63 AC 53 ms
10,728 KB
testcase_64 AC 2 ms
4,348 KB
testcase_65 AC 2 ms
4,348 KB
testcase_66 AC 2 ms
4,348 KB
testcase_67 AC 3 ms
4,348 KB
testcase_68 AC 4 ms
4,348 KB
testcase_69 AC 5 ms
4,372 KB
testcase_70 AC 8 ms
4,868 KB
testcase_71 AC 18 ms
6,400 KB
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 AC 2 ms
4,348 KB
testcase_76 WA -
testcase_77 AC 2 ms
4,348 KB
testcase_78 AC 2 ms
4,348 KB
testcase_79 WA -
testcase_80 AC 2 ms
4,348 KB
testcase_81 WA -
testcase_82 AC 2 ms
4,348 KB
testcase_83 AC 2 ms
4,348 KB
testcase_84 AC 239 ms
117,344 KB
testcase_85 AC 263 ms
117,344 KB
testcase_86 AC 2 ms
4,348 KB
testcase_87 AC 2 ms
4,348 KB
testcase_88 AC 224 ms
117,344 KB
testcase_89 AC 222 ms
117,344 KB
testcase_90 AC 2 ms
4,348 KB
testcase_91 WA -
testcase_92 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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("-1"); 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;
}



















0