Codeforces 796D Police Stations (bfs+思维)

在一个由Zane和Inzane建立的国家中,面对财务危机,他们决定最小化道路维护成本,通过关闭尽可能多的道路,同时确保每个城市都能在特定距离内到达警察局。本文探讨了如何在不违反国家法律的情况下,找到并确定可以关闭的最大数量的道路。

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个结点  m个特殊点  距离k  m个特殊点的下标  n-1条边

问最多能删除几条边  使得对于每个点  存在一个特殊点  他们的距离不超过k

输出删除的边数和下标

题解:先把所有的点扔到队列里面

做一遍bfs  如果当前边没走过但是到的点走过

就说明这条边可以删除

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
struct P
{
    int v,id;
    P(int v,int id):v(v),id(id){}
    P(){}
};
int edge[N],vis[N];
queue<int>que;
vector<P>vec[N];
int bfs()
{
    int u,v,i,j,ans=0;
    while(!que.empty()){
        u = que.front();
        que.pop();
        for(i=0;i<vec[u].size();i++) {
            P now = vec[u][i];
            if(edge[now.id]) continue;
            if(vis[now.v]) {
                edge[now.id] = 2;
                ans++;
                continue;
            }
            vis[now.v]=1;
            edge[now.id]=1;
            que.push(now.v);
        }
    }
    return ans;
}
int main()
{
    int n,m,d;
    int u,v;
    int i,j;
    cin>>n>>m>>d;
    while(m--) {
        cin>>u;
        vis[u]=1;
        que.push(u);
    }
    for(i=1;i<n;i++) {
        cin>>u>>v;
        vec[u].push_back(P(v,i));
        vec[v].push_back(P(u,i));
    }
    printf("%d\n",bfs());
    for(i=1;i<n;i++) {
        if(edge[i]==2) printf("%d ",i);
    }
    printf("\n");
    return 0;
}

 

当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值