zoukankan      html  css  js  c++  java
  • Codeforces Round #408 (Div. 2) D

    地址:http://codeforces.com/contest/796/problem/D

    题目:

    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·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 ≤ 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.

    思路:多起点bfs,当bfs到两个相连的点u,v && dis[u]<=d&&dis[v]<=d时则说明该边可以删除。

      bfs过程中hs一下路径就好了

      代码交了两发,一次1980ms,一次1996ms,,感觉再交几次就可能t了

      其实这题就是个多原点最小生成树,可以去掉map优化。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=3e5+7;
    12 const int mod=1e9+7;
    13 
    14 map<PII,int>hs;
    15 queue<int>q;
    16 vector<int>mp[K];
    17 set<int>sa,sb;
    18 int n,k,d,ans,dis[K],v[K];
    19 void bfs(void)
    20 {
    21     while(q.size())
    22     {
    23         for(int k=0,sz=q.size();k<sz;k++)
    24         {
    25             int x=q.front();q.pop();
    26             for(int i=0;i<mp[x].size();i++)
    27             {
    28                 int u=mp[x][i],f=hs[MP(x,u)];
    29                 if(!f)f=hs[MP(u,x)];
    30                 if(sa.find(f)!=sa.end()||sb.find(f)!=sb.end())
    31                     continue;
    32                 if(dis[u]<=d&&dis[x]<=d)
    33                 {
    34                     ans++,sb.insert(f);
    35                     continue;
    36                 }
    37                 if(dis[u]>d)   dis[u]=dis[x]+1,q.push(u),sa.insert(f);
    38             }
    39         }
    40     }
    41 }
    42 int main(void)
    43 {
    44     cin>>n>>k>>d;
    45     memset(dis,0x3f3f3f3f,sizeof dis);
    46     for(int i=1,x;i<=k;i++)
    47         scanf("%d",&x),q.push(x),dis[x]=0;
    48     for(int i=1,x,y;i<n;i++)
    49         scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x),hs[MP(x,y)]=i;
    50     bfs();
    51     cout<<ans<<endl;
    52     for(auto x:sb)
    53         printf("%d ",x);
    54     return 0;
    55 }

     

  • 相关阅读:
    DirectX编译环境配置
    [转]unresolved external symbol __imp__PlaySoundA@12的解决方法
    Win7/Win8/Win10显示桌面按钮
    Win8中更改账户信息
    3D游戏图形引擎
    【整理】鼠标位置编码(Mouse Position Code)和鼠标激活返回值(MOUSEACTIVATE Return Codes)
    精简ICO图标可减小EXE程序文件大小
    【整理】SYSCOMMAND的wParam值的宏定义
    迅雷网速测试器 下载速率测试记录
    【整理】窗体消息(Window Messages) 的宏定义
  • 原文地址:https://www.cnblogs.com/weeping/p/6693262.html
Copyright © 2011-2022 走看看