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
| #include<iostream> #include<cstdio> #include<queue> #define ll long long using namespace std; namespace Ehnaev{ inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();} while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();} return ret*f; } inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);} else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);} while(len>=0) putchar(buf[len--]); } }using Ehnaev::read;using Ehnaev::write; inline void writeln(ll x) {write(x);putchar(10);} inline void writes(ll x) {write(x);putchar(32);}
const ll N=3e5;
ll n,k,d,tot,cnt_col; ll ver[N*2+5],nxt[N*2+5],head[N+5],flg[N+5],vis[N+5],a[N+5]; ll flg_[N+5],ans[N+5],id[N*2+5];
struct node{ll c,d,dis;}t; queue<node> q;
inline void Addedge(ll u,ll v) { ver[++tot]=v;nxt[tot]=head[u];head[u]=tot; }
int main() {
n=read();k=read();d=read();
for(ll i=1;i<=k;i++) { a[i]=read();flg[a[i]]=1; }
for(ll i=1;i<n;i++) { ll u,v;u=read();v=read(); Addedge(u,v);id[tot]=i; Addedge(v,u);id[tot]=i; }
for(ll i=1;i<=n;i++) { if(flg[i]) { ++cnt_col;vis[i]=cnt_col; t.d=i;t.c=cnt_col;t.dis=0;q.push(t); } }
while(!q.empty()) { node h=q.front();q.pop(); if(h.dis==d) continue; for(ll i=head[h.d];i;i=nxt[i]) { if(vis[ver[i]]) continue; vis[ver[i]]=h.c; t.d=ver[i];t.c=h.c;t.dis=h.dis+1; q.push(t); } }
ll cnt_ans=0; for(ll i=1;i<=n;i++) { for(ll j=head[i];j;j=nxt[j]) { if(vis[ver[j]]!=vis[i]) { if(flg_[id[j]]) continue; ans[++cnt_ans]=id[j]; flg_[id[j]]=1; } } }
writeln(cnt_ans);
for(ll i=1;i<=cnt_ans;i++) {writes(ans[i]);}
return 0; }
|