結果

問題 No.1123 Afforestation
ユーザー LayCurseLayCurse
提出日時 2020-07-22 22:42:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,236 ms / 2,500 ms
コード長 9,471 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 218,748 KB
実行使用メモリ 151,936 KB
最終ジャッジ日時 2024-06-22 23:34:03
合計ジャッジ時間 20,801 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 18 ms
6,912 KB
testcase_05 AC 382 ms
43,264 KB
testcase_06 AC 1,218 ms
151,552 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 8 ms
6,016 KB
testcase_12 AC 132 ms
16,256 KB
testcase_13 AC 1,203 ms
151,936 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 17 ms
7,168 KB
testcase_20 AC 454 ms
43,008 KB
testcase_21 AC 1,236 ms
151,296 KB
testcase_22 AC 1,175 ms
151,552 KB
testcase_23 AC 1,194 ms
151,808 KB
testcase_24 AC 1,230 ms
151,936 KB
testcase_25 AC 1,219 ms
151,680 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 6 ms
5,888 KB
testcase_43 AC 12 ms
7,168 KB
testcase_44 AC 27 ms
13,056 KB
testcase_45 AC 25 ms
12,928 KB
testcase_46 AC 35 ms
14,080 KB
testcase_47 AC 29 ms
14,080 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 3 ms
5,376 KB
testcase_52 AC 1 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
testcase_54 AC 1 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 3 ms
5,376 KB
testcase_57 AC 3 ms
5,376 KB
testcase_58 AC 6 ms
5,888 KB
testcase_59 AC 11 ms
7,040 KB
testcase_60 AC 25 ms
12,928 KB
testcase_61 AC 25 ms
13,056 KB
testcase_62 AC 33 ms
13,952 KB
testcase_63 AC 33 ms
13,952 KB
testcase_64 AC 2 ms
5,376 KB
testcase_65 AC 2 ms
5,376 KB
testcase_66 AC 2 ms
5,376 KB
testcase_67 AC 2 ms
5,376 KB
testcase_68 AC 3 ms
5,376 KB
testcase_69 AC 4 ms
5,376 KB
testcase_70 AC 6 ms
5,888 KB
testcase_71 AC 11 ms
7,040 KB
testcase_72 AC 1 ms
5,376 KB
testcase_73 AC 2 ms
5,376 KB
testcase_74 AC 2 ms
5,376 KB
testcase_75 AC 1 ms
5,376 KB
testcase_76 AC 2 ms
5,376 KB
testcase_77 AC 2 ms
5,376 KB
testcase_78 AC 1 ms
5,376 KB
testcase_79 AC 1 ms
5,376 KB
testcase_80 AC 1 ms
5,376 KB
testcase_81 AC 1 ms
5,376 KB
testcase_82 AC 1 ms
5,376 KB
testcase_83 AC 2 ms
5,376 KB
testcase_84 AC 836 ms
151,552 KB
testcase_85 AC 805 ms
151,808 KB
testcase_86 AC 2 ms
5,376 KB
testcase_87 AC 1 ms
5,376 KB
testcase_88 AC 278 ms
151,680 KB
testcase_89 AC 274 ms
151,808 KB
testcase_90 AC 3 ms
5,376 KB
testcase_91 AC 2 ms
5,376 KB
testcase_92 AC 2 ms
5,376 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