不能一个一个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. python while 循环结构

  2. TensorFlow的 卷积层

    用 TensorFlow 做卷积 让我们用所学知识在 TensorFlow 里构建真的 CNNs.在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias.这在很大程度上 ...

  3. HTML打印print

    上代码: //打印 function printme() { global_Html = document.body.innerHTML; //document.body.innerHTML = do ...

  4. Open Source Projects Released By Google

    Open Source Projects Released By Google Google has released over 20 million lines of code and over 9 ...

  5. php解压缩

    1.zip文件 2.rar文件 3.php调用linux指令进行解压缩 解压7z文件: 注:Windows下的文件编码和LINUX不一样,中文系统为GB,LINUX为UTF-8编码,这种情况下,中文名 ...

  6. day1_python流程控制、For循环

    一.流程控制 条件语句 1.1.单分支 ? 1.2.多分支 ? 需求一.用户登陆验证 #!/usr/bin/env python name=input('请输入用户名字:') password=inp ...

  7. ubuntu18.04 挂载ntfs硬盘无法写入解决办法

    win10和ubuntu18.04双系统,在ubuntu下通过/etc/fstab挂载ntfs硬盘无写入权限,尝试通过chmod修改写入权限和ntfs-config图形工具修改写入权限均失败.在ubu ...

  8. XTU 1236 Fraction

    Fraction Accepted : 168   Submit : 1061 Time Limit : 1000 MS   Memory Limit : 65536 KB Fraction Prob ...

  9. JavaScript跨域问题

    通过实现Ajax通信的主要限制,来源于跨域安全策略.默认情况下,XHR对象只能访问与包含它的页面位于同一个域中的资源.这种安全策略可以预防某些恶意行为.但是,实现合理的跨域请求对于开发某些浏览器应用程 ...

  10. 在vuex的mutations中使用vue的小技巧

    问题: 在vuex组件中的mutations属性中的定义的函数,有时会要用到vue这个对象.正常在其他的地方使用是通过this这个变量来获取,但是在mutations定义的函数中this指定的是Vue ...