796D(bfs)

题目链接: http://codeforces.com/problemset/problem/796/D

题意: 给出一颗 n 个节点树, 树枝连接的两个定点距离为 1, 树中有 k 个特殊点, 问最多可以删除哪些树枝, 使得树中其他顶点到特殊点的最小距离不大于 d.

注意: 题目说明了一定有解.

思路: bfs

可以直接 bfs 求其他点到特殊点的最短距离, 因为题目说明给出的数据都是有解的, 即所有顶点到特殊点的距离都是不大于 d 的. 那么搜完所有顶点后剩余的边就是不必要的.  即答案中的可删除边.

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <queue>
 5 #include <map>
 6 using namespace std;
 7
 8 const int MAXN = 3e5 + 10;
 9 int vis[MAXN], tag[MAXN];
10 map<pair<int, int>, int> mp;
11 vector<int> vt[MAXN];
12 queue<int> q;
13
14 int main(void){
15     int n, k, d, x, y, ans = 0;
16     scanf("%d%d%d", &n, &k, &d);
17     for(int i = 0; i < k; i++){
18         scanf("%d", &x);
19         q.push(x);
20         vis[x] = 1;
21     }
22     for(int i = 1; i <= n - 1; i++){
23         scanf("%d%d", &x, &y);
24         vt[x].push_back(y);
25         vt[y].push_back(x);
26         mp[{x, y}] = mp[{y, x}] = i;
27     }
28     while(!q.empty()){
29         int p = q.front();
30         q.pop();
31         for(int i = 0; i < vt[p].size(); i++){
32             if(!vis[vt[p][i]]){
33                 vis[vt[p][i]] = 1;
34                 q.push(vt[p][i]);
35                 tag[mp[{p, vt[p][i]}]] = 1;
36                 ans++;
37             }
38         }
39     }
40     cout << n - ans - 1 << endl;
41     for(int i = 1; i <= n - 1; i++){
42         if(!tag[i]) cout << i << " ";
43     }
44     cout << endl;
45     return 0;
46 }

时间: 2024-08-10 01:53:33

796D(bfs)的相关文章

CodeForces 796D bfs

CodeForces 796D 题意:n个城市,k个警察局,n-1条边连成树,所有边长都为1,给定的图满足规则:任一城市到离它最近的警察局距离不超过d. 问你最多可以删掉多少条边,使得依旧满足规则. tags:从所有警察局开始一起bfs,这样当要走向一个点to的时候,肯定是离警察局最近的路.如果to没有走过,就说明这条边 i 是需要的,标记它.最后没有被标记到的边就是不需要的.  注意坑点:d 可以为0 #include<bits/stdc++.h> using namespace std;

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

BFS+康托展开(洛谷1379 八数码难题)

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变. 输入格式: 输入初试状态,一行九个数字,空格用0表示 输出格式: 只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据) 输入样例#1: 2831

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

POJ3967Ideal Path[反向bfs 层次图]

Ideal Path Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1754   Accepted: 240 Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colo

胜利大逃亡(续)(状态压缩bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

邻接表实现Dijkstra算法以及DFS与BFS算法

//============================================================================ // Name : ListDijkstra.cpp // Author : fffff // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //==========================

图的两种遍历-DFS&amp;BFS

DFS和BFS在图中的应用: 图连通性判定:路径的存在性:图中是否存在环:求图的最小生成树:求图的关键路径:求图的拓扑排序. DFS:简单的说,先一直往深处走,直到不能再深了,再从另一条路开始往深处走,直到所有路都走完: struct node { int next; //E[i].next指向图中与i同父的下一个结点 int to; //E[i].to指向图中i的子结点 }E[110]; int N; int fa[110]; //记录各点的父结点 bool vis[110]; //记录这个点