96D - Police Stations

96D - Police Stations

思路:bfs,从所有的警察局开始bfs,因为bfs的深度一样,而且题目给的树保证满足条件,所以不用考虑深度。

如果搜索到一个点a,他的下一个点b已经被搜索过了,而且a到b这条路没有被走过,那么这条路可以被砍掉。

不能用dfs,这样可能导致某些点搜索不到,反例读者自己找。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=3e5+5;
bool vis[N]={false};
bool vs[N]={false};
vector<int>g[N];
vector<int>edge[N];
vector<int>ans;
int n,k,d,a,u,v;
queue<int>q;

void bfs()
{
    int now;
    int nxt;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<g[now].size();i++)
        {
            if(!vis[g[now][i]])vis[g[now][i]]=true,vs[edge[now][i]]=true,q.push(g[now][i]);
            else if(!vs[edge[now][i]])ans.pb(edge[now][i]);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k>>d;
    for(int i=0;i<k;i++)cin>>a,vis[a]=true;
    for(int i=0;i<n-1;i++)cin>>u>>v,g[u].pb(v),g[v].pb(u),edge[v].pb(i+1),edge[u].pb(i+1);

    for(int i=1;i<=n;i++)if(vis[i])q.push(i);
    bfs();
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout<<ans.size()<<‘\n‘;
    for(int i=0;i<ans.size();i++)cout<<ans[i]<<‘ ‘;
    cout<<‘\n‘;
    return 0;
}           
时间: 2024-10-18 05:51:31

96D - Police Stations的相关文章

CF796D Police Stations BFS+染色

类似贪心,用 BFS 对树进行染色,然后枚举哪些边的两个端点颜色不同. code: #include <bits/stdc++.h> #define N 300006 #define setIO(s) freopen(s".in","r",stdin) using namespace std; vector<int>G; queue<int>q; int n,k,d,edges,vis[N],hd[N],to[N<<1

ZOJ 2699 Police Cities

Police Cities Time Limit: 10 Seconds      Memory Limit: 32768 KB Once upon the time there lived a king and he had a big kingdom. And there were n cities in his kingdom and some of them were connected by the roads. And the roads were all one-way becau

Codeforces Round #408 (Div. 2) D

Description 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,

Codeforces Round #408 (Div. 2)ABCD

A. Buying A House 分别向左右枚举,找到符合题意的点,输出最短的长度*10. #include <bits/stdc++.h> using namespace std; int n, k, s, a[105]; int main() { scanf("%d%d%d", &n, &s, &k); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); int m

scoj2832: Mars city fzoj1462

Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Year 2900. Many people left Earth and built some cities on Mars. Currently there are N cities, some of which are connected by narrow one-way roads. The president of Mars has decided

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018 Problem A. Can Shahhoud Solve it? Problem B. Defeat the Monsters Problem C. UCL Game Night Problem

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

Jumbo versus the police

原文 Last Christmas, the circus owner, Jimmy Gates, decided to take some presents to a children' hospital. Dressed up as Father Christmas and accompanied by a 'guard of honour' of six pretty girls, he set off down the main street of the city riding a b

gas stations

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e