//标准的层次遍历模板

//居然因为一个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. 关于Linux中exec的一点心得

    最近在学习linux操作系统中的相关知识,在使用execlp系统调用时,发现了些有趣的东西. 首先,关于execlp函数的用法: int execlp(const char *file, const ...

  2. 回调--一个经典例子让你彻彻底底理解java回调机制

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 以前不理解什么叫回调 ...

  3. 图片_ _Android--加载大分辨率图片到内存

    http://www.cnblogs.com/plokmju/p/android_LoadBigImage.html#3084005 前言 在使用ImageView显示图片的时候,直接加载一个图片资源 ...

  4. stdlib

    system(pause): int cmp(const void *a ,const void *b) { return *(int *)a - *(int *)b ;          //从小到 ...

  5. JS定时执行,循环执行

    从http://www.jb51.net/article/23863.htm  转载 Document自带的方法: 循环执行:var timeid = window.setInterval(“方法名或 ...

  6. 设置oracle_home

    set ORACLE_HOME=F:\app\rh\product\11.2.0\dbhome_1

  7. SQL Server 2005中的分区表(五):添加一个分区(转)

    所谓天下大事,分久必合,合久必分,对于分区表而言也一样.前面我们介绍过如何删除(合并)分区表中的一个分区,下面我们介绍一下如何为分区表添加一个分区. 为分区表添加一个分区,这种情况是时常会 发生的.比 ...

  8. [Flex] ButtonBar系列——垂直布局

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  9. Delphi的Socket编程步骤(repulish)

    转贴自:http://topic.csdn.net/t/20010727/16/212155.html ClientSocket 和ServerSocket几个重要的属性:   1.client和se ...

  10. Android--输入自动提示AutoCompleteTextView

    布局文件: <TextView android:id="@+id/title" android:layout_width="wrap_content" a ...