Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.
There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.
However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.
Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.
The first line contains three integers n, k, and d (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105, 0 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.
The i-th of the following n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities directly connected by the road with index i.
It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.
In the first line, print one integer s that denotes the maximum number of roads that can be shut down.
In the second line, print s distinct integers, the indices of such roads, in any order.
If there are multiple answers, print any of them.
6 2 4 1 6 1 2 2 3 3 4 4 5 5 6
1 5
6 3 2 1 5 6 1 2 1 3 1 4 1 5 5 6
2 4 5
In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.
n个点的树上有k个警察局,问你最多可以删除多少条边,使得剩下的点到最近的近差距的距离都不大于d
感觉这个d没啥用。。因为题目说了一开始可行,因此我们只需要把所有警察局加进queue里面bfs即可。当下一个点走过了但是边没走过的时候,那条边就可以删除
#include<queue>
#include<cstdio>
using namespace std;
queue<int> Q;
struct line
{
int s,t;
int next;
}a[600001];
int head[300001];
int edge;
inline void add(int s,int t)
{
a[edge].next=head[s];
head[s]=edge;
a[edge].s=s;
a[edge].t=t;
}
int pre[300001];
bool v[300001];
bool vx[300001];
int xx[300001];
int main()
{
int n,k,t;
scanf("%d%d%d",&n,&k,&t);
int ss,tt,i,x;
for(i=1;i<=k;i++)
{
scanf("%d",&x);
Q.push(x);
v[x]=true;
}
for(i=1;i<=n-1;i++)
{
scanf("%d%d",&ss,&tt);
edge++;
add(ss,tt);
edge++;
add(tt,ss);
}
int ans=0;
while(!Q.empty())
{
int d=Q.front();
Q.pop();
for(i=head[d];i!=0;i=a[i].next)
{
int t=a[i].t;
if(!v[t])
{
v[t]=true;
Q.push(t);
pre[t]=d;
vx[(i+1)/2]=true;
}
else if(!vx[(i+1)/2]&&pre[d]!=t)
{
vx[(i+1)/2]=true;
ans++;
xx[ans]=(i+1)/2;
}
}
}
printf("%d\n",ans);
for(i=1;i<=ans-1;i++)
printf("%d ",xx[i]);
if(ans!=0)
printf("%d\n",xx[i]);
return 0;
}