不能一个一个bfs,要一起bfs

#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 300100
using namespace std;
vector<int>G[maxn];
void insert(int be, int en) {
G[be].push_back(en);
}
int n, m, k;
int vis[maxn];
queue<int>que;
int bfs() {
while (!que.empty()) {
int x = que.front();
que.pop();
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (!vis[p]) {
vis[p] = vis[x];
que.push(p);
}
}
}
return 0;
}
struct Node {
int be, en;
}cnn[maxn];
vector<int>ins; int main() {
int be, en;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d", &be);
if(vis[be] == 0) que.push(be);
vis[be] = i;
}
for (int i = 1; i < n; i++) {
scanf("%d %d", &be, &en);
insert(be, en);
insert(en, be);
cnn[i].be = be;
cnn[i].en = en;
}
int cnt = 0;
bfs();
for (int i = 1; i < n; i++) {
be = cnn[i].be;
en = cnn[i].en;
if (vis[be] != vis[en]) {
ins.push_back(i);
}
}
printf("%d\n", ins.size());
for (int i = 0;i< ins.size(); i++) {
printf("%d ", ins[i]);
}
printf("\n");
return 0;
}

  

CodeForces 796D的更多相关文章

  1. 【codeforces 796D】Police Stations

    [题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...

  2. CodeForces - 796D Police Stations bfs

    思路:删除尽量多的边使得所有点都能在限制距离之内到达一个警局,删除边会形成多棵子树,最多只能k棵.其实就是以每个警局为根结点,把整棵树划分为以警局为根结点的k棵树,说明要删除的边的数量就是k-1条,即 ...

  3. Police Stations CodeForces - 796D (bfs)

    大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...

  4. codeforces 796A-D

    决定在 codeforces 练题啦,决定每个比赛刷前四道...太难就算了 796A Buying A House 题意:给出x轴上的n 个点,每个点有个权值,问离m 点最近的权值小于等于k 的点离m ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. 注意特殊情况!最长上升子序列!!poj2533

    poj 2533 简单的动归.用O(n^2)的算法也能过.但是有个细节!刚开始ans初始化为0时是错的!!!要初始化为1.因为只有1个数的时候,下面的循环是不会执行的.....或者特判.. #incl ...

  2. day15 web前端之css

    css的概念以及初体验 概念: CSS(cascading style sheet)也就是层叠样式表:它是一种网页设计的新技术,现在已经被大多数浏览器所支持,层位网页设计必不可少的工具之一.优点:   ...

  3. JavaScript 开发者都应该知道的十个概念

    1. 原始值和引用值(Value vs. Reference) 理解对象.数组和函数是如何复制和传递到函数中的.了解引用值是被复制了什么,理解原始值是通过复制值来进行复制和传递的. 简析:ECMASc ...

  4. Python语言的缺点

  5. Python语言的特点

  6. Libev源码分析08:Libev中的内存扩容方法

    在Libev中,如果某种结构的数组需要扩容,它使用array_needsize宏进行处理,比如: array_needsize (int, fdchanges, fdchangemax, fdchan ...

  7. uva 10253 Series-Parallel Networks (整数划分+多重集)

    UVa Online Judge 题意是计算给定数量的边通过串联并联两种方式,能组成多少种不同的网络.将它转化为一个树形结构,也就是求有多少不同构的树. 代码如下: #include <cstd ...

  8. behavior planning——15.cost function design weightTweaking

    Designing cost functions is difficult and getting them all to cooperate to produce reasionable vehic ...

  9. js实现开关灯游戏

    需求: 点击三个按钮,页面出现不同数量的“灯” 所有的灯有相同的点击效果.点击一个灯的时候,this和他的上下左右都会变成另一种背景色. 代码在这里~~~ 文章地址 https://www.cnblo ...

  10. scala资料总结,一些小技巧

    scala资料总结,一些小技巧 1.得到每种数据类型所表示的范围 Short.MaxValue 32767 Short.MinValue -32768 Int.MaxValue 2147483647 ...