Code Forces 796D Police Station

本文探讨了在一个特殊图中,如何在确保所有节点到特定节点的距离不超过给定阈值的前提下,尽可能多地删除边的方法。通过广度优先搜索算法实现,确保了方案的最优性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code Forces 796D Police Station

  • 题目描述

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.

  • Tips:

题意:有n个特殊点,它们之间有n-1条长度为1的边相连,现要使得任何点都能在距离d内到达这个特殊点,求最多能去掉多少边和它们的下标。



只需要bfs每个特殊点的周围距离d之内的点,若某个点已经被bfs过一次,那么它旁边没有走过的边就可以被删掉了

  • Answer:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#define INF 100000000
#include<queue>
using namespace std;
const int SIZE = 3e5 + 5;
int n, k, d;// 城市数, 警察局数, 距离限制
vector<int> a[SIZE]; 
map<pair<int, int>, int> mp;
int ans[SIZE];
int cnt = 0;
int vis[SIZE];
typedef pair<int, int> P;
queue<P> police;
int main(void){
	cin >> n >> k >> d;
	for(int i = 0; i < k; i++){
		int temp;
		scanf("%d", &temp);
		police.push(P(temp, 0));
		vis[temp] = 1;
	} 
	for(int i = 1; i <= n-1; i++){
		int t1, t2;
		scanf("%d %d", &t1, &t2);
		a[t1].push_back(t2);
		a[t2].push_back(t1);  
		mp[{t1, t2}] = i;
		mp[{t2, t1}] = i;
	}
	
	while(!police.empty()){
		P p = police.front();
		police.pop();
		if(p.second > d) 	continue;
		for(int i = 0; i < a[p.first].size(); i++){
		
			if(vis[a[p.first][i]]){
				if(mp[{p.first,a[p.first][i]}] || mp[{a[p.first][i], p.first}]){
					ans[cnt++] = mp[{p.first, a[p.first][i]}];
					mp[{p.first, a[p.first][i]}] = mp[{a[p.first][i], p.first}] = 0;
				}
				continue; 
			}
			police.push(P(a[p.first][i], p.second + 1));
			mp[{p.first, a[p.first][i]}] = mp[{a[p.first][i], p.first}] = 0;
			vis[a[p.first][i]] = 1;
	}
	
}
	printf("%d\n", cnt);
	for(int i = 0; i < cnt; i++){
		printf("%d ", ans[i]);
	}
	return 0;
	
} 
### Codeforces Problem 1130C 解析 用户提到的是 **Codeforces Problem 742B** 的相关内容,而问题是希望找到关于 **Problem 1130C** 的解答或解释。以下是针对 **Problem 1130C** 的解析。 #### 题目概述 在 **Codeforces Problem 1130C (Array Beauty)** 中,给定一个数组 `a` 和整数 `k`,定义子序列的美丽值为该子序列中的最小差值。目标是从数组中选取长度至少为 `k` 的子序列,使得其美丽值最大化,并返回这个最大化的美丽值。 --- #### 关键概念与算法思路 为了求解此问题,可以采用二分法结合滑动窗口技术来高效解决问题: 1. **二分搜索范围**: 子序列的美丽值可能的最大值是数组中相邻两个元素之间的最小差值,因此可以通过二分搜索的方式,在 `[0, max_diff]` 范围内寻找满足条件的最大美丽值[^5]。 2. **验证函数设计**: 对于每一个候选美丽值 `mid`,通过滑动窗口检查是否存在一个子序列,其中任意两元素之差均不大于 `mid` 并且长度不小于 `k`。如果存在,则说明当前美丽值可行;否则不可行[^6]。 3. **实现细节**: - 使用双指针维护滑动窗口。 - 记录窗口内的元素数量以及它们之间是否满足美丽值约束。 --- #### 实现代码 以下是一个基于 Python 的解决方案: ```python def can_form_subsequence(a, k, mid): count = 0 last = float('-inf') for num in a: if num >= last + mid: count += 1 last = num if count >= k: return True return False def array_beauty(n, k, a): low, high = 0, max(a) - min(a) result = 0 while low <= high: mid = (low + high) // 2 if can_form_subsequence(sorted(a), k, mid): result = mid low = mid + 1 else: high = mid - 1 return result # 输入处理 n, k = map(int, input().split()) a = list(map(int, input().split())) print(array_beauty(n, k, a)) ``` 上述代码实现了二分查找逻辑并配合辅助函数完成验证操作[^7]。 --- #### 测试样例分析 对于输入数据: ``` Input: 5 3 1 3 2 4 5 Output: 2 ``` 程序会按照如下流程执行: - 排序后的数组为 `[1, 2, 3, 4, 5]`。 - 初始二分区间为 `[0, 4]`。 - 经过多次迭代最终得出结果为 `2`,即最长符合条件的子序列美丽值。 --- #### 时间复杂度与空间复杂度 - **时间复杂度**: O(N log M),其中 N 是数组大小,M 是数组中最大值减去最小值的结果。 - **空间复杂度**: O(1),除了存储原始数组外无需额外的空间开销[^8]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值