CodeForces - 796D:Police Stations

题目链接:CodeForces - 796D
题目描述

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.

输入

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.

输出

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.

题目大意:
一共有n个结点,由n-1条边连接,每条边长度为1。总共有k个特殊结点,它们分布在指定位置(与之前n中的结点重合),要求最多去掉几条边,仍然可以保证每个结点到最近的特殊结点的距离小于等于d.

思路:
1)同C题,n个结点n-1条边,不存在环。每个结点到它最近的特殊结点只有唯一的路径。
2)如果删除某一条边,则形成两棵树,每一棵树至少要有一个特殊结点,所以答案小于等于k-1。
3)对于每一个普通结点,把它加入到离它最近的特殊结点形成的树中。
4)对每一个特殊结点进行一次广度优先搜索,如果存在普通结点已被标记过,则此边可以删除。

CODE:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> pai;
int n,k,d,ans;
const int MAXN=3e5+10;
queue<pai> que;
vector<pai> wire[MAXN];
int vis[MAXN];
int flag[MAXN];
void BFS()
{
	while(!que.empty())
	{
		int f=que.front().first;
		int t=que.front().second;
		que.pop();
		if(vis[f])continue;
		vis[f]=1;
		for(int i=0;i<wire[f].size();i++)
		{
			int v=wire[f][i].first;
			int num=wire[f][i].second;
			if(v!=t)
			{
				if(vis[v])flag[num]=1,ans++;
				else que.push(pai(v,f));
			}
		}
	}
}
void init()
{
	ans=0;
	for(int i=1;i<=n;i++)wire[i].clear();
	while(!que.empty())que.pop();
	memset(vis,0,sizeof(vis));
	memset(flag,0,sizeof(flag));
}
int main()
{
	while(~scanf("%d %d %d",&n,&k,&d))
	{
		int f,t;
		init();
		for(int i=1;i<=k;++i)
		{
			scanf("%d",&f);
			que.push(pai(f,0));
		}
		for(int i=1;i<=n-1;i++)
		{
			scanf("%d %d",&f,&t);
			wire[f].push_back(pai(t,i));
			wire[t].push_back(pai(f,i));
		}
		BFS();
		printf("%d\n",ans);
		for(int i=1;i<=n-1;++i)
			if(flag[i])printf("%d ",i);	
		printf("\n");		
	}	
	return 0;
} 
内容概要:本文探讨了在微电网优化中如何处理风光能源的不确定性,特别是通过引入机会约束和概率序列的方法。首先介绍了风光能源的随机性和波动性带来的挑战,然后详细解释了机会约束的概念,即在一定概率水平下放松约束条件,从而提高模型灵活性。接着讨论了概率序列的应用,它通过对历史数据分析生成多个可能的风光发电场景及其概率,以此为基础构建优化模型的目标函数和约束条件。文中提供了具体的Matlab代码示例,演示了如何利用CPLEX求解器解决此类优化问题,并强调了参数选择、模型构建、约束添加以及求解过程中应注意的技术细节。此外,还提到了一些实用技巧,如通过调整MIP gap提升求解效率,使用K-means聚类减少场景数量以降低计算复杂度等。 适合人群:从事电力系统研究、微电网设计与运营的专业人士,尤其是那些对风光不确定性建模感兴趣的研究者和技术人员。 使用场景及目标:适用于需要评估和优化含有大量间歇性可再生能源接入的微电网系统,旨在提高系统的经济性和稳定性,确保在面对风光出力波动时仍能维持正常运作。 其他说明:文中提到的方法不仅有助于学术研究,也可应用于实际工程项目中,帮助工程师们制定更为稳健的微电网调度计划。同时,文中提供的代码片段可供读者参考并应用于类似的问题情境中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值