cf796d 树,bfs好题!
绝对是好题,把所有警察局放入队列然后开始广搜,如果碰到了vis过的顶点,但是那条边没有访问过,那么这条边就可以删掉
另外广搜的vis标记是在入队时就打的,,
- #include<bits/stdc++.h>
- using namespace std;
- #define maxn 300005
- struct Edge{
- int to,next,flag,id;
- }edge[maxn<<];
- int n,k,d,head[maxn],tot,p[maxn],vis[maxn];
- void init(){
- tot=;
- memset(vis,,sizeof vis);
- memset(head,-,sizeof head);
- }
- void addedge(int u,int v,int id){
- edge[tot].to=v;edge[tot].flag=;edge[tot].id=id;edge[tot].next=head[u];
- head[u]=tot++;
- }
- int main(){
- int u,v;
- init();
- scanf("%d%d%d",&n,&k,&d);
- for(int i=;i<=k;i++)scanf("%d",&p[i]);
- for(int i=;i<n;i++){
- scanf("%d%d",&u,&v);
- addedge(u,v,i);addedge(v,u,i);
- }
- queue<int> q;while(!q.empty())q.pop();
- for(int i=;i<=k;i++)q.push(p[i]),vis[p[i]]=;
- while(!q.empty()){
- int u=q.front();q.pop();
- //cout << u << '\n';
- for(int i=head[u];i!=-;i=edge[i].next){
- int v=edge[i].to;
- //cout << v << ' ';
- if(vis[v] && !edge[i].flag){
- edge[i].flag=edge[i^].flag=;
- continue;
- }
- else if(!vis[v] && !edge[i].flag){
- edge[i].flag=edge[i^].flag=;
- vis[v]=;
- q.push(v);
- }
- }
- }
- int ans=;
- for(int i=;i<tot;i+=) if(edge[i].flag==) ans++;printf("%d\n",ans);
- for(int i=;i<tot;i+=) if(edge[i].flag==) printf("%d ",edge[i].id);
- return ;
- }
cf796d 树,bfs好题!的更多相关文章
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...
- WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS date: 2020-03-20 10:43:00 categories: acm tags: [ ...
- HDU 4578 Transformation --线段树,好题
题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...
- Hihicoder 题目1 : Trie树(字典树,经典题)
题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- 学习windows编程 day3 之滚动条完善
1.不再使用setscrollrange,setscrollpos,getscrollrange,getscrollpos这些函数,这只是有助于理解其中运行原理 2.改用setscrollinfo,g ...
- 【1】存在大于1s的FullGC
目前有存在大于1s的FullGC,金桥的机器目前是2C4G的,使用的GC方法只能使用单线程进行串行的回收,导致GC比较慢. 建议可以调整GC参数,改用CMS,能够解决这个问题, 参数修改方法: 在应用 ...
- PHP 错误 系列:编码格式错误解决
一.Phalcon模型代码日志错误代码 错误页面显示: Log日志错误代码: PHP message: PHP Fatal error: Namespace declaration statement ...
- SSM-1第一章 认识SSM框架和Redis
第一章 认识SSM框架和Redis 1.1 Spring框架 Sring理念 Ioc 控制反转 IOC是一个容器,在Spring中,它会认为一切Java资源都是JavaBean,容器的目标是 ...
- js scroll函数
$(function () { $(".sticky").hide(); var top = $(window).scrollTop(); if (top >= 100) { ...
- android AsyncHttpClient使用
1.www.github.com下载jar包 loopj/android-async-http 将下载好的文件导入项目中 2.main.xml <?xml version="1.0&q ...
- [C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针
int main(){ //单值指针(指向单个值得指针,类同普通变量) int *px = new int; *px = 100; printf("%d",*px); delete ...
- HDU2444 The Accomodation of Students【匈牙利算法】
题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...
- 第5月第7天 php slim
1. <?php require 'Slim/Slim.php'; require 'DBManagement.php'; \Slim\Slim::registerAutoloader(); $ ...
- Sqoop异常:Please set $ACCUMULO_HOME to the root of your Accumulo installation.
shell-init: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: ...