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 ...
随机推荐
- 通过swagger将API业务版本号与Gitlab代码版本号绑定
1.调用Gitlab API获取项目commit ID 2.编辑 Swagger2.java @Configuration @EnableSwagger2 @EnableWebMvc public c ...
- 【读书笔记】iOS-使用SQL数据库保存信息
使用BLOB字段来保存图片是不是一个好的方法还存在争议,小图片除外.更常用的方法是将图片保存为一个文件,然后只在数据中保存图片文件的元数据,比如文件的路径.但是,如果你想把数据文件(初始数据)打包成一 ...
- 读懂SAP Leonardo物联网平台
读懂SAP Leonardo物联网平台 https://blog.csdn.net/weixin_42137700/article/details/81903290 本文比较系统.全面地介绍了SAP ...
- Ubuntu切换root身份,命令行以中文显示
很多VPS商给的默认用户名并不是root,用以下命令处理即可: 1.修改root密码 sudo passwd root 输入密码,回车,再确认一次即可 2.更改密码后切换root身份 su root ...
- 利用奇异值分解(SVD)进行图像压缩-python实现
首先要声明,图片的算法有很多,如JPEG算法,SVD对图片的压缩可能并不是最佳选择,这里主要说明SVD可以降维 相对于PAC(主成分分析),SVD(奇异值分解)对数据的列和行都进行了降维,左奇异矩阵可 ...
- C# 异步编程4 async与await 异步程序开发
随着C#异步程序开发系列的深入,你会发现编写异步程序越发简单.事物的发展就是这样的规律,从简单到复杂再到简单. 在C# 5.0中我们可以通过async与await关键字实现快捷的异步程序开发,如下: ...
- [20180627]truncate table的另类恢复.txt
[20180627]truncate table的另类恢复.txt --//前几天看链接http://www.xifenfei.com/2018/06/truncate-table-recovery. ...
- CSS| 學習心得
resize :both , 只有overflow設置為auto時, 才能起作用???
- Django电商项目---完成登录验证和用户中心(个人信息)day3
登录验证的实现 背景说明: 用户在商品界面选择商品后,在点击购物车或者结算订单之前 需要完成用户的登录验证,这里用装饰器来完成 创建装饰器类: df_user/user_decorator.py ...
- yolo.h5制作方法
学习吴恩达的深度学习第三课缺少yolo.h5文件,花了很长时间来解决这个问题. 看到CSDN上各种需要积分下载的yolo.h5文件,实在看不下去了. 从 https://github.com/alla ...