不能一个一个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. Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全

    [相关已翻译的本系列其他文章,点击分类里面的spring security 4] 上一篇:Spring Security 4 整合Hibernate 实现持久化登录验证(带源码) 原文地址:http: ...

  2. 上传图片如何对图片进行压缩canvas

    前言:哈喽,朋友们,最近一直在马不停蹄地赶项目,很久没有写博客了.今天我们来看一下前端上传图片地时候如何对图片进行压缩 1.图片上传 我近期写项目都是使用的VUE,这里上传图片使用了Element-u ...

  3. 操作SDO_GEOMETRY字段

    读取SDO_GEOMETRY字段 select  to_char(regexp_replace(sdo_util.to_gmlgeometry(t.intsxn_geom),'</?[^> ...

  4. codeforces1249-div3

    A B C 等比数列的性质,前面的i项的和,不会超过第i+1项 D 有若干个区间,要求每一个点被区间覆盖的次数不能超过k个.问移除的最少的区间的数目. 贪心: 若某个点被覆盖了k次以上,那么肯定是移除 ...

  5. 2019-6-23-修复-dotnet-Core-缺SDK编译失败

    title author date CreateTime categories 修复 dotnet Core 缺SDK编译失败 lindexi 2019-6-23 10:55:9 +0800 2019 ...

  6. 使用基于Apache Spark的随机森林方法预测贷款风险

    使用基于Apache Spark的随机森林方法预测贷款风险   原文:Predicting Loan Credit Risk using Apache Spark Machine Learning R ...

  7. Gora是一个类似Hibernate的ORM框架

    Gora是一个类似Hibernate的ORM框架,但是不只是支持关系数据库,更重要支持NoSQL之类大数据的存储. 支持NoSQL之类大数据的存储 Apache Gora是一个开源的ORM(Objec ...

  8. H3C 高级ACL

  9. 降智严重——nowcoder练习赛46&&codeforces #561 Div2

    两场比赛降智不停,熬夜爆肝更掉rating nowcoder: https://ac.nowcoder.com/acm/contest/894#question T1:水题 T2:考虑a和b的子区间! ...

  10. UVa 1354 Mobile Computing[暴力枚举]

    **1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...