Codeforces Round #408 (Div. 2) D. Police Stations 最短路、BFS

D. Police Stations
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

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 nk, and d (2 ≤ n ≤ 3·1051 ≤ k ≤ 3·1050 ≤ 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 ≤ nui ≠ 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.



Source

Codeforces Round #408 (Div. 2)


My Solution

题意:有一棵无根树,有一些节点上有一些标记(police station),初始时满足,每个节点至少 与一个被标记过的节点相连且距离不超过d,要求去掉尽可能多的节点,使剩余的图依然满足,每个节点至少 与一个被标记过的节点相连且距离不超过d。


最短路、BFS

本来用dfs写了一发,后来发现很多情况下会重复去掉边。

用BFS来做比较好,从所有的被标记过的节点开始BFS,当接下来的边会导向已经访问过的节点时把这条边减去,否则放入队列。

这样每个节点都会与一个被标记过的节点直接或间接的相连,且距离不超过d。

最终会被减掉m-1条边,m表示size{unique{pi}}.

这里可以用反证法证明,如果减掉m-1+a(a>0)条边,则图会被切成大于等于m+1块,然后只有m个点被标记,所以必定有些块不满足要求。

反之,如果减点 m-1-a(a>0),则图会边切成小于等于m-1块,这个时候根据鸽巢原理,至少存在一个块中有2个节点被标记,显然还有更优的方案。

复杂度 O(n)


[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <vector>  
  5. #include <queue>  
  6. #include <algorithm>  
  7. using namespace std;  
  8. typedef long long LL;  
  9. typedef pair<intint> ii;  
  10. const int MAXN = 3e5 + 8;  
  11.   
  12. vector<ii> sons[MAXN];  
  13. queue<ii> que;  
  14. bool vis[MAXN], f[MAXN];  
  15.   
  16. inline void bfs()  
  17. {  
  18.     int fa, u, v, sz, i;  
  19.     while(!que.empty()){  
  20.         u = que.front().first;  
  21.         fa = que.front().second;  
  22.         que.pop();  
  23.         if(vis[u]) continue;  
  24.         vis[u] = true;  
  25.         sz = sons[u].size();  
  26.         for(i = 0; i < sz; i++){  
  27.             v = sons[u][i].first;  
  28.             if(v == fa) continue;  
  29.             if(vis[v]){  
  30.                 f[sons[u][i].second] = true;  
  31.             }  
  32.             else que.push(ii(v, u));  
  33.         }  
  34.   
  35.     }  
  36. }  
  37.   
  38. int main()  
  39. {  
  40.     #ifdef LOCAL  
  41.     freopen("d.txt""r", stdin);  
  42.     //freopen("d.out", "w", stdout);  
  43.     int T = 4;  
  44.     while(T--){  
  45.     #endif // LOCAL  
  46.     ios::sync_with_stdio(false); cin.tie(0);  
  47.   
  48.     int n, k, d, i, u, v, ans = 0;  
  49.     cin >> n >> k >> d;  
  50.     for(i = 1; i <= k; i++){  
  51.         cin >> u;  
  52.         que.push(ii(u, -1));  
  53.     }  
  54.     for(i = 1; i < n; i++){  
  55.         cin >> u >> v;  
  56.         sons[u].push_back(ii(v, i));  
  57.         sons[v].push_back(ii(u, i));  
  58.     }  
  59.   
  60.     bfs();  
  61.     for(i = 1; i <= n; i++){  
  62.         if(f[i]) ans++;  
  63.     }  
  64.     cout << ans << "\n";  
  65.     ans = 0;  
  66.     for(i = 1; i <= n; i++){  
  67.         if(f[i]){  
  68.             if(ans) cout << " ";  
  69.             ans++;  
  70.             cout << i;  
  71.         }  
  72.     }  
  73.   
  74.   
  75.     #ifdef LOCAL  
  76.     for(i = 1; i <= n; i++) sons[i].clear();  
  77.     memset(vis, falsesizeof vis);  
  78.     memset(f, falsesizeof f);  
  79.     cout << endl;  
  80.     }  
  81.     #endif // LOCAL  
  82.     return 0;  
  83. }  


  Thank you!

                                                                                                                                             ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值