[spoj] 10707: Count on a tree II

题意

一棵树上,每个节点都有颜色,每次询问树链上颜色种数.

传送门

分析

莫队算法

代码

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
#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
typedef long long ll ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}template <typename TP>
inline void reads(TP& ret) {
truewhile (ret=getchar() , ret<'!') ;
truewhile (CH=getchar() , CH>'!') ;
}

#include <algorithm>
#include <cmath>

#define maxn 40010LL
#define maxm 40010LL
#define maxc 40010LL
#define maxq 100010LL
#define maxk 17LL

//struct FST { int to , next ; } e[maxm<<1] ;
int to[maxm<<1] , next[maxm<<1] , star[maxn] = {0} , tote = 1 ;
inline void AddEdge(int u , int v) { to[++tote] = v ; next[tote] = star[u] ; star[u] = tote ; }

int n , m , ansnow ;
int cntblc , szblc , idx , top ;
int col[maxn] , cnt[maxc] = {0} ;
int dfn[maxn] , sta[maxn] , belong[maxn] , dep[maxn] , size[maxn] , par[maxn][maxk] ;
int ans[maxq] ;
bool vis[maxn] = {0} ;

struct QUERY {
trueint u , v , lca , id ;
truebool operator < (const QUERY& b) const
true{ return belong[u]==belong[b.u]? dfn[v]<dfn[b.v] : belong[u]<belong[b.u] ; }
} q[maxq] ;

inline void dfs(int u , int fa) {
int v , p ;
truesta[++top] = u ;
truepar[u][0] = fa ;
truedfn[u] = ++idx ;
truedep[u] = dep[fa]+1 ;
truesize[u] = 0 ;
truefor (p = star[u] ; p ; p = next[p])
truetrueif (v=to[p] , v!=fa) {
truetruetruedfs(v , u) ;
truetruetruesize[u] += size[v] ;
truetruetrueif (size[u] >= szblc) {
truetruetruetruesize[u] = 0 ; cntblc ++ ;
truetruetruetruewhile (sta[top] != u) belong[sta[top--]] = cntblc ;
truetruetrue}
truetrue}
truesize[u] ++ ;
}

inline void ST() {
int i , k ;
trueRep (k,1,maxk-1)
truetrueRep (i,1,n)
truetruetruepar[i][k] = par[par[i][k-1]][k-1] ;
}

inline int LCA(int u , int v) {
trueint k ;
trueif (dep[u] < dep[v]) std::swap(u,v) ;
trueRev (k,maxk-1,0)
truetrueif (dep[par[u][k]] >= dep[v]) u = par[u][k] ;
trueif (u == v) return u ;
trueRev (k,maxk-1,0)
truetrueif (par[u][k] != par[v][k])
truetruetrueu = par[u][k] , v = par[v][k] ;
truereturn par[u][0] ;
}

inline void Reverse(int u) {
trueif (vis[u]) { vis[u]=false;cnt[col[u]]--;if(cnt[col[u]]==0)ansnow--; }
trueelse { vis[u]=true;cnt[col[u]]++;if(cnt[col[u]]==1)ansnow++; }
}

inline void MoveTo(int u,int v) {
truewhile (u != v)
truetrueif (dep[u] > dep[v]) Reverse(u) , u = par[u][0] ;
truetrueelse Reverse(v) , v = par[v][0] ;
}

inline void GetAns(int i) {
trueReverse(q[i].lca) ;
trueans[q[i].id] = ansnow ;
trueReverse(q[i].lca) ;
}

int t[maxn] , rec[maxn] ;

inline bool cmp(const int& a , const int& b) {
truereturn rec[a]==rec[b]?a<b:rec[a]<rec[b] ;
}

int main() {
int i , u , v ;
// #define READ
true#ifdef READ
truetruefreopen(".in" ,"r",stdin ) ;
truetruefreopen(".out","w",stdout) ;
true#endif
trueread(n) , read(m) ;
trueif (n < 5) szblc = n ;
trueelse szblc = (int)pow(n,2.0/3.0) ;
trueRep (i,1,n) read(rec[i]) , t[i] = i ;
truestd::sort(t+1,t+n+1,cmp) ;
truet[0] = -1 ;
trueRep (i,1,n) {
truetrueif (rec[t[i]] == rec[t[i-1]]) col[t[i]] = col[t[i-1]] ;
truetrueelse col[t[i]] = i ;
true}
trueRep (i,2,n) {
truetrueread(u) , read(v) ;
truetrueAddEdge(u,v) , AddEdge(v,u) ;
true}
truecntblc = idx = dep[0] = ansnow = top = 0 ;
truedfs(1,0) ;
truecntblc ++ ;
truewhile (top) belong[sta[top--]] = cntblc ;
trueST() ;
trueRep (i,1,m) {
truetrueread(u) , read(v) ;
truetrueif (belong[u] > belong[v]) std::swap(u,v) ;
truetrueq[i].u = u , q[i].v = v , q[i].lca = LCA(u,v) , q[i].id = i ;
true}
truestd::sort(q+1,q+m+1) ;
trueMoveTo(q[1].u,q[1].v) ;
trueGetAns(1) ;
trueRep (i,2,m) {
truetrueMoveTo(q[i-1].u,q[i].u) ;
truetrueMoveTo(q[i-1].v,q[i].v) ;
truetrueGetAns(i) ;
true}
trueRep (i,1,m) printf("%d\n", ans[i]) ;
true#ifdef READ
truetruefclose(stdin) ; fclose(stdout) ;
true#else
truetruegetchar() ; getchar() ;
true#endif
truereturn 0 ;
}

热评文章