//标准的层次遍历模板

//居然因为一个j写成了i,debug半天。。。。。解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1001;
struct node
{
int level;
vector<int>child;
};
node list[maxn];
int n,l;
bool vis[maxn];

int bfs(int v)
{
for(int j=0;j<maxn;j++){list[j].level=0;vis[j]=false;}
queue<int>q;
q.push(v);vis[v]=true;
int cnt=0;
while(!q.empty())
{
int tmp=q.front();q.pop();
if(list[tmp].level>l)break;
cnt++;
for(int i=0;i<list[tmp].child.size();i++)
{
int s=list[tmp].child[i];
if(!vis[s])
{
list[s].level=list[tmp].level+1;
vis[s]=true;
q.push(s);
}
}
}
return cnt-1;
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,tmp;
while(scanf("%d%d",&n,&l)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%d",&tmp);
list[tmp].child.push_back(i);
}
}
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d",&tmp);
int ans=bfs(tmp);
printf("%d\n",ans);
}
}
return 0;
}

PAT1076. Forwards on Weibo(标准bfs模板)的更多相关文章

  1. PAT甲题题解-1076. Forwards on Weibo (30)-BFS

    题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...

  2. PAT1076. Forwards on Weibo (30)

    使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using nam ...

  3. PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)

    1076 Forwards on Weibo (30分)   Weibo is known as the Chinese version of Twitter. One user on Weibo m ...

  4. PAT 1076 Forwards on Weibo[BFS][一般]

    1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...

  5. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

  6. 1076 Forwards on Weibo (30 分)

    1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...

  7. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...

  8. HDU5012:Dice(bfs模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the ...

  9. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

随机推荐

  1. SSH登录很慢问题的解决

    用ssh连其他linux机器,会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析的问题 OpenSSH在用户登录的 ...

  2. svn在linux下的使用(转)

    ubuntu命令行模式操作svn 首先要安装SVN客户端到你的系统才能操作各种命令 apt-get install subversion 1.将文件checkout到本地目录 svn checkout ...

  3. .net EntityFramework用法探索系列 1

    EntityFramework用法探索系列 (一)DatabaseFirst (二)CodeFirst (三)CodeFirst流畅API (四)Repository和UnitOfWork (五)引入 ...

  4. hdu 5437 Alisha’s Party 模拟 优先队列

    Problem Description Princess Alisha invites her friends to come to her birthday party. Each of her f ...

  5. studio--问题

    2,Android studio 怎么能删除干净一个module? 工程→open module settings→选中你要删除的module,然后点“一”,确定后返回,即可在module邮件下看到d ...

  6. ylbtech-Unitity-CS:Generics

    ylbtech-Unitity-CS:Generics 1.A,效果图返回顶部 Unsorted List: Raul:35 Alessandro:30 Maria:72 Hiroyuki:108 A ...

  7. JAVA 创建类,使用类

    一.创建类: Test.java //定义类 public class Test{ //属性 String name; String gender; int age; //方法,无参无返回 publi ...

  8. java多线程之ThreadLocal

    ThreadLocal为每个线程保存变量,以保证数据同步. package Thread.Common; import java.util.Random; import java.util.concu ...

  9. 如何搭建Struts2环境

    1.解压下载到的struts-2.3.16压缩包. 2.将struts2-blank\WEB-INF\lib 下的jar包复制到Eclipse项目下的libs文件夹下. 3.struts-2.3.16 ...

  10. orientationchange不管用啊

    首先引入JQuery Mobile包,将 <script> //手持设备方向改变时执行 $(window).bind( 'orientationchange', function(e){ ...