結果

問題 No.1123 Afforestation
ユーザー LayCurseLayCurse
提出日時 2020-07-22 22:42:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,278 ms / 2,500 ms
コード長 9,471 bytes
コンパイル時間 3,915 ms
コンパイル使用メモリ 217,580 KB
実行使用メモリ 153,044 KB
最終ジャッジ日時 2023-09-05 02:45:57
合計ジャッジ時間 23,601 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,556 KB
testcase_01 AC 3 ms
7,576 KB
testcase_02 AC 3 ms
7,580 KB
testcase_03 AC 4 ms
8,224 KB
testcase_04 AC 19 ms
10,896 KB
testcase_05 AC 414 ms
46,320 KB
testcase_06 AC 1,231 ms
152,972 KB
testcase_07 AC 2 ms
7,740 KB
testcase_08 AC 3 ms
7,584 KB
testcase_09 AC 3 ms
7,620 KB
testcase_10 AC 3 ms
7,884 KB
testcase_11 AC 10 ms
9,924 KB
testcase_12 AC 148 ms
20,128 KB
testcase_13 AC 1,250 ms
152,916 KB
testcase_14 AC 3 ms
7,468 KB
testcase_15 AC 2 ms
7,556 KB
testcase_16 AC 3 ms
7,544 KB
testcase_17 AC 3 ms
7,800 KB
testcase_18 AC 4 ms
8,204 KB
testcase_19 AC 19 ms
11,048 KB
testcase_20 AC 515 ms
46,016 KB
testcase_21 AC 1,268 ms
152,440 KB
testcase_22 AC 1,262 ms
152,728 KB
testcase_23 AC 1,222 ms
152,952 KB
testcase_24 AC 1,278 ms
152,852 KB
testcase_25 AC 1,232 ms
152,780 KB
testcase_26 AC 2 ms
7,536 KB
testcase_27 AC 2 ms
7,596 KB
testcase_28 AC 2 ms
7,524 KB
testcase_29 AC 3 ms
7,464 KB
testcase_30 AC 2 ms
7,660 KB
testcase_31 AC 3 ms
7,552 KB
testcase_32 AC 3 ms
7,524 KB
testcase_33 AC 2 ms
7,584 KB
testcase_34 AC 4 ms
8,232 KB
testcase_35 AC 3 ms
8,368 KB
testcase_36 AC 2 ms
7,536 KB
testcase_37 AC 2 ms
7,516 KB
testcase_38 AC 2 ms
7,608 KB
testcase_39 AC 4 ms
8,248 KB
testcase_40 AC 4 ms
8,180 KB
testcase_41 AC 3 ms
8,176 KB
testcase_42 AC 8 ms
9,784 KB
testcase_43 AC 12 ms
10,908 KB
testcase_44 AC 30 ms
16,728 KB
testcase_45 AC 30 ms
16,740 KB
testcase_46 AC 39 ms
17,736 KB
testcase_47 AC 34 ms
17,704 KB
testcase_48 AC 2 ms
7,464 KB
testcase_49 AC 3 ms
7,496 KB
testcase_50 AC 4 ms
8,236 KB
testcase_51 AC 4 ms
8,420 KB
testcase_52 AC 3 ms
7,532 KB
testcase_53 AC 2 ms
7,520 KB
testcase_54 AC 2 ms
7,540 KB
testcase_55 AC 4 ms
8,184 KB
testcase_56 AC 4 ms
8,212 KB
testcase_57 AC 4 ms
8,168 KB
testcase_58 AC 7 ms
9,864 KB
testcase_59 AC 13 ms
10,944 KB
testcase_60 AC 30 ms
16,960 KB
testcase_61 AC 29 ms
16,696 KB
testcase_62 AC 39 ms
17,844 KB
testcase_63 AC 40 ms
17,708 KB
testcase_64 AC 3 ms
7,588 KB
testcase_65 AC 2 ms
7,508 KB
testcase_66 AC 3 ms
7,724 KB
testcase_67 AC 3 ms
7,760 KB
testcase_68 AC 4 ms
8,256 KB
testcase_69 AC 5 ms
8,544 KB
testcase_70 AC 7 ms
9,832 KB
testcase_71 AC 13 ms
10,960 KB
testcase_72 AC 3 ms
7,484 KB
testcase_73 AC 2 ms
7,484 KB
testcase_74 AC 3 ms
7,512 KB
testcase_75 AC 2 ms
7,500 KB
testcase_76 AC 2 ms
7,612 KB
testcase_77 AC 2 ms
7,556 KB
testcase_78 AC 3 ms
7,528 KB
testcase_79 AC 3 ms
7,532 KB
testcase_80 AC 2 ms
7,516 KB
testcase_81 AC 2 ms
7,484 KB
testcase_82 AC 2 ms
7,720 KB
testcase_83 AC 2 ms
7,492 KB
testcase_84 AC 753 ms
152,748 KB
testcase_85 AC 750 ms
152,956 KB
testcase_86 AC 2 ms
7,464 KB
testcase_87 AC 2 ms
7,628 KB
testcase_88 AC 267 ms
152,648 KB
testcase_89 AC 262 ms
153,044 KB
testcase_90 AC 2 ms
7,472 KB
testcase_91 AC 2 ms
7,560 KB
testcase_92 AC 2 ms
7,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
template<class T, class S> struct maxflow{
  int node;
  int st;
  int ed;
  int *es;
  int *emem;
  int **edge;
  int **rev;
  int *level;
  int *qq;
  T **flow;
  T eps;
  void malloc(int N){
    int i;
    es = (int*)std::malloc(N*sizeof(int));
    emem = (int*)std::malloc(N*sizeof(int));
    level = (int*)std::malloc(N*sizeof(int));
    qq = (int*)std::malloc(N*sizeof(int));
    edge = (int**)std::malloc(N*sizeof(int*));
    rev = (int**)std::malloc(N*sizeof(int*));
    flow = (T**)std::malloc(N*sizeof(T*));
    for(i=(0);i<(N);i++){
      emem[i] = 0;
      edge[i] = rev[i] = NULL;
      flow[i] = NULL;
    }
  }
  void walloc(int N, void**mem = &wmem){
    int i;
    walloc1d(&es, N, mem);
    walloc1d(&emem, N, mem);
    walloc1d(&level, N, mem);
    walloc1d(&qq, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&rev, N, mem);
    walloc1d(&flow, N, mem);
    (*mem) = (flow + N);
  }
  void levelize(void){
    int i;
    int j;
    int k;
    int t;
    int q_st = 0;
    int q_ed = 1;
    for(i=(0);i<(node);i++){
      level[i] = -1;
    }
    level[st] = 0;
    qq[0] = st;
    while(q_st != q_ed){
      i = qq[q_st++];
      t = level[i] + 1;
      for(j=(0);j<(es[i]);j++){
        if(flow[i][j] > eps){
          k = edge[i][j];
          if(level[k]!=-1){
            continue;
          }
          level[k] = t;
          qq[q_ed++] = k;
          if(k==ed){
            return;
          }
        }
      }
    }
  }
  S pushflow(int i, S lim){
    int j;
    int k;
    int ji;
    S s;
    S t;
    S res = 0;
    if(i==ed){
      return lim;
    }
    for(j=(0);j<(es[i]);j++){
      if(flow[i][j] > eps){
        k = edge[i][j];
        if(level[k] != level[i]+1){
          continue;
        }
        s =min_L(lim, (S)flow[i][j]);
        t = pushflow(k, s);
        if(!t){
          continue;
        }
        res += t;
        lim -= t;
        ji = rev[i][j];
        flow[i][j] -= t;
        flow[k][ji] += t;
        if(!lim){
          break;
        }
      }
    }
    if(lim){
      level[i] = -1;
    }
    return res;
  }
  S solve(int st_, int ed_){
    S res = 0;
    st = st_;
    ed = ed_;
    for(;;){
      levelize();
      if(level[ed] == -1){
        break;
      }
      res += pushflow(st, numeric_limits<S>::max());
    }
    return res;
  }
  void init(int N){
    int i;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    eps = (T)1e-9;
  }
  void memoryExpand(int i, int sz){
    if(sz <= emem[i]){
      return;
    }
    sz =max_L(sz, max_L(3, emem[i]*2));
    emem[i]=sz;
    edge[i] = (int*)realloc(edge[i], sz*sizeof(int));
    rev[i] = (int*)realloc(rev[i], sz*sizeof(int));
    flow[i] = (T*)realloc(flow[i], sz*sizeof(T));
  }
  void addEdge(int n1, int n2, T f1, T f2 = 0){
    int s1 = es[n1]++;
    int s2 = es[n2]++;
    if(s1 >= emem[n1]){
      memoryExpand(n1, es[n1]);
    }
    if(s2 >= emem[n2]){
      memoryExpand(n2, es[n2]);
    }
    edge[n1][s1]=n2;
    edge[n2][s2]=n1;
    flow[n1][s1]=f1;
    flow[n2][s2]=f2;
    rev[n1][s1]=s2;
    rev[n2][s2]=s1;
  }
  void addEdgeAdv(int n1, int n2, T f1, T f2 = 0){
    int s1 = es[n1]++;
    int s2 = es[n2]++;
    edge[n1][s1]=n2;
    edge[n2][s2]=n1;
    flow[n1][s1]=f1;
    flow[n2][s2]=f2;
    rev[n1][s1]=s2;
    rev[n2][s2]=s1;
  }
  void setGraph(int N, int M, int n1[], int n2[], T f1[], T f2[]){
    int i;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[n1[i]]++;
      es[n2[i]]++;
    }
    for(i=(0);i<(N);i++){
      memoryExpand(i, es[i]);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      addEdgeAdv(n1[i], n2[i], f1[i], f2[i]);
    }
    eps = (T)1e-9;
  }
  void setGraph_w(int N, int M, int n1[], int n2[], T f1[], T f2[], void **mem = wmem){
    int i;
    int j;
    int k;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = emem[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[n1[i]]++;
      es[n2[i]]++;
    }
    edge[0] = (int*)(*mem);
    int HK5nwQQr = N;
    for(i=(1);i<(HK5nwQQr);i++){
      edge[i] = edge[i-1] + es[i-1];
    }
    rev[0] = edge[N-1] + es[N-1];
    int uaHhfk0v = N;
    for(i=(1);i<(uaHhfk0v);i++){
      rev[i] = rev[i-1] + es[i-1];
    }
    flow[0] = (T*)(rev[N-1] + es[N-1]);
    int vJNsb2nO = N;
    for(i=(1);i<(vJNsb2nO);i++){
      flow[i] = flow[i-1] + es[i-1];
    }
    *mem = (void*)(flow[N-1] + es[N-1]);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      addEdgeAdv(n1[i], n2[i], f1[i], f2[i]);
    }
    eps = (T)1e-9;
  }
}
;
int H;
int W;
int A[2000];
int B[2000];
int K;
int X[16];
int Y[16];
char res[2000][2001];
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int s;
  int flow;
  int node;
  int st;
  int ed;
  maxflow<int,int> f;
  rd(H);
  rd(W);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(H);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(W);e98WHCEY++){
      rd(B[e98WHCEY]);
    }
  }
  rd(K);
  {
    int FmcKpFmN;
    for(FmcKpFmN=(0);FmcKpFmN<(K);FmcKpFmN++){
      rd(X[FmcKpFmN]);X[FmcKpFmN] += (-1);
      rd(Y[FmcKpFmN]);Y[FmcKpFmN] += (-1);
    }
  }
  {
    int t_ynMSdg;
    int KrdatlYV;
    if(H==0){
      KrdatlYV = 0;
    }
    else{
      KrdatlYV = A[0];
      for(t_ynMSdg=(1);t_ynMSdg<(H);t_ynMSdg++){
        KrdatlYV += A[t_ynMSdg];
      }
    }
    s =KrdatlYV;
  }
  int a2conNHc;
  int hCmBdyQB;
  if(W==0){
    hCmBdyQB = 0;
  }
  else{
    hCmBdyQB = B[0];
    for(a2conNHc=(1);a2conNHc<(W);a2conNHc++){
      hCmBdyQB += B[a2conNHc];
    }
  }
  if(s !=hCmBdyQB){
    wt_L(":(");
    wt_L('\n');
    return 0;
  }
  for(i=(0);i<(H);i++){
    for(j=(0);j<(W);j++){
      res[i][j] = '.';
    }
  }
  for(i=(0);i<(K);i++){
    res[X[i]][Y[i]] = 'x';
  }
  node = H + W;
  st = node++;
  ed = node++;
  f.malloc(node);
  f.init(node);
  for(i=(0);i<(H);i++){
    f.addEdge(st, i, A[i]);
  }
  for(i=(0);i<(W);i++){
    f.addEdge(i+H, ed, B[i]);
  }
  for(i=(0);i<(H);i++){
    for(j=(0);j<(W);j++){
      if(res[i][j] != 'x'){
        f.addEdge(i,j+H, 1);
      }
    }
  }
  flow = f.solve(st,ed);
  if(flow != s){
    wt_L(":(");
    wt_L('\n');
    return 0;
  }
  for(i=(0);i<(H);i++){
    for(j=(0);j<(f.es[i]);j++){
      k = f.edge[i][j] - H;
      if(k < 0 || k >= W){
        continue;
      }
      if(f.flow[i][j]==0){
        res[i][k] = 'o';
      }
    }
  }
  wt_L("Yay!");
  wt_L('\n');
  {
    int ytthggxT;
    for(ytthggxT=(0);ytthggxT<(H);ytthggxT++){
      wt_L(res[ytthggxT]);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int H, W, A[2000], B[2000], K, X[16], Y[16];
// char res[2000][2001];
// {
//   int i, j, k, s, flow, node, st, ed;
//   maxflow<int,int> f;
//   rd(H,W,A(H),B(W),K,(X--,Y--)(K));
//   s = sum(A(H));
//   if(s != sum(B(W))) wt(":("), return 0;
// 
//   rep(i,H) rep(j,W) res[i][j] = '.';
//   rep(i,K) res[X[i]][Y[i]] = 'x';
// 
//   node = H + W;
//   st = node++;
//   ed = node++;
//   f.malloc(node);
//   f.init(node);
//   rep(i,H) f.addEdge(st, i, A[i]);
//   rep(i,W) f.addEdge(i+H, ed, B[i]);
//   rep(i,H) rep(j,W) if(res[i][j] != 'x') f.addEdge(i,j+H, 1);
// 
//   flow = f.solve(st,ed);
//   if(flow != s) wt(":("), return 0;
// 
//   rep(i,H) rep(j,f.es[i]){
//     k = f.edge[i][j] - H;
//     if(k < 0 || k >= W) continue;
//     if(f.flow[i][j]==0) res[i][k] = 'o';
//   }
// 
//   wt("Yay!");
//   wtLn(res(H));
// }
0