PAT A1076 Forwards on Weibo (30 分)——图的bfs
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:
M[i] user_list[i]
where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.
Then finally a positive K is given, followed by K UserID's for query.
Output Specification:
For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.
Sample Input:
7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6
Sample Output:
4
5
#include <stdio.h>
#include <algorithm>
#include <set>
//#include <string.h>
#include <vector>
//#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
const int inf = ;
int n,k,m,l; bool vis[maxn];
struct node{
int id,lvl;
};
vector<node> adj[maxn];
int bfs(int root){
fill(vis,vis+maxn,false);
int num=;
queue<node> q;
node st;
st.id = root;
st.lvl = ;
q.push(st);
vis[root]=true;
while(!q.empty()){
node now = q.front();
q.pop();
int u=now.id;
for(int i=;i<adj[u].size();i++){
node next = adj[u][i];
next.lvl = now.lvl+;
if(vis[adj[u][i].id]==false && next.lvl<=l){
q.push(next);
vis[next.id]=true;
num++;
}
}
}
return num;
}
int main(){
scanf("%d %d",&n,&l);
for(int i=;i<=n;i++){
scanf("%d",&k);
node user;
user.id = i;
for(int j=;j<k;j++){
scanf("%d",&m);
adj[m].push_back(user);
}
}
scanf("%d",&k);
for(int j=;j<k;j++){
scanf("%d",&m);
int res=bfs(m);
printf("%d\n",res);
}
}
注意点:图的多次bfs,要注意初始化vis,和dfs不同的是bfs的vis是入队一次就要设为true,而不是访问了才设为true,否则会多次访问到。记录层数需要结构体,和A1106 Lowest Price In Supply Chain 不同,1106需要两个队列来完成一层层保存,因为他是二叉树,而图会存在环,两个队列也可以,不过这题没必要。
PAT A1076 Forwards on Weibo (30 分)——图的bfs的更多相关文章
- 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 ...
- 【PAT甲级】1076 Forwards on Weibo (30 分)
题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...
- 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 ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- 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[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
随机推荐
- Java坦克大战(一)
接下来的几篇博客,想记录一下通过学习坦克大战项目来循序渐进的学习Java基础.主要是为了巩固基础知识,当然学习编程重要的还是多敲,问题通常是在敲代码的过程中发现的,积累也是在敲代码中寻求的经验.这个坦 ...
- 【Spring】26、利用Spring的AbstractRoutingDataSource解决多数据源,读写分离问题
多数据源问题很常见,例如读写分离数据库配置. 1.首先配置多个datasource <bean id="dataSource" class="org.apache. ...
- 配置supervisor管理beego应用
一.golang.beego等环境安装与配置 二.supervisor安装 github项目地址:https://github.com/Supervisor/supervisor 克隆项目:git c ...
- js 前端有消息了 声音提示给用户
前言:工作中有需求,在数据变更有变更时采用声音提示给用户,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9936180.html 网站地址:我的个 ...
- FormData对象的使用
一.概述 FormData类型是XMLHttpRequest 2级定义的,它是为序列化表以及创建与表单格式相同的数据提供便利. 作用:1.利用一些键值对来模拟一系列表单控件:即将form中的所有表单元 ...
- 《Inside C#》笔记(十五) 非托管代码 下
二编写不安全代码 a)fixed关键字 代码中体现了fixed的用法:fixed (type* ptr= expression) { …}:type是类似int*这样的非托管类型或void类型,exp ...
- HDFS Sink使用技巧
1.文件滚动策略 在HDFS Sink的文件滚动就是文件生成,即关闭当前文件,创建新文件.它的滚动策略由以下几个属性控制: hdfs.rollInterval 基于时间间隔来进行文件滚动,默认是30, ...
- 安卓APP应用在各大应用市场上架方法整理
想要把APP上架到应用市场都要先注册开发者账号才可以.这里的方法包括注册帐号和后期上架及一些需要注意的问题.注意:首次提交应用绝对不能随便删除,否则后面再提交会显示应用APP冲突,会要求走应用认领流程 ...
- SQL PLUS的语句执行Commit
oracle 中有个commit,是用来提交事务的.今天发现sql developer和sql plus的数据查询不一样. 如果我们对数据库进行增删改查,在提交sql语句之后,如果不点击commit, ...
- centos6.9设置桥接网络模式方法
第一步:设置 VMware 在 VMware 中打开[编辑]->[虚拟网络编辑器],添加 VMnet0,并选择桥接模式.需要注意的是,需要选择“桥接到”的网卡,使用无线网卡就选无线网卡,使用有线 ...