結果

問題 No.912 赤黒木
ユーザー LayCurseLayCurse
提出日時 2019-10-18 22:50:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 3,000 ms
コード長 5,925 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 214,516 KB
実行使用メモリ 23,956 KB
最終ジャッジ日時 2023-09-08 00:57:44
合計ジャッジ時間 5,615 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,560 KB
testcase_01 AC 3 ms
9,524 KB
testcase_02 AC 3 ms
9,500 KB
testcase_03 AC 3 ms
9,500 KB
testcase_04 AC 3 ms
9,556 KB
testcase_05 AC 3 ms
9,652 KB
testcase_06 AC 3 ms
9,512 KB
testcase_07 AC 3 ms
9,520 KB
testcase_08 AC 2 ms
9,560 KB
testcase_09 AC 3 ms
9,780 KB
testcase_10 AC 3 ms
9,768 KB
testcase_11 AC 3 ms
9,560 KB
testcase_12 AC 51 ms
21,728 KB
testcase_13 AC 52 ms
23,956 KB
testcase_14 AC 49 ms
21,760 KB
testcase_15 AC 53 ms
23,828 KB
testcase_16 AC 50 ms
21,708 KB
testcase_17 AC 51 ms
21,724 KB
testcase_18 AC 50 ms
21,688 KB
testcase_19 AC 48 ms
21,632 KB
testcase_20 AC 48 ms
21,700 KB
testcase_21 AC 47 ms
21,608 KB
testcase_22 AC 38 ms
21,048 KB
testcase_23 AC 38 ms
21,052 KB
testcase_24 AC 41 ms
21,132 KB
testcase_25 AC 51 ms
21,772 KB
testcase_26 AC 52 ms
23,800 KB
testcase_27 AC 51 ms
21,956 KB
testcase_28 AC 53 ms
21,804 KB
testcase_29 AC 55 ms
21,764 KB
testcase_30 AC 53 ms
21,712 KB
testcase_31 AC 50 ms
21,640 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 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 void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
inline void wt_L(char a){
  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){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
struct graph{
  int N;
  int *es;
  int **edge;
  void setEdgeRootedTree(int N__, int M, int A[], int B[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){
    int i;
    int j;
    int k;
    int *dist;
    int *q;
    int qs;
    int qe;
    int *ind;
    void *tmem;
    N = N__;
    tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * N);
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], &tmem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
    walloc1d(&dist, N, &tmem);
    walloc1d(&q, N, &tmem);
    walloc1d(&ind, N, &tmem);
    if(cnv==NULL){
      walloc1d(&cnv, N, &tmem);
    }
    for(i=(0);i<(N);i++){
      dist[i] = -1;
    }
    dist[root] = 0;
    qs = qe = 0;
    q[qe++] = root;
    while(qs < qe){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(dist[k]==-1){
          dist[k] = dist[i] + 1;
          q[qe++] = k;
        }
      }
    }
    if(reorder == 0){
      for(i=(0);i<(N);i++){
        cnv[i] = i;
      }
      for(i=(0);i<(N);i++){
        ind[i] = i;
      }
    }
    else{
      for(i=(0);i<(N);i++){
        cnv[i] = q[i];
      }
      for(i=(0);i<(N);i++){
        ind[cnv[i]] = i;
      }
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      es[ind[j]]++;
    }
    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++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      j = ind[j];
      k = ind[k];
      edge[j][es[j]++] = k;
    }
  }
}
;
int N;
int A[200000];
int B[200000];
int deg[200000];
int cnv[200000];
int rev[200000];
int od[200000];
int dp[3][200000];
int nx[3];
int main(){
  int KL2GvlyY;
  wmem = memarr;
  int i;
  int j;
  int k;
  int res;
  graph g;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N-1);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
    }
  }
  g.setEdgeRootedTree(N, N-1, A, B, 0, 1, cnv);
  for(i=(0);i<(N);i++){
    rev[cnv[i]] = i;
  }
  for(KL2GvlyY=(0);KL2GvlyY<(2*N-2);KL2GvlyY++){
    deg[rev[rd_int()-1]] ^= 1;
  }
  for(i=(1);i<(3);i++){
    for(j=(0);j<(N);j++){
      dp[i][j] = 1073709056;
    }
  }
  for(i=(N)-1;i>=(0);i--){
    od[i] = deg[i];
    for(j=(0);j<(g.es[i]);j++){
      k = g.edge[i][j];
      od[i] ^= od[k];
    }
    for(j=(0);j<(g.es[i]);j++){
      int x;
      k = g.edge[i][j];
      for(x=(0);x<(3);x++){
        nx[x] = 1073709056;
      }
      for(x=(0);x<(3);x++){
        int y;
        for(y=(0);y<(3);y++){
          if(x+y <= 2){
            chmin(nx[x+y], dp[x][i] + dp[y][k] + (od[k] + y) % 2);
          }
        }
      }
      if(deg[k]){
        for(x=(0);x<(2);x++){
          int y;
          for(y=(0);y<(2);y++){
            if(x+y+1 <= 2){
              chmin(nx[x+y+1], dp[x][i] + dp[y][k] + (od[k] + y + 1) % 2);
            }
          }
        }
      }
      for(x=(0);x<(3);x++){
        dp[x][i] = nx[x];
      }
    }
  }
  res = N - 1 +min_L(dp[0][0], dp[2][0]);
  if(deg[0]){
    chmin(res, N - 1 + dp[1][0]);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20191012-1

// --- original code ---
// int N, A[2d5], B[2d5], deg[2d5], cnv[2d5], rev[2d5];
// int od[2d5], dp[3][2d5], nx[3];
// {
//   int i, j, k, res;
//   graph g;
//   rd(N,(A--,B--)(N-1));
//   g.setEdgeRootedTree(N, N-1, A, B, 0, 1, cnv);
//   rep(i,N) rev[cnv[i]] = i;
//   rep(2N-2) deg[rev[rd_int()-1]] ^= 1;
//   rep(i,1,3) rep(j,N) dp[i][j] = int_inf;
//   rrep(i,N){
//     od[i] = deg[i];
//     rep(j,g.es[i]){
//       k = g.edge[i][j];
//       od[i] ^= od[k];
//     }
//     rep(j,g.es[i]){
//       k = g.edge[i][j];
//       rep(x,3) nx[x] = int_inf;
//       rep(x,3) rep(y,3) if(x+y <= 2){
//         nx[x+y] <?= dp[x][i] + dp[y][k] + (od[k] + y) % 2;
//       }
//       if(deg[k]) rep(x,2) rep(y,2) if(x+y+1 <= 2){
//         nx[x+y+1] <?= dp[x][i] + dp[y][k] + (od[k] + y + 1) % 2;
//       }
//       rep(x,3) dp[x][i] = nx[x];
//     }
// //    wt(i,":",deg[i],":",dp[0][i],dp[1][i],dp[2][i]);
//   }
//   res = N - 1 + min(dp[0][0], dp[2][0]);
//   if(deg[0]) res <?= N - 1 + dp[1][0];
//   wt(res);
// }
0