PAT甲题题解-1076. Forwards on Weibo (30)-BFS
题目大意:给出每个用户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的更多相关文章
- PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题
水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- PAT甲题题解-1014. Waiting in Line (30)-模拟,优先级队列
题意:n个窗口,每个窗口可以排m人.有k为顾客需要办理业务,给出了每个客户的办理业务时间.银行在8点开始服务,如果窗口都排满了,客户就得在黄线外等候.如果有一个窗口用户服务结束,黄线外的客户就进来一个 ...
- PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)
(先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- unbuntu 安装python包提示E: Unable to locate package python-timeout
今天本想着在unbuntu环境下安装python的一个包,安装了几次都提示 E: Unable to locate package python-timeout 查阅了一些信息才知道,原来是一些软件源 ...
- 系统升级win7 sp1后,ado,MSJRO.tlh error 问题
MSJRO.tlh() : error C2501: '_RecordsetPtr' : missing storage-class or type specifiers MSJRO.tlh() : ...
- C++第六次作业
前言 拿到作业的时候,整个人都不好了,虽然之前和同学说以后一起写游戏,画界面,然而现在的自己对界面的知识一窍不通,虽然同学分享了一些资料,但是通过这次作业,发现自己火候还是不够-- 问题描述及仓库地址 ...
- Django商城项目笔记No.10用户部分-登录接口
Django商城项目笔记No.10用户部分-登录接口 添加url路由 接下来第二步,增加返回内容: 增加结果如下: 配置:上边的方法定义了返回的内容都有哪些,那这个方法jwt还不知道,需要配置: 修改 ...
- python class根据配置自定义函数
今天看到了一种有趣的定义函数的方式: class Test(object): def define_get_methods(cls, method_name, path): def inner_get ...
- python第四十四课——继承性之单继承
2.继承性 继承: 使用场景: 1).生活层面:... 2).计算机层面: 两部分组成,一部分我们称为父类(基类.超类.superclass),另一部分我们称为子类(派生类.subclass), 子类 ...
- header头
<?php header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在 heade ...
- [Java123] Spring
最近转组需要Hands on进行一些Java开发工作. 已经不是用十几年前初级Java写代码就能应付的了. 踏踏实实拾起来过去含含糊糊走过的章节吧. https://www.cnblogs.com/x ...
- PAT B1003 我要通过!
“答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...
- Kafka 集群部署
kafka是一个分布式消息队列,需要依赖ZooKeeper,请先安装好zk集群 kafka安装包解压 $ -0.9.0.1.tgz $ -0.9.0.1 /usr/kafka $ cd /usr/ka ...