[codeforces796D]Police Stations

time limit per test : 2 seconds
memory limit per test : 256 megabytes

分数:2100

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 1 1 to n n n, connected only by exactly n   −   1 n - 1 n1 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 n - 1 n1 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 n, k n,k, and d ( 2   ≤   n   ≤   3 ⋅ 1 0 5 , 1   ≤   k   ≤   3 ⋅ 1 0 5 , 0   ≤   d   ≤   n   −   1 ) d (2 ≤ n ≤ 3·10^5, 1 ≤ k ≤ 3·10^5, 0 ≤ d ≤ n - 1) d(2n3105,1k3105,0dn1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains k k k integers p 1 ,   p 2 ,   . . . ,   p k ( 1   ≤   p i   ≤   n ) p_1, p_2, ..., p_k (1 ≤ p_i ≤ n) p1,p2,...,pk(1pin) — each denoting the city each police station is located in.
The i i i-th of the following n   −   1 n - 1 n1 lines contains two integers u i u_i ui and v i ( 1   ≤   u i ,   v i   ≤   n , u i   ≠   v i ) v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) vi(1ui,vin,ui̸=vi) — the cities directly connected by the road with index i i 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 s s that denotes the maximum number of roads that can be shut down.
In the second line, print s s 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个节点的树,有k个关键节点,问最多删去多少条边可以使得每个点到离自己最近的关键节点的距离小于等于d,要求输出一种删边方案。
题解:
很显然我们可以让删掉之后的每个联通块内只有一个关键节点。这样是最优的,那么我们从所有关键节点开始同时bfs,遇上连接两个不同关键节点控制区域的边就将这个边删去,显然可以得到最好的答案。

#include<bits/stdc++.h>
#define ll long long
#define INF 1999122700
using namespace std;
int ne,h[300004];
struct edge{
    int to,nt,w;
}e[600004];
void add(int u,int v,int w){
    e[++ne].to=v;e[ne].w=w;
    e[ne].nt=h[u];h[u]=ne;
}
int dis[300004],pit[300004];
int n,k,d,p[300004];
void dfs(int x,int fa){
    for(int i=h[x];i;i=e[i].nt){
        if(e[i].to==fa)continue;
        if(dis[x]+1<=d&&dis[e[i].to]>dis[x]+1){
            dis[e[i].to]=dis[x]+1;
            pit[e[i].w]=1;
            dfs(e[i].to,x);
        }
        else{
            pit[e[i].w]=0;
        }
    }
}
queue<int>q;
vector<int>ans;
int main(){
    ne=0;
    while(!q.empty())q.pop();
    memset(pit,0,sizeof(pit));
    memset(h,0,sizeof(h));
    scanf("%d%d%d",&n,&k,&d);
    for(int i=1;i<=n;i++)dis[i]=INF;
    for(int i=1;i<=k;i++){
        scanf("%d",&p[i]);
        q.push(p[i]);
        dis[p[i]]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;scanf("%d%d",&u,&v);
        add(u,v,i);add(v,u,i);
    }
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=h[x];i;i=e[i].nt){
            if(dis[e[i].to]==INF){
                dis[e[i].to]=dis[x]+1;
                pit[e[i].w]=1;
                q.push(e[i].to);
            }
        }
    }
    ans.clear();
    for(int i=1;i<n;i++){
        if(!pit[i]){
            ans.push_back(i);
        }
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)printf("%d ",ans[i]);
    if(ans.size())puts("");
    return 0;
}

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值