結果

問題 No.1078 I love Matrix Construction
ユーザー LayCurseLayCurse
提出日時 2020-06-12 22:24:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,192 bytes
コンパイル時間 3,422 ms
コンパイル使用メモリ 223,032 KB
実行使用メモリ 45,516 KB
最終ジャッジ日時 2024-06-24 05:16:13
合計ジャッジ時間 7,638 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 45 ms
21,120 KB
testcase_06 AC 9 ms
7,808 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 31 ms
16,768 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 114 ms
33,408 KB
testcase_11 WA -
testcase_12 AC 67 ms
28,032 KB
testcase_13 WA -
testcase_14 AC 58 ms
22,656 KB
testcase_15 AC 84 ms
29,824 KB
testcase_16 AC 8 ms
12,368 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 WA -
testcase_19 AC 18 ms
10,752 KB
testcase_20 AC 17 ms
10,624 KB
testcase_21 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
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');
  }
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  a[k] = aval;
  b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  for(i=sz-1;i>k;i--){
    d[i] = d[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
struct graph{
  int N;
  int *es;
  int **edge;
  void setDirectEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&edge[0], M, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
    }
  }
  graph reverse(void **mem = &wmem){
    int i;
    int j;
    int k;
    graph g;
    g.N = N;
    walloc1d(&g.es, N, mem);
    walloc1d(&g.edge, N, mem);
    for(i=(0);i<(N);i++){
      g.es[i] = 0;
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        g.es[edge[i][j]]++;
      }
    }
    for(i=(0);i<(N);i++){
      walloc1d(&g.edge[i], g.es[i], mem);
    }
    for(i=(0);i<(N);i++){
      g.es[i] = 0;
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        g.edge[k][g.es[k]++] = i;
      }
    }
    return g;
  }
  inline int sccDFS(int num[], int st, int mx){
    int i;
    int j;
    num[st]=-2;
    for(i=(0);i<(es[st]);i++){
      j=edge[st][i];
      if(num[j]==-1){
        mx=sccDFS(num,j,mx);
      }
    }
    num[st]=mx;
    return mx+1;
  }
  int scc(int res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int ret=0;
    graph r;
    int *st;
    int st_size;
    int *num;
    int *nrv;
    r = reverse(&mem);
    walloc1d(&st, N, &mem);
    walloc1d(&num, N, &mem);
    walloc1d(&nrv, N, &mem);
    for(i=(0);i<(N);i++){
      res[i] = num[i] = -1;
    }
    k = 0;
    for(i=(0);i<(N);i++){
      if(num[i]==-1){
        k = sccDFS(num,i,k);
      }
    }
    for(i=(0);i<(N);i++){
      nrv[num[i]] = i;
    }
    for(k=N-1;k>=0;k--){
      i=nrv[k];
      if(res[i]>=0){
        continue;
      }
      res[i]=ret;
      st_size=0;
      st[st_size++]=i;
      while(st_size){
        i=st[--st_size];
        for(j=(0);j<(r.es[i]);j++){
          if(res[r.edge[i][j]]==-1){
            res[r.edge[i][j]]=ret;
            st[st_size++]=r.edge[i][j];
          }
        }
      }
      ret++;
    }
    return ret;
  }
  int TopologicalSort(int res[], void *mem=wmem){
    int i;
    int j;
    int k;
    int rs;
    int *deg;
    int *q;
    int qs = 0;
    int qe = 0;
    walloc1d(&deg, N, &mem);
    walloc1d(&q, N, &mem);
    rs = 0;
    for(i=(0);i<(N);i++){
      deg[i] = 0;
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        deg[edge[i][j]]++;
      }
    }
    for(i=(0);i<(N);i++){
      if(deg[i]==0){
        q[qe++] = i;
      }
    }
    while(qs < qe){
      i = q[qs++];
      res[rs++] = i;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        deg[k]--;
        if(deg[k]==0){
          q[qe++] = k;
        }
      }
    }
    if(rs==N){
      return 1;
    }
    return 0;
  }
}
;
int N;
int S[500];
int T[500];
int U[500];
int n;
int m;
int a[1000000];
int b[1000000];
int n2;
int m2;
int a2[1000000];
int b2[1000000];
int con[1000000];
int con2[1000000];
int rev[1000000];
int res[500][500];
graph g;
graph g2;
int main(){
  int i;
  wmem = memarr;
  int x;
  int y;
  int z;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(S[Lj4PdHRW]);S[Lj4PdHRW] += (-1);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(N);e98WHCEY++){
      rd(T[e98WHCEY]);T[e98WHCEY] += (-1);
    }
  }
  {
    int FmcKpFmN;
    for(FmcKpFmN=(0);FmcKpFmN<(N);FmcKpFmN++){
      rd(U[FmcKpFmN]);
    }
  }
  n = 2*N*N;
  for(i=(0);i<(N);i++){
    int j;
    for(j=(0);j<(N);j++){
      x = S[i] * N + j;
      y = j * N + T[i];
      z = U[i];
      if(z%2==0){
        if(z/2==0){
          arrInsert(m, m, a, x +0, b, y +N*N);
        }
        else{
          arrInsert(m, m, a, x +0, b, y +0);
        }
      }
      else{
        if(z/2==0){
          arrInsert(m, m, a, x +N*N, b, y +N*N);
        }
        else{
          arrInsert(m, m, a, x +N*N, b, y +0);
        }
      }
      if(z/2==0){
        if(z%2==0){
          arrInsert(m, m, a, y +0, b, x +N*N);
        }
        else{
          arrInsert(m, m, a, y +0, b, x +0);
        }
      }
      else{
        if(z%2==0){
          arrInsert(m, m, a, y +N*N, b, x +N*N);
        }
        else{
          arrInsert(m, m, a, y +N*N, b, x +0);
        }
      }
    }
  }
  g.setDirectEdge(n, m, a, b);
  n2 = g.scc(con);
  for(i=(0);i<(N);i++){
    if(con[i]==con[i+N*N]){
      wt_L(-1);
      wt_L('\n');
      return 0;
    }
  }
  for(i=(0);i<(m);i++){
    if(con[a[i]] != con[b[i]]){
      arrInsert(m2, m2, a2, con[a[i]], b2, con[b[i]]);
    }
  }
  g2.setDirectEdge(n2, m2, a2, b2);
  g2.TopologicalSort(con2);
  for(i=(0);i<(n2);i++){
    rev[con2[i]] = i;
  }
  for(i=(0);i<(N*N);i++){
    if(rev[con[i]] < rev[con[i+N*N]]){
      res[i/N][i%N] = 1;
    }
  }
  {
    int iMWUTgY_;
    int AlM5nNnR;
    for(iMWUTgY_=(0);iMWUTgY_<(N);iMWUTgY_++){
      if(N==0){
        wt_L('\n');
      }
      else{
        for(AlM5nNnR=(0);AlM5nNnR<(N-1);AlM5nNnR++){
          wt_L(res[iMWUTgY_][AlM5nNnR]);
          wt_L(' ');
        }
        wt_L(res[iMWUTgY_][AlM5nNnR]);
        wt_L('\n');
      }
    }
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, S[500], T[500], U[500];
// int n, m, a[1d6], b[1d6];
// int n2, m2, a2[1d6], b2[1d6];
// int con[1d6], con2[1d6], rev[1d6], res[500][500];
// graph g, g2;
// {
//   int x, y, z;
//   rd(N,(S--)(N),(T--)(N),U(N));
//   n = 2*N*N;
//   rep(i,N) rep(j,N){
//     x = S[i] * N + j;
//     y = j * N + T[i];
//     z = U[i];
//     arrInsert(m, m, a, x + if[z%2==0, 0, N*N], b, y + if[z/2==0, N*N, 0]);
//     arrInsert(m, m, a, y + if[z/2==0, 0, N*N], b, x + if[z%2==0, N*N, 0]);
//   }
//   g.setDirectEdge(n, m, a, b);
//   n2 = g.scc(con);
//   rep(i,N) if(con[i]==con[i+N*N]) wt(-1), return 0;
//   rep(i,m) if(con[a[i]] != con[b[i]]) arrInsert(m2, m2, a2, con[a[i]], b2, con[b[i]]);
//   g2.setDirectEdge(n2, m2, a2, b2);
//   g2.TopologicalSort(con2);
//   rep(i,n2) rev[con2[i]] = i;
//   rep(i,N*N){
//     if(rev[con[i]] < rev[con[i+N*N]]) res[i/N][i%N] = 1;
//   }
//   wt(res(N,N));
// }
0