結果

問題 No.912 赤黒木
ユーザー LayCurseLayCurse
提出日時 2019-11-02 11:33:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 3,000 ms
コード長 5,890 bytes
コンパイル時間 3,121 ms
コンパイル使用メモリ 213,636 KB
実行使用メモリ 23,924 KB
最終ジャッジ日時 2023-10-13 01:14:08
合計ジャッジ時間 7,283 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,500 KB
testcase_01 AC 3 ms
9,612 KB
testcase_02 AC 3 ms
9,672 KB
testcase_03 AC 3 ms
9,512 KB
testcase_04 AC 3 ms
9,500 KB
testcase_05 AC 3 ms
9,572 KB
testcase_06 AC 3 ms
9,564 KB
testcase_07 AC 3 ms
9,512 KB
testcase_08 AC 3 ms
9,488 KB
testcase_09 AC 3 ms
9,504 KB
testcase_10 AC 3 ms
9,560 KB
testcase_11 AC 3 ms
9,516 KB
testcase_12 AC 53 ms
21,876 KB
testcase_13 AC 55 ms
23,924 KB
testcase_14 AC 51 ms
21,680 KB
testcase_15 AC 55 ms
23,880 KB
testcase_16 AC 53 ms
21,712 KB
testcase_17 AC 53 ms
21,740 KB
testcase_18 AC 51 ms
21,660 KB
testcase_19 AC 50 ms
21,880 KB
testcase_20 AC 49 ms
21,700 KB
testcase_21 AC 50 ms
21,776 KB
testcase_22 AC 39 ms
20,996 KB
testcase_23 AC 39 ms
20,988 KB
testcase_24 AC 39 ms
21,144 KB
testcase_25 AC 53 ms
21,728 KB
testcase_26 AC 55 ms
23,864 KB
testcase_27 AC 53 ms
21,760 KB
testcase_28 AC 58 ms
21,732 KB
testcase_29 AC 58 ms
21,708 KB
testcase_30 AC 55 ms
21,724 KB
testcase_31 AC 51 ms
21,636 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, i;
  wmem = memarr;
  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++){
    int j;
    for(j=(0);j<(N);j++){
      dp[i][j] = 1073709056;
    }
  }
  for(i=(N)-1;i>=(0);i--){
    int Q5VJL1cS, e98WHCEY;
    od[i] = deg[i];
    for(Q5VJL1cS=(0);Q5VJL1cS<(g.es[i]);Q5VJL1cS++){
      auto &k = g.edge[i][Q5VJL1cS];
      od[i] ^= od[k];
    }
    for(e98WHCEY=(0);e98WHCEY<(g.es[i]);e98WHCEY++){
      int x;
      auto &k = g.edge[i][e98WHCEY];
      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 20191102-1

// --- original code ---
// int N, A[2d5], B[2d5], deg[2d5], cnv[2d5], rev[2d5];
// int od[2d5], dp[3][2d5], nx[3];
// {
//   int 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[g.edge[i]](k,g.es[i]) od[i] ^= od[k];
//     rep[g.edge[i]](k,g.es[i]){
//       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];
//     }
//   }
//   res = N - 1 + min(dp[0][0], dp[2][0]);
//   if(deg[0]) res <?= N - 1 + dp[1][0];
//   wt(res);
// }
0