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 ...
随机推荐
- SDN期末作业-通过SDN的应用实现负载均衡
负载均衡程序 1.程序链接:https://github.com/424baopu/software/tree/master/LoadBalance 2.场景 topo: 场景描述: 服务器host ...
- 阿里八八Alpha阶段Scrum(5/12)
今日进度 叶文滔: 与添加日程界面完成界面对接. 问题困难:发现浮动按钮拖曳存在BUG,无法正确判断拖曳与点击事件,已经修复为普通悬浮按钮. 林炜鸿: 绘制完成添加日程界面. 李嘉群: 1.尝试有关用 ...
- Stop Bitbucket prompting for password in git
出处:http://qosys.info/485/bitbucket-git-prompt-for-password In some cases after adding public ssh key ...
- facebook api & oauth protocal
http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-10.5 http://stackoverflow.com/questions/14 ...
- python string.md
string 包含用于处理文本的常量和类.string模块始于Python的最早版本. 2.0版本中, 许多之前只在模块中实现的函数被转移为string对象的方法. 之后的版本中, 虽然这些函数仍然可 ...
- [SDOI2008]洞穴勘测
嘟嘟嘟 写完lct的板儿后觉得这就是一道大水题. 连pushup都不用. 不过还是因为一个zz的错误debug了一小会儿(Link的时候连出自环--) 还有一件事就是Cut的时候判断条件还得加上,因为 ...
- Python高级网络编程系列之基础篇
一.Socket简介 1.不同电脑上的进程如何通信? 进程间通信的首要问题是如何找到目标进程,也就是操作系统是如何唯一标识一个进程的! 在一台电脑上是只通过进程号PID,但在网络中是行不通的,因为每台 ...
- Grunt-jsdoc生成JS API文档
Grunt-jsdoc生成JS API文档 具体的请看官网 https://github.com/krampstudio/grunt-jsdoc 一:首先确保本机电脑上是否已经安装了nodejs和np ...
- docker push images login -u harbor 问题记录 https 证书
1.[root@dev-100 Desktop]# docker login -u clouder -p engine harbor.xiaowei.com 2.docker tag busybox: ...
- BroadcastReceiver广播相关 - 转
BroadcastReceiver广播接收者用于接收系统或其他程序(包括自己程序)发送的广播. 一.注册广播 在android中,我们如果想接收到广播信息,必须自定义我们的广播接收者.要写一个类来继承 ...