绝对是好题,把所有警察局放入队列然后开始广搜,如果碰到了vis过的顶点,但是那条边没有访问过,那么这条边就可以删掉

另外广搜的vis标记是在入队时就打的,,

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 300005
  4.  
  5. struct Edge{
  6. int to,next,flag,id;
  7. }edge[maxn<<];
  8. int n,k,d,head[maxn],tot,p[maxn],vis[maxn];
  9.  
  10. void init(){
  11. tot=;
  12. memset(vis,,sizeof vis);
  13. memset(head,-,sizeof head);
  14. }
  15.  
  16. void addedge(int u,int v,int id){
  17. edge[tot].to=v;edge[tot].flag=;edge[tot].id=id;edge[tot].next=head[u];
  18. head[u]=tot++;
  19. }
  20.  
  21. int main(){
  22. int u,v;
  23. init();
  24. scanf("%d%d%d",&n,&k,&d);
  25. for(int i=;i<=k;i++)scanf("%d",&p[i]);
  26. for(int i=;i<n;i++){
  27. scanf("%d%d",&u,&v);
  28. addedge(u,v,i);addedge(v,u,i);
  29. }
  30.  
  31. queue<int> q;while(!q.empty())q.pop();
  32. for(int i=;i<=k;i++)q.push(p[i]),vis[p[i]]=;
  33. while(!q.empty()){
  34. int u=q.front();q.pop();
  35. //cout << u << '\n';
  36. for(int i=head[u];i!=-;i=edge[i].next){
  37. int v=edge[i].to;
  38. //cout << v << ' ';
  39. if(vis[v] && !edge[i].flag){
  40. edge[i].flag=edge[i^].flag=;
  41. continue;
  42. }
  43. else if(!vis[v] && !edge[i].flag){
  44. edge[i].flag=edge[i^].flag=;
  45. vis[v]=;
  46. q.push(v);
  47. }
  48. }
  49. }
  50. int ans=;
  51. for(int i=;i<tot;i+=) if(edge[i].flag==) ans++;printf("%d\n",ans);
  52. for(int i=;i<tot;i+=) if(edge[i].flag==) printf("%d ",edge[i].id);
  53. return ;
  54. }

cf796d 树,bfs好题!的更多相关文章

  1. hdu5325 树的思维题

    pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...

  2. WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS

    title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS date: 2020-03-20 10:43:00 categories: acm tags: [ ...

  3. HDU 4578 Transformation --线段树,好题

    题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...

  4. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  5. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  6. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  7. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  8. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  10. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. 学习windows编程 day3 之滚动条完善

    1.不再使用setscrollrange,setscrollpos,getscrollrange,getscrollpos这些函数,这只是有助于理解其中运行原理 2.改用setscrollinfo,g ...

  2. 【1】存在大于1s的FullGC

    目前有存在大于1s的FullGC,金桥的机器目前是2C4G的,使用的GC方法只能使用单线程进行串行的回收,导致GC比较慢. 建议可以调整GC参数,改用CMS,能够解决这个问题, 参数修改方法: 在应用 ...

  3. PHP 错误 系列:编码格式错误解决

    一.Phalcon模型代码日志错误代码 错误页面显示: Log日志错误代码: PHP message: PHP Fatal error: Namespace declaration statement ...

  4. SSM-1第一章 认识SSM框架和Redis

    第一章 认识SSM框架和Redis   1.1 Spring框架 Sring理念  Ioc  控制反转  IOC是一个容器,在Spring中,它会认为一切Java资源都是JavaBean,容器的目标是 ...

  5. js scroll函数

    $(function () { $(".sticky").hide(); var top = $(window).scrollTop(); if (top >= 100) { ...

  6. android AsyncHttpClient使用

    1.www.github.com下载jar包 loopj/android-async-http 将下载好的文件导入项目中 2.main.xml <?xml version="1.0&q ...

  7. [C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针

    int main(){ //单值指针(指向单个值得指针,类同普通变量) int *px = new int; *px = 100; printf("%d",*px); delete ...

  8. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

  9. 第5月第7天 php slim

    1. <?php require 'Slim/Slim.php'; require 'DBManagement.php'; \Slim\Slim::registerAutoloader(); $ ...

  10. Sqoop异常:Please set $ACCUMULO_HOME to the root of your Accumulo installation.

    shell-init: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: ...