【codeforces 796D】Police Stations

【题目链接】:http://codeforces.com/contest/796/problem/D

【题意】

在一棵树上,保证每个点在距离d之内都有一个警察局;
让你删掉最多的边,使得剩下的森林仍然满足上述约束;

【题解】

从每个警察局开始进行bfs;
这样每个路线第一次到达的点肯定是最短路;
所以bfs的时候遇到没访问过的点,就记录它被访问,即它被这个状态一开始的警察局所管辖(控制);
则可以想象每个警察局都同时往外扩张;
则每个警察局管的范围就确定是那些了;
则那些点肯定是到那个管辖它的警察局最近的;
这样就能确定最后哪些边是要保留下来的了;
删掉剩余的边就好;
(每个点开始找最短路是哪条边的,那条边就保留下来);
警察局不用分配给它边.

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 3e5+1000;

int n,k,d,dis[N];
int p[N];
vector <pii> G[N];
queue <int> dl;
bool bo[N];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ms(dis,255);
    rei(n),rei(k),rei(d);
    rep1(i,1,k)
        rei(p[i]),dl.push(p[i]),dis[p[i]] = 0;
    rep1(i,1,n-1)
    {
        int x,y;
        rei(x),rei(y);
        G[x].ps(mp(y,i)),G[y].ps(mp(x,i));
    }
    while (!dl.empty())
    {
        int x = dl.front();
        dl.pop();
        for (pii temp:G[x])
        {
            int y = temp.fi;
            if (dis[y]==-1)
            {
                dis[y] = dis[x]+1;
                dl.push(y);
            }
        }
    }
    int cnt = 0;
    rep1(i,1,n)
    if (dis[i]!=0)
    {
        int mi = 21e8,id = -1;
        for (pii temp:G[i])
        {
            int y = temp.fi;
            if (dis[y]<mi)
            {
                mi = dis[y],id = temp.se;
            }
        }
        cnt++;
        bo[id] = true;
    }
    printf("%d\n",n-1-cnt);
    rep1(i,1,n-1)
    if (!bo[i])
        {
            printf("%d ",i);
        }
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(142)  评论(0编辑  收藏  举报