Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】

本文介绍了一种在保证每个普通节点到特殊节点距离不超过给定阈值的前提下,最大化裁减树形结构中边数量的算法。通过广度优先搜索策略确保了特殊节点覆盖的有效性和最优化,最终实现了道路维护成本的最小化。

D. Police Stations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.


题目大意:

给你一个树,其中有N个点,N-1条无向边,现在有M个特殊点,问你最多可以切割多少条边,使得每个普通点到最近的特殊点的距离小于等于d.

保证有解。


思路:


保证有解,其实这个d就没有卵用了。

我们只要将这M个特殊点放到队列中,此时Bfs肯定有一个时间优先度。

那么我们直接Bfs.使得每次队头向下走一步,走到没有走过的点上边,对应记录每条边是否使用过即可。

这样去分块一定是最优的。

剩余没用过的边一定是最多的任意切割的边。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
struct node
{
    int to,pos;
}now,nex;
vector<node >mp[300800];
int vis[300800];
int ans[300800];
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        queue<int >s;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<m;i++)
        {
            int x;
            scanf("%d",&x);
            s.push(x);
            vis[x]=1;
        }
        for(int i=1;i<=n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            now.to=y;
            now.pos=i;
            mp[x].push_back(now);
            now.to=x;
            now.pos=i;
            mp[y].push_back(now);
        }
        while(!s.empty())
        {
            int u=s.front();
            s.pop();
            for(int i=0;i<mp[u].size();i++)
            {
                int v=mp[u][i].to;
                if(vis[v]==0)
                {
                    ans[mp[u][i].pos]=1;
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
        int cnt=0;
        for(int i=1;i<=n-1;i++)if(ans[i]==0)cnt++;
        printf("%d\n",cnt);
        for(int i=1;i<=n-1;i++)
        {
            if(ans[i]==0)printf("%d ",i);
        }
        printf("\n");
    }
}













基于径向基函数神经网络RBFNN的自适应滑模控制学习(Matlab代码实现)内容概要:本文介绍了基于径向基函数神经网络(RBFNN)的自适应滑模控制方法,并提供了相应的Matlab代码实现。该方法结合了RBF神经网络的非线性逼近能力和滑模控制的强鲁棒性,用于解决复杂系统的控制问题,尤其适用于存在不确定性和外部干扰的动态系统。文中详细阐述了控制算法的设计思路、RBFNN的结构与权重更新机制、滑模面的构建以及自适应律的推导过程,并通过Matlab仿真验证了所提方法的有效性和稳定性。此外,文档还列举了大量相关的科研方向和技术应用,涵盖智能优化算法、机器学习、电力系统、路径规划等多个领域,展示了该技术的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及工程技术人员,特别是从事智能控制、非线性系统控制及相关领域的研究人员; 使用场景及目标:①学习和掌握RBF神经网络与滑模控制相结合的自适应控制策略设计方法;②应用于电机控制、机器人轨迹跟踪、电力电子系统等存在模型不确定性或外界扰动的实际控制系统中,提升控制精度与鲁棒性; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,深入理解算法实现细节,同时可参考文中提及的相关技术方向拓展研究思路,注重理论分析与仿真验证相结合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值