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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include<bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define reg register const int maxn=10005; char buffer[maxn],*S,*T; inline char Get_Char(){ if(S==T){ T=(S=buffer)+fread(buffer,1,maxn,stdin); if(S==T)return EOF; } return *S++; } inline int read(){ reg char c; reg int re=0,f=0; for(c=Get_Char();c<'0' or c>'9';c=Get_Char())if(c=='-')f=1; for(;c>='0' and c<='9';)re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char(); if(f)return -re; return re; } inline void read(int&x){ reg char c; reg int re=0,f=0; for(c=Get_Char();c<'0' or c>'9';c=Get_Char())if(c=='-')f=1; for(;c>='0' and c<='9';)re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char(); if(f)x=-re; else x=re; } inline void read(ll&x){ reg char c; reg ll re=0,f=0; for(c=Get_Char();c<'0' or c>'9';c=Get_Char())if(c=='-')f=1; for(;c>='0' and c<='9';)re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char(); if(f)x=-re; else x=re; } inline void print(int x){ if(x<10){ putchar(x+'0'); return; } print(x/10); putchar(x%10+'0'); } const int mxn=2e5+5; vector<int>g[mxn]; int n,R,q,r[mxn]; int dep[mxn*2]; int ord[mxn*2],cco,cord[mxn*2]; int mi[mxn*2][22]; int lg[mxn*2],co; int st[mxn*2],ed[mxn*2]; inline void dfs(int x,int par=0,int deep=1){ cord[++cco]=x; st[x]=cco;ed[x]=cco; dep[x]=deep;ord[x]=++co; for(int y:g[x]){ if(par==y)continue; dfs(y,x,deep+1); cord[++cco]=x; ed[x]=cco; } } inline void init(){ lg[1]=0; for(int i=2;i<mxn*2;++i)lg[i]=lg[i>>1]+1; for(int i=1;i<=cco;++i)mi[i][0]=cord[i]; for(int k=1;k<22;++k){ for(int i=1;i<=cco-(1<<k)+1;++i){ int x=mi[i][k-1],y=mi[i+(1<<(k-1))][k-1]; if(dep[x]<dep[y])mi[i][k]=x; else mi[i][k]=y; } } } inline int lca(int x,int y){ int fx=st[x],fy=ed[y]; if(fx>fy)swap(fx,fy); int ax=mi[fx][lg[fy-fx]],ay=mi[fy-(1<<lg[fy-fx])+1][lg[fy-fx]]; if(dep[ax]<dep[ay])return ax; else return ay; } inline bool cmp(int x,int y){return ord[x]<ord[y];} int sta[mxn],top=0; int sz[mxn]; int up[404][mxn],dw[404][mxn]; inline vector<int>mer(vector<int>x,vector<int>y){ vector<int>rt;rt.clear(); int i=0,j=0; for(;i<x.size() and j<y.size();) if(cmp(x[i],y[j]))rt.push_back(x[i]),++i; else rt.push_back(y[j]),++j; for(;i<x.size();++i)rt.push_back(x[i]); for(;j<y.size();++j)rt.push_back(y[j]); return rt; } inline void gen(vector<int>v,int y){ top=0; sta[++top]=1; sz[1]=r[1]==y; for(int i:v){ if(i!=1){ int l=lca(sta[top],i); if(l!=sta[top]){ for(;ord[l]<ord[sta[top-1]];)sz[sta[top-1]]+=sz[sta[top]],--top; if(ord[l]>ord[sta[top-1]]){ sz[l]=r[l]==y; sz[l]+=sz[sta[top]]; sta[top]=l; }else sz[l]+=sz[sta[top]],top--; } sz[i]=r[i]==y; sta[++top]=i; } } for(int i=top-1;i>=1;--i)sz[sta[i]]+=sz[sta[i+1]]; } vector<int>col[mxn]; vector<int>hea; int ans[404][404]; int id[mxn];
inline void prep(int x,int fa,int cl,int flg=0){ if(r[x]==hea[cl])dw[cl][x]=1,++flg; up[cl][x]=flg; for(int y:g[x])if(y!=fa){ prep(y,x,cl,flg); dw[cl][x]+=dw[cl][y]; } } inline void solve(){ memset(id,-1,sizeof(id)); read(n),read(R),read(q); read(r[1]);col[r[1]].push_back(1); for(int i=2;i<=n;++i){ int x;read(x),read(r[i]); g[x].push_back(i); g[i].push_back(x); col[r[i]].push_back(i); } dfs(1); init(); int bound=500; for(int i=1;i<=R;++i)if(col[i].size()>=bound)hea.push_back(i); for(int i=0;i<hea.size();++i)id[hea[i]]=i; for(int i=1;i<=n;++i)sort(col[i].begin(),col[i].end(),cmp); for(int i=0;i<hea.size();++i)for(int j=0;j<hea.size();++j)if(i!=j){ gen(mer(col[hea[i]],col[hea[j]]),hea[j]);
ll res=0; for(int f:col[hea[i]])res+=sz[f]; ans[i][j]=res; } for(int i=0;i<hea.size();++i)prep(1,0,i); for(;q--;){ int x,y;read(x),read(y); if(~id[x] and ~id[y])print(ans[id[x]][id[y]]),putchar('\n'); else if(~id[x] and id[y]==-1){ ll res=0; for(int i:col[y])res+=up[id[x]][i]; print(res),putchar('\n'); }else if(id[x]==-1 and ~id[y]){ ll res=0; for(int i:col[x])res+=dw[id[y]][i]; print(res),putchar('\n'); }else{ gen(mer(col[x],col[y]),y);
ll res=0; for(int f:col[x])res+=sz[f]; print(res),putchar('\n'); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0); int T=1; for(;T--;)solve(); return 0; }
|