#pragma GCC optimize("Ofast") #include using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define RREP(i,n) for(int i=(n-1);i>=0;i--) #define ALL(v) v.begin(),v.end() using P=tuple; auto smerge=[](P x,P y)->P{ auto [a,b]=x; auto [c,d]=y; return {a+c,max(b,d)}; }; template struct ReRooting{ struct Edge { int from,to; key_t cost; sum_t dp1,dp2; //dp1:部分木の内容 //dp2:e=g[idx][i]の時、g[idx][0,i)のdp1の累積が入る }; using F=function; using G=function; vector> g; const F merge; const G score; const sum_t id; vector dp1,dp2; vector ans; ReRooting(int n,const G &score,const F &merge,const sum_t &id=sum_t{}) :g(n),merge(merge),score(score),id(id),dp1(n,id),dp2(n,id),ans(n){} void add_edge(int u,int v,const key_t &c){ g[u].emplace_back(Edge{u,v,c,id,id}); g[v].emplace_back(Edge{v,u,c,id,id}); } void add_directed_edge(int u,int v,const key_t &c){ g[u].emplace_back(Edge{u,v,c,id,id}); } void dfs1(int idx,int pre){ for(auto &e:g[idx])if(e.to!=pre){ dfs1(e.to,idx); e.dp1=score(dp1[e.to],e); dp1[idx]=merge(dp1[idx],e.dp1); } } void dfs2(int idx,int pre,sum_t top){ sum_t now=id; for(auto &e:g[idx]){ e.dp2=now; if(e.to==pre)e.dp1=score(top,e); now=merge(now,e.dp1); auto [a,b]=e.dp1; } dp2[idx]=now; now=id; reverse(g[idx].begin(),g[idx].end()); for(auto &e:g[idx]){ if(e.to!=pre){ auto [a,b]=e.dp2; auto [c,d]=now; dfs2(e.to,idx,smerge(e.dp2,now)); } now=merge(now,e.dp1); } } vector build(){ dfs1(0,-1); dfs2(0,-1,id); return dp2; } }; const int INF=1e9+7; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,k;cin>>n>>k; if(k==1){ vector> g(n); REP(_,n-1){ int u,v;cin>>u>>v;u--;v--; g[u].push_back(v); g[v].push_back(u); } int r;cin>>r;r--; vector ans(n,-1); queue que; que.push(r); ans[r]=0; while(que.size()){ int p=que.front();que.pop(); for(int q:g[p])if(ans[q]<0){ ans[q]=ans[p]+1; que.push(q); } } REP(i,n)cout< v(n,false); auto score=[&](P x,auto e)->P{ auto [a,b]=x; if(a==0&&b==0){ if(v[e.to])return {2,1}; return {0,INF}; } return {a+2,a+1-b}; }; auto merge=[&](auto x,auto y)->P{ auto [a,b]=x; auto [c,d]=y; return {a+c,max(b,c-d)}; }; ReRooting

g(n,score,merge,{0,0}); REP(_,n-1){ int u,v;cin>>u>>v;u--;v--; g.add_edge(u,v,{0,INF}); } REP(_,k){ int a;cin>>a;a--; v[a]=true; } auto ans=g.build(); for(auto &[x,y]:ans)cout<