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 ...
随机推荐
- 有关java编辑器myeclipse编辑网站的一些设置(个人习惯)
一.界面显示设置 首先进入一个新的空间,里面的设置肯定都是默认的.点击上方导航栏的window-Perferences-Appearance可以去进行设置界面的显示,Theme中可以选择windows ...
- November 13th, 2017 Week 46th Monday
Don't undermine your worth by comparing yourself with others. 别拿自己和他人比较,这只会降低你原有的价值. Honestly, I don ...
- vlanif和vlan路由
配置基于接口划分VLAN示例(接入层设备作为网关) 简介 划分VLAN的方式有:基于接口.基于MAC地址.基于IP子网.基于协议.基于策略(MAC地址.IP地址.接口).其中基于接口划分VLAN,是最 ...
- 本博客已不再更新,新文章将发布在我的个人博客:https://www.tapme.top
如题,本博客已不再更新,请访问个人博客:www.tapme.top
- MySQL keepalived 双主.md
MySQL keepalived 双主搭建 环境说明 系统 IP 主机名 mysql keepalived VIP CentOS 6.8 192.168.197.61 C6-node1 5.6.36 ...
- oracle11g dataguard 备库数据同步的检查方法
概述: 一.环境 主库: ip地址:192.168.122.203 oracle根目录:/data/db/oracle SID:qyq 数据文 ...
- single number和变体
给array of integers. 裡面有一个数字是单独出现 其他都会出现两次(而且一起出现)ex: [1,2,2,3,3]要判断哪个数字是单独出现的. 以这个例子的话就是 1 LZ 一开始先说 ...
- Android Des加密解密
算法转自:http://www.linuxidc.com/Linux/2011-08/41866.htm import java.security.Key; import java.security. ...
- (二)基于阿里云的MQTT远程控制(购买阿里云,在云端安装MQTT,测试MQTT远程通信)
QQ名称为Friday~的网友把他自己买MQTT的过程截图发给了我,今天就说一下如何购买阿里云,安装MQTT可以参考 http://www.cnblogs.com/yangfengwu/p/77646 ...
- php中经常使用的string函数
strpos() ---返回字符串在另一字符串中首次出现的位置 strrpos() ---查找字符串在另一字符串中最后出现的位置 strchr() === strstr() ---找到字符 ...