//标准的层次遍历模板

//居然因为一个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. OAF_EO系列2 - Validation数据校验验证机制(概念)

    2014-06-12 Created By BaoXinjian

  2. KMP算法的Next数组详解 转

    这个写的很好,还有讲kmp,值得一看. http://www.cnblogs.com/tangzhengyue/p/4315393.html 转载请注明来源,并包含相关链接. 网上有很多讲解KMP算法 ...

  3. NSMutableAttributedString 设置不同颜色,不同字体的String

    UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 20, 190, 70)]; infoLabel.backgroun ...

  4. sikuli+java实例

      新建java工程,导入sikuli-script.jar包 public class TestSikuli { public static void openPage() throws FindF ...

  5. Mysql 学习笔记 20140219

    1. Mysql常用命令:每个命令以分号结束. create database name;          创建数据库 use databasename;              选择数据库 dr ...

  6. mssqlserver 查询数据库表结构语句

    查询指定表结构的表名.列名.类型.说明.字段长度 select o.name as tableName,c.name as columnName,t.name as columnType,p.valu ...

  7. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

  8. 合并 hdfs 文件

     待研究,只做保存 将HDFS中不同目录下面的数据合在一起,并存放在指定的目录中,示例如: sqoop merge –new-data /test/p1/person –onto /test/p2 ...

  9. 【转】sed命令详解

    原文:http://www.cnblogs.com/emanlee/archive/2013/09/07/3307642.html sed命令行格式为: sed [-nefri]  'command' ...

  10. (转)C#使用Mysql记录

    (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 http://dev.mysql.com/downloads/connector/ 我下载的版本为: mysql-connector ...