結果
問題 | No.912 赤黒木 |
ユーザー | LayCurse |
提出日時 | 2019-11-02 11:33:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 60 ms / 3,000 ms |
コード長 | 5,890 bytes |
コンパイル時間 | 2,786 ms |
コンパイル使用メモリ | 217,828 KB |
実行使用メモリ | 22,888 KB |
最終ジャッジ日時 | 2024-09-14 23:03:49 |
合計ジャッジ時間 | 6,779 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 56 ms
19,712 KB |
testcase_13 | AC | 60 ms
20,224 KB |
testcase_14 | AC | 51 ms
22,096 KB |
testcase_15 | AC | 55 ms
22,480 KB |
testcase_16 | AC | 54 ms
20,684 KB |
testcase_17 | AC | 54 ms
20,816 KB |
testcase_18 | AC | 52 ms
22,092 KB |
testcase_19 | AC | 51 ms
22,476 KB |
testcase_20 | AC | 51 ms
20,816 KB |
testcase_21 | AC | 52 ms
20,940 KB |
testcase_22 | AC | 40 ms
21,068 KB |
testcase_23 | AC | 40 ms
19,916 KB |
testcase_24 | AC | 39 ms
21,964 KB |
testcase_25 | AC | 54 ms
22,600 KB |
testcase_26 | AC | 55 ms
22,728 KB |
testcase_27 | AC | 52 ms
20,560 KB |
testcase_28 | AC | 56 ms
22,888 KB |
testcase_29 | AC | 56 ms
21,840 KB |
testcase_30 | AC | 58 ms
21,584 KB |
testcase_31 | AC | 57 ms
20,812 KB |
ソースコード
#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); // }