求助,悬关
查看原帖
求助,悬关
570086
RikkaTakanashi楼主2023/9/28 22:04
#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
int n,m,q,c[maxn];
vector<int> g[maxn];
vector<int> ct[maxn];
void add_edge(int u,int v){
	g[u].push_back(v);
}
int fa[maxn],dep[maxn],son[maxn],siz[maxn];
int ids[maxn],w[maxn],top[maxn],cnt;
void dfs1(int u,int f){
	fa[u]=f;
	siz[u]=1;
	dep[u]=dep[f]+1;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(v==f) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[son[u]]<siz[v]){
			son[u]=v;
		}
	}
}
void dfs2(int u,int f){
	ids[u]=++cnt;
	w[cnt]=u;
	top[u]=f;
	if(son[u]) dfs2(son[u],f);
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(v!=fa[u] && v!=son[u]){
			dfs2(v,v);
		}
	}
}
/*---------------------------------------*/
#define ls id<<1
#define rs id<<1|1
struct node{
	int a[20],tot;
	node(){
		tot=0;
	}
	void ist(int x){
		a[++tot]=x;
	}
	void st(){
		sort(a+1,a+tot+1);
		tot=min(tot,10);
	}
	void print(int a1){
		cout << min(tot,a1) << " ";
		if(!tot){
            cout << endl;
            return ;
        }
		for(int i=1;i<=min(a1,tot);i++){
			cout << a[i] << " ";
		}
		cout << endl;
	}
}t[maxn<<3];
node mg(node n1,node n2);
void push_up(int id){
	t[id]=mg(t[ls],t[rs]);
}
node mg(node n1,node n2){
	node ret;
	for(int i=1;i<=n1.tot;i++) ret.ist(n1.a[i]);
	for(int i=1;i<=n2.tot;i++) ret.ist(n2.a[i]);
	ret.st();
	return ret;
}
void build(int l,int r,int id){
	if(l==r){
		for(int i=0;i<ct[w[l]].size();i++){
			t[id].ist(ct[w[l]][i]);
		}
		t[id].st();
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls);
	build(mid+1,r,rs);
	push_up(id);
}
node query(int al,int ar,int l,int r,int id){
	node ret;
	if(al<=l && r<=ar){
		ret=mg(ret,t[id]);
		return ret;
	}
	int mid=(l+r)>>1;
	if(al<=mid) ret=mg(ret,query(al,ar,l,mid,ls));
	if(ar>mid) ret=mg(ret,query(al,ar,mid+1,r,rs));
	return ret;
}
node querypath(int x,int y){
	node ret;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ret=mg(ret,query(ids[top[x]],ids[x],1,n,1));
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	ret=mg(ret,query(ids[x],ids[y],1,n,1));
	ret.st();
	return ret;
}
int main(){
	cin >> n >> m >> q;
	for(int i=1;i<n;i++){
		int u,v;
		cin >> u >> v;
		add_edge(u,v);
		add_edge(v,u);
	}
	for(int i=1;i<=m;i++){
		cin >> c[i];
		ct[c[i]].push_back(i);
	}
	
	dfs1(1,0);
	dfs2(1,1);
	build(1,n,1);
	while(q--){
		int qu,qv,qa;
		cin >> qu >> qv >> qa;
		querypath(qu,qv).print(qa);
	}
	return 0;
}

第八个点WA

2023/9/28 22:04
加载中...