codeforces contest/796/problem/D

该问题要求在保持每个城市都能在限制距离内到达警察局的情况下,确定最多可以关闭多少条道路。给定城市数量、警察站数量和最大距离,通过BFS策略从每个警察站开始寻找可关闭的道路。
摘要由CSDN通过智能技术生成

D. Police Stations
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Input
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.

Output
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.

Examples
input
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6
output
1
5
input
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
output
2
4 5
Note
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.

题意:给你一棵树,然后几个特殊点,让你删除更多边,使得最后每个点离最近的特殊的的距离不大于一个数。

解题思路:若有x个特殊点,则一定能删除x - 1条边满足题意,因为没有删除之前每个点都是满足题意的,但是要删除哪x - 1条边呢,这个很简单,直接bfs就行,从每个特殊点开始bfs。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int head[3*maxn];
int n,k,d;
bool p[maxn];
bool visit[maxn];
bool flag[maxn];
int cnt;
set<int> g[maxn];
set<int>::iterator it;
struct edge{
    int u;
    int v;
    int id;
    int last;
}Edge[3*maxn];
void add(int u,int v,int id)
{
    Edge[cnt].u = u;
    Edge[cnt].v = v;
    Edge[cnt].id = id;
    Edge[cnt].last = head[u];
    head[u] = cnt++;
}
struct node{
    int value;
    int step;
};
void bfs()
{
    queue<node> Q;
    node n0;
    node ans;
    node nx;
    for(int i = 1; i <= n; i++)
    {
        if(p[i])
        {
            n0.value = i;
            n0.step = 0;
            Q.push(n0);
        }
    }
    while(!Q.empty())
    {
        node nx = Q.front();
        Q.pop();
        int value = nx.value;
        int step = nx.step;
        visit[value] = true;
        if(step == d)
        {
            for(int i = head[value]; i != -1; i = Edge[i].last)
            {
                int v = Edge[i].v;
                int id = Edge[i].id;
                it = g[value].find(v);
                if(it != g[value].end())
                {
                    flag[id] = true;
                    g[value].erase(v);
                    g[v].erase(value);
                }
            }
        }
        else
        {
            for(int i = head[value]; i != -1; i = Edge[i].last)
            {
                int v = Edge[i].v;
                int id = Edge[i].id;
                if(visit[v]||p[v])
                {
                    it = g[value].find(v);
                    if(it != g[value].end())
                    {
                        flag[id] = true;
                        g[value].erase(v);
                        g[v].erase(value);
                    }
                }
                else
                {
                    g[value].erase(v);
                    g[v].erase(value);
                    ans.value = v;
                    ans.step = step + 1;
                    visit[v] = true;
                    Q.push(ans);
                }
            }
        }
    }
}
int main()
{
    memset(p,false,sizeof(false));
    memset(visit,false,sizeof(visit));
    memset(head,-1,sizeof(head));
    memset(flag,false,sizeof(flag));
    scanf("%d%d%d",&n,&k,&d);
    int police;
    for(int i = 1; i <= k; i++)
    {
        scanf("%d",&police);
        p[police] = true;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(p[i]) ans++;
    }
    int u,v;
    for(int i = 1; i <= n - 1; i++)
    {
        scanf("%d%d",&u,&v);
        add(u,v,i);
        add(v,u,i);
        g[u].insert(v);
        g[v].insert(u);
    }
    printf("%d\n",ans - 1);
    bfs();
    int path[maxn];
    int res = 0;
    for(int i = 1; i <= n - 1; i++)
    {
        if(flag[i])
        path[++res] = i;
    }
    sort(path + 1,path + res + 1);
    if(ans - 1)
    {
        if(ans - 1 == 1) printf("%d\n",path[1]);
        else
        {
            printf("%d",path[1]);
            for(int i = 2; i <= res; i++)
            {
                printf(" %d",path[i]);
            }
            printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值