Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】
来源:互联网 发布:类似易企秀的软件 编辑:程序博客网 时间:2024/06/01 08:30
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 mostd kilometers along the roads.
There are n cities in the country, numbered from1 to n, connected only by exactlyn - 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 hask 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 andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — the cities directly connected by the road with indexi.
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 withind 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.
6 2 41 61 22 33 44 55 6
15
6 3 21 5 61 21 31 41 55 6
24 5
In the first sample, if you shut down road 5, all cities can still reach a police station withink = 4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either4 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"); }}
- Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】
- Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】
- Codeforces Round #408 (Div. 2) -- D. Police Stations(bfs)
- Codeforces Round #408 (Div. 2) D. Police Stations 最短路、BFS
- Codeforces Round #408 (Div. 2)-D. Police Stations-多起点bfs
- Codeforces Round #408 (Div. 2) D. Police Stations
- Codeforces Round #408 (Div. 2)-D. Police Stations
- Codeforces Round #408 D. Police Stations
- Codeforces 796D Police Stations (bfs+思维)
- Codeforces 796D Police Stations 构造+BFS
- D. Police Stations----多起点bfs
- Codeforces Round #408 (Div. 2) 思维,模拟,DP, 多点BFS
- Codeforces Round #449 (Div. 2) D(思维)
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #375 (Div. 2) D bfs
- 796D Police Stations
- Codeforces Round #244 (Div. 2)a Police Recruits
- Codeforces Round #244 (Div. 2)E. Police Patrol
- [HDU 4739]Zhuge Liang's Mines:状压DP
- 克隆二叉树
- 伸展树(Splay Tree)
- Java之final、finally、finalize
- js获取select标签选中的值
- Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】
- okHttp网络请求框架 get 及post请求方式
- 【Spring学习29】cglib的Lookup方法
- bzoj4551: [Tjoi2016&Heoi2016]树
- AOP的hello world
- leetcode 100. Same Tree
- 1037. 在霍格沃茨找零钱(20)
- atcoder square869120Contest #4 D
- 【深度学习】caffe 中的一些参数介绍