题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,
每个人只考虑转发一次。
用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vis判断之前是否入过队列,不能重复入队。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=;
int vis[maxn];
int head[maxn];
int tot;
int ans=;
struct Edge{
int to;
int next;
}edge[maxn*]; void init(){
tot=;
memset(head,-,sizeof(head));
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} struct Node{
int u;
int layer;
};
void BFS(int u,int L){
queue<Node> q;
Node s,tmp;
s.u=u;
s.layer=;
q.push(s);
while(!q.empty()){
tmp=q.front();
q.pop();
if(tmp.layer>L)
break;
if(tmp.layer!=)
ans++;
for(int k=head[tmp.u];k!=-;k=edge[k].next){
int v=edge[k].to;
if(!vis[v]){
s.u=v;
s.layer=tmp.layer+;
vis[v]=; //要注意这里就要标记vis=1,而不是从队列里取出来时标记,会超时。
q.push(s);
}
}
}
}
int main()
{
int n,l,m,k,v;
init();
scanf("%d %d",&n,&l);
for(int i=;i<=n;i++){
scanf("%d",&m);
for(int j=;j<m;j++){
//注意题目,是i关注了m个人,也就是这m个人发布的消息能被i看到,建立v->i的边
scanf("%d",&v);
add(v,i);
}
}
int id;
scanf("%d",&k);
for(int i=;i<k;i++){
scanf("%d",&id);
memset(vis,,sizeof(vis));
vis[id]=;
ans=;
//dfs(id,0,l+1,id);
BFS(id,l);
printf("%d\n",ans);
}
return ;
}

PAT甲题题解-1076. Forwards on Weibo (30)-BFS的更多相关文章

  1. PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题

    水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  2. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

  3. PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

    题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...

  4. PAT甲题题解-1014. Waiting in Line (30)-模拟,优先级队列

    题意:n个窗口,每个窗口可以排m人.有k为顾客需要办理业务,给出了每个客户的办理业务时间.银行在8点开始服务,如果窗口都排满了,客户就得在黄线外等候.如果有一个窗口用户服务结束,黄线外的客户就进来一个 ...

  5. PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)

    (先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在 ...

  6. 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 ...

  7. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  8. PAT 1076. Forwards on Weibo (30)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  9. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

随机推荐

  1. MySQL基础之---mysqlimport工具和LOAD DATA命令导入文本文件

     1.mysqlimport工具的使用 看一下命令的使用方法: shell > mysqlimport -u root -p [--LOCAL] DBname File [option] --f ...

  2. 乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group

    乘风破浪:LeetCode真题_025_Reverse Nodes in k-Group 一.前言 将一个链表按照一定的长度切成几部分,然后每部分进行翻转以后再拼接成一个链表是比较困难的,但是这也能锻 ...

  3. SDN期末作业验收

    作业链接:https://edu.cnblogs.com/campus/fzu/SoftwareDefinedNetworking2017/homework/1585 负载均衡程序 1.github链 ...

  4. 死磕nginx系列--配置文档解读

    nginx配置文件主要分为四个部分: main(全局设置) http ( ) upstream(负载均衡服务器设置) server(主机设置) location(URL匹配特点位置的设置) serve ...

  5. laravel扩展推荐

    1. Intervention/image 图片处理 2.Laravel User Agent 轻松识别客户端信息 3.OAuth 2.0 支持 4.页面面包屑工具 5.计划任务分发器(直接可替换掉 ...

  6. Chrome插件(扩展)开发全攻略

    [干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html

  7. oracle 查询SQL 的执行速度

    SELECT SE.SID,       OPNAME,       TRUNC(SOFAR / TOTALWORK * 100, 2) || '%' AS PCT_WORK,       ELAPS ...

  8. 有crontab中的脚本不执行,需要在脚本里面export各种环境变量

    [oracle@sta ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ]; ...

  9. HDU1863(Kruskal+并查集水题)

    https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...

  10. 3-(基础入门篇)稍微了解一下(需要知道的关于Lua的一些基本的知识)

      http://www.cnblogs.com/yangfengwu/p/8948935.html 基础教程源码链接如果失效,请在淘宝介绍中下载,由于链接很容易失效,如果失效请联系卖家,谢谢 htt ...