1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" using namespace std; #define MAXN 40006 #define B 206 #define swap( a , b ) ( (a) ^= (b) , (b) ^= (a) , (a) ^= (b) ) int n , m , blo , sz , bl; int c[MAXN] , C[MAXN]; int head[MAXN] , to[MAXN << 1] , nex[MAXN << 1] , ecn; void ade( int u , int v ) { to[++ ecn] = v , nex[ecn] = head[u] , head[u] = ecn; } int ps[B] , im[MAXN] , pre[B][B] , cid , ee; int w[MAXN] , an; void dfs( int u , int fa ) { if( !w[c[u]] ) ++ an; ++ w[c[u]]; if( im[u] ) pre[cid][im[u]] = an; for( int i = head[u] ; i ; i = nex[i] ) { int v = to[i]; if( v == fa ) continue; dfs( v , u ); } -- w[c[u]]; if( !w[c[u]] ) -- an; }
int kk[MAXN << 2][B] , idx = 0 , rt[MAXN]; void build( ) { idx = rt[0] = 1; for( int i = 1 ; ( i - 1 ) * bl < sz ; ++ i ) kk[1][i] = i + 1 , ++ idx; } void add( int rt , int old , int x ) { memcpy( kk[rt] , kk[old] , sizeof kk[old] ); ++ idx; memcpy( kk[idx] , kk[kk[rt][( x - 1 ) / bl + 1]] , sizeof kk[1] ); kk[rt][( x - 1 ) / bl + 1] = idx; ++ kk[idx][( x - 1 ) % bl + 1]; } int que( int rt , int x ) { int t = kk[rt][( x - 1 ) / bl + 1]; return kk[t][( x - 1 ) % bl + 1]; }
int dfn[MAXN << 1] , dep[MAXN << 1] , pos[MAXN << 1] , en = 0 , d[MAXN] , par[MAXN]; int dfs1( int u , int fa ) { dfn[++en] = u , pos[u] = en , dep[en] = d[u] , par[u] = fa; rt[u] = ++ idx; add( rt[u] , rt[fa] , c[u] ); int re = 0; for( int i = head[u] ; i ; i = nex[i] ) { int v = to[i]; if( v == fa ) continue; d[v] = d[u] + 1; re = max( re , dfs1( v , u ) ); dfn[++ en] = u , dep[en] = d[u]; } if( re - d[u] >= blo && d[u] % blo == 0 ) ps[++ ee] = u , im[u] = ee; return re ? re : d[u]; } int lg[MAXN << 1]; int st[MAXN << 1][17]; void ST() { for( int i = 1 ; i <= en ; ++ i ) lg[i] = lg[i - 1] + (1 << lg[i - 1] == i); for( int i = 1 ; i <= en ; ++ i ) st[i][0] = i; for( int i = 1 ; ( 1 << i ) <= en ; ++ i ) for (int j = 1; j + (1 << i) - 1 <= en; j++) st[j][i] = dep[st[j][i - 1]] < dep[st[j + (1 << (i - 1))][i - 1]] ? st[j][i - 1] : st[j + (1 << (i - 1))][i - 1]; } int lca( int l , int r ) { l = pos[l] , r = pos[r]; if( l > r ) swap( l , r ); int k = lg[r - l + 1] - 1; return dep[st[l][k]] <= dep[st[r-(1<<k)+1][k]] ? dfn[st[l][k]] : dfn[st[r-(1<<k)+1][k]]; } int col[MAXN] , cn , l; int jump( int u ) { if( im[u] ) return im[u]; col[++ cn] = c[u]; if( u == l ) return -1; return jump( par[u] ); } int rejjump( int u ) { if( im[u] ) return im[u]; col[++ cn] = c[u]; return rejjump( par[u] ); } int oc[MAXN]; int main() {
cin >> n >> m; blo = sqrt( n ); for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&c[i]) , C[i] = c[i]; sort( C + 1 , C + 1 + n ); sz = unique( C + 1 , C + 1 + n ) - C - 1; bl = sqrt( sz ); for( int i = 1 ; i <= n ; ++ i ) c[i] = lower_bound( C + 1 , C + 1 + sz , c[i] ) - C; for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v); ade( u , v ) , ade( v , u ); } build( ); dfs1( 1 , 0 );
ST( ); for( int i = 1 ; i <= blo ; ++ i ) { int p = ps[i]; cid = i; dfs( p , p ); } int u , v , psu , psv , flg = 0 , re , last = 0 , t , pr; while( m-- ) { scanf("%d%d",&u,&v); u ^= last;
l = lca( u , v ); cn = re = 0; psu = jump( u ) , psv = jump( v ); if( psu == psv ) { oc[c[l]] = 1 , ++ re; for( int i = 1 ; i <= cn ; ++ i ) if( !oc[col[i]] ) oc[col[i]] = 1 , ++ re; for( int i = 1 ; i <= cn ; ++ i ) oc[col[i]] = 0; oc[c[l]] = 0; } else if( ~psu && ~psv ) { re = pre[psu][psv]; for( int i = 1 ; i <= cn ; ++ i ) if( !oc[col[i]] ) { oc[col[i]] = 1; if( que( rt[ps[psu]] , col[i] ) + que( rt[ps[psv]] , col[i] ) - que( rt[par[l]] , col[i] ) * 2 == 0 ) ++ re; } for( int i = 1 ; i <= cn ; ++ i ) oc[col[i]] = 0; } else { if( ~psu ) swap( u , v ) , swap( psu , psv ); pr = l; t = cn; while( !im[pr] ) { pr = par[pr]; if( !oc[c[pr]] ) { oc[c[pr]] = 1; if (que(rt[ps[psv]], c[pr]) - que(rt[par[l]], c[pr]) == 0) --re; col[++ cn] = c[pr]; } } re += pre[im[pr]][psv]; for( int i = t + 1 ; i <= cn ; ++ i ) oc[col[i]] = 0; cn = t; for( int i = 1 ; i <= cn ; ++ i ) if( !oc[col[i]] ) { oc[col[i]] = 1; if( que( rt[ps[psv]] , col[i] ) - que( rt[par[l]] , col[i] ) == 0 ) ++ re; } for( int i = 1 ; i <= cn ; ++ i ) oc[col[i]] = 0; } printf("%d\n",last = re); } }
|