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 ...
随机推荐
- JS基本包装类型之三(String)
1. 基本数据类型和基本包装类型 这里以字符串类型来讲解基本数据类型和基本包装类型. JS中存在基本数据类型String(typeof返回"string"), 也存在基本包装数据类 ...
- 计算机网络原理和OSI模型与TCP模型
计算机网络原理和OSI模型与TCP模型 一.计算机网络的概述 1.计算机网络的定义 计算机网络是一组自治计算机的互连的集合 2.计算机网络的基本功能 a.资源共享 b.分布式处理与负载均衡 c.综合信 ...
- 学习windows编程 day4 之 矩形的操作
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...
- Hi3519v101-uboot-start.S分析
00032: #include <config.h>00033: #include <version.h> 1)#include <config.h>.config ...
- 微信接口开发之高级篇系列【微信JS-SDK】
PHP微信公众平台开发高级篇—微信JS-SDK 第一步.绑定域名: 第二步.引入JS文件: 第三部.通过Config接口注入权限验证配置 第四部.通过Read接口处理成功验证 第五部.通过Error接 ...
- Gitlab配置阿里邮件通知
1. 在/etc/gitlab/gitlab.rb 中添加如下内容 $ vi /etc/gitlab/gitlab.rb gitlab_rails['smtp_enable'] = true git ...
- JS 互相调用iframe页面中js方法、VUE里 iframe 互调方法
1,父 html 调用子 iframe 内方法: document.getElementById("iframe").contentWindow.func(data1,data2. ...
- ;(function ($, undefined){ })(jQuery); 的使用及说明
对于很多初学者来说很难明白这表示什么,下边我将为大家介绍其相应的作用. 1.代码最前面的分号,可以防止多个文件压缩合并以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2.匿名函数(func ...
- asp.net mvc4 Json问题
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- [C++]数组指针与指针数组
//声明: 1.&----取地址运算符 eg: int m = 1; int *p = &m;//(*p) == m的地址 == &m; 2.*----间接访问运算符 eg: ...