This commit is contained in:
2023-12-23 22:23:48 +08:00
commit 43ced8bd2a
58 changed files with 5702 additions and 0 deletions

186
ACMOJ-2002.cpp Normal file
View File

@ -0,0 +1,186 @@
#include<cstdio>
#define unlikely(x) __builtin_expect(!!(x), 0)
struct char_reader
{
FILE* f; char *buf,*p1,*p2; int size;
char_reader(FILE* fin,int bufsize=1<<16) { f=fin; size=bufsize; p1=p2=0; buf=new char[size]; }
~char_reader() { delete []buf; }
inline int operator()() { return p1==p2&&(p2=(p1=buf)+fread(buf,1,size,f),p1==p2)?EOF:*p1++; }
};
struct char_writer
{
FILE* f; char *buf,*p,*end; int size;
char_writer(FILE* fout,int bufsize=1<<16) { f=fout; size=bufsize; buf=new char[size]; p=buf; end=buf+bufsize; }
~char_writer() { fwrite(buf,p-buf,1,f); delete []buf; }
inline char operator()(char ch)
{
if(unlikely(end==p)) { fwrite(buf,end-buf,1,f); p=buf; }
return *p++=ch;
}
};
char_reader gch(stdin);
char_writer wch(stdout);
template<typename T> inline int read(T& t)
{
bool f=false; int ch; while(ch=gch(),!((ch>='0'&&ch<='9')||ch=='-')) { if(ch==EOF) return 0; }
t=0;
if(ch=='-') f=true,ch=gch(); t=ch^48; while(ch=gch(),ch>='0'&&ch<='9') t=(t<<3)+(t<<1)+(ch^48);
if(f) t=-t;
return 1;
}
inline int read(char &c)
{
c=0; int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; } c=ch;
return 1;
}
inline int read(char *s)
{
int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; }
*s++=ch; while(ch=gch(),!(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')&&ch!=EOF) *s++=ch;
return 1;
} inline int read(const char *s) { return read((char*)s); }
inline int readline(char *s)
{
int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; }
*s++=ch; while(ch=gch(),!(ch=='\n'||ch=='\r')&&ch!=EOF) *s++=ch;
return 1;
} inline int readline(const char *s) { return readline((char*)s); }
template<typename T> inline void write(T t)
{
int stk[20],cnt=0;
if(t==0) { wch('0'); return; } if(t<0) { wch('-'); t=-t; }
while(t>0) { stk[cnt++]=t%10; t/=10; } while(cnt) wch(stk[--cnt]+'0');
}
inline void write(char t) { wch(t); }
inline void write(char *s) { while(*s) wch(*s++); } inline void write(const char *s) { write((char*)s); }
#if __cplusplus >= 201103L
template<typename T,typename... Args> inline int read(T& t,Args&... args) { return read(t)+read(args...); }
template<typename T,typename... Args> inline void write(T t,Args... args) { write(t); write(args...); }
#else
template<typename A_t,typename B_t> inline int read(A_t &a,B_t &b) { return read(a)+read(b); }
template<typename A_t,typename B_t,typename C_t> inline int read(A_t &a,B_t &b,C_t &c) { return read(a)+read(b)+read(c); }
template<typename A_t,typename B_t,typename C_t,typename D_t> inline int read(A_t &a,B_t &b,C_t &c,D_t &d) { return read(a)+read(b)+read(c)+read(d); }
template<typename A_t,typename B_t> inline void write(A_t a,B_t b) { write(a); write(b); }
template<typename A_t,typename B_t,typename C_t> inline void write(A_t a,B_t b,C_t c) { write(a); write(b); write(c); }
template<typename A_t,typename B_t,typename C_t,typename D_t> inline void write(A_t a,B_t b,C_t c,D_t d) { write(a); write(b); write(c); write(d); }
#endif
#include<cstdio>
#include<algorithm>
#include<unordered_map>
#include<cassert>
using namespace std;
typedef long long LL;
const int maxn=5e5+10;
struct Edge
{
int v;
Edge *nxt;
}mem[maxn*2],*G[maxn],*ecnt=mem;
inline void AddEdge(int u,int v)
{
ecnt->v=v;
ecnt->nxt=G[u];
G[u]=ecnt++;
}
int n,m,c[maxn],res[maxn],sub_tree_size[maxn];
vector<pair<int,int>> query[maxn];
struct Data
{
vector<int> colors;
unordered_map<int,int> mp;
vector<int> times_to_numbers;
};
int dfs1(int u,int fa)
{
sub_tree_size[u]=1;
for(Edge *it=G[u];it;it=it->nxt)
if(it->v!=fa)
sub_tree_size[u]+=dfs1(it->v,u);
return sub_tree_size[u];
}
Data* dfs2(int u,int fa)
{
// fprintf(stderr,"visit %d from %d\n",u,fa);
Data *this_data=nullptr;
if(sub_tree_size[u]==1)
{
this_data=new Data;
this_data->colors.push_back(c[u]);
this_data->mp[c[u]]=1;
this_data->times_to_numbers.push_back(0);
this_data->times_to_numbers.push_back(1);
for(int i=0;i<query[u].size();i++)
{
int id=query[u][i].first,k=query[u][i].second;
if(k==1) res[id]=1;
}
return this_data;
}
vector<Data*> sub_data;
int main_son=0;
assert(sub_tree_size[main_son]==0);
for(Edge *it=G[u];it;it=it->nxt)
if(it->v!=fa)
{
Data *p=dfs2(it->v,u);
if(sub_tree_size[it->v]>sub_tree_size[main_son])
{
main_son=it->v;
swap(this_data,p);
}
if(p!=nullptr) sub_data.push_back(p);
}
for(int i=0;i<sub_data.size();i++)
{
Data *p=sub_data[i];
for(int j=0;j<p->colors.size();j++)
{
this_data->colors.push_back(p->colors[j]);
int tms=++this_data->mp[p->colors[j]];
if(tms>=this_data->times_to_numbers.size())
{
this_data->times_to_numbers.push_back(0);
// assert(tms==this_data->times_to_numbers.size());
}
this_data->times_to_numbers[tms]++;
}
delete p;
}
this_data->colors.push_back(c[u]);
int tms=++this_data->mp[c[u]];
if(tms>=this_data->times_to_numbers.size())
{
this_data->times_to_numbers.push_back(0);
// assert(tms==this_data->times_to_numbers.size());
}
this_data->times_to_numbers[tms]++;
for(int i=0;i<query[u].size();i++)
{
int id=query[u][i].first,k=query[u][i].second;
if(k>=this_data->times_to_numbers.size()) continue;
res[id]=this_data->times_to_numbers[k];
}
return this_data;
}
int main()
{
#ifdef local
freopen("pro.in","r",stdin);
#endif
read(n,m);
for(int i=1;i<=n;i++) read(c[i]);
for(int i=1;i<=n-1;i++)
{
int x,y; read(x,y);
AddEdge(x,y); AddEdge(y,x);
}
for(int i=1;i<=m;i++)
{
int u,k; read(u,k);
query[u].push_back(make_pair(i,k));
}
dfs1(1,0);
dfs2(1,0);
for(int i=1;i<=m;i++) write(res[i],'\n');
return 0;
}