一切杀不死我的,都将使我强大!

1076 Forwards on Weibo(30 分)

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 triger, 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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 3000 ms

内存限制: 64 MB

代码长度限

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
const int maxn=1010;//最大为1000
using namespace std;
struct Node
{int id;//用户编号 int layer;//当前搜索的层次
};
vector<Node>map[maxn];//邻接表储存
bool vis[maxn] = {false} ;//标记是否入队 int bfs(int s,int L)
{int ans=0;//能够转发的最大次数 queue<Node>q;Node start;start.id = s;start.layer = 0;q.push(start);//入队 vis[start.id] = true;//标记已经入队 while (!q.empty()){Node top = q.front();q.pop();//取队首 int u = top.id;//队首用户编号 for (int i=0;i<map[u].size();i++)//遍历当前用户可以传递的其他用户 {Node temp = map[u][i];temp.layer = top.layer + 1;//层次 +1 if (vis[temp.id]==false && temp.layer<=L)//如果没有入队过,也就是没有被访问过,并且层次没有超过,则可以入队。 {q.push(temp);vis[temp.id] = true;//标记已经入队 ans++;//转发次数 +1 }}}return ans;
}int main()
{Node user;int n,L,ID,sum,T,s;cin>>n>>L;for (int i=1;i<=n;i++){user.id = i;//用户编号赋值 cin>>sum;for (int j=0;j<sum;j++){cin>>ID;map[ID].push_back(user);}}cin>>T;while (T--){memset(vis,false,sizeof(vis));cin>>s;int ANS = bfs(s,L);cout<<ANS<<endl;//输出答案就好啦 }return 0;
}

PAT A级 1076 Forwards on Weibo (超详细BFS做法)相关推荐

  1. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

  2. 【PAT甲级 - C++题解】1076 Forwards on Weibo

    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343

  3. 1076 Forwards on Weibo

    1. 这题说的是,微博上人们之间有关注和被关注的关系,如果一个人发博,他的追随者就可能转发,追随者的追随者又可能转发,以此类推.现在给定一个人,求其微博可能被转发的人数,但是注意有一个关注链长的上限, ...

  4. 1076 Forwards on Weibo——最后用menset函数

    方法一: #include<cstdio> #include<queue> #include<vector> #include<algorithm> # ...

  5. 1076 Forwards on Weibo (30 分)

    广度搜索BFS 注意理解题意,关注者与被关注者的关系. 同时L是表示,在L层以内的所有节点,包括L层. #include<bits/stdc++.h> using namespace st ...

  6. 1076. Forwards on Weibo 解析

    注意题意输入粉丝的时候,第i行是第i个关注了谁,而不是他的粉丝. 在遍历的时候也是只转发第一次,所以要一个isVis数组去标记一下. #include <iostream> #includ ...

  7. PAT_甲级_1076 Forwards on Weibo (30point(s)) (C++)【BFS/微博扩散】

    目录 1,题目描述 题目描述 输入 2,思路 BFS算法 3,AC代码 4,解题过程 第一搏 第二搏 1,题目描述 Sample Input: 7 3 3 2 3 4 0 2 5 6 2 3 1 2 ...

  8. 保姆级教程 CSS 知识点梳理大全,超详细!!!

    保姆级教程 CSS 知识点梳理大全,超详细!!! ✴️大家好,我是王同学,好久不见,趁着假期王同学把CSS 知识点梳理了一遍 ✴️如果对你有帮助就给我点个赞吧,这样我们就互不相欠了 ✴️星光不负赶路人 ...

  9. 从购买服务器到网站搭建成功保姆级教程~超详细

    ??从购买服务器到网站搭建成功保姆级教程~真的超详细,各位看官细品 ??前言 ??预备知识 ??什么是云服务器? ??什么是域名? ??什么是SSL证书? ??服务器选配 ??阿里云[官网链接](ht ...

最新文章

  1. SDWebImage
  2. 裸辞后,从Android转战Web前端的学习以及求职之路
  3. python去重复记录_python如何处理重复值数据?
  4. Linux内核将用Nftables替代iptables
  5. MNIST机器学习入门【学习笔记】
  6. 明天发布一个基于Silverlight的类Visio小型绘图工具项目。
  7. 国际象棋测试软件只能支持8核,CPU多线程测试:wPrime/国际象棋_AMD FX-8350_CPUCPU评测-中关村在线...
  8. mac整站下载工具wget
  9. kmz转换为dwg_CAD软件中的PDF插件如何实现转换DWG?一篇文章完整解释
  10. PS如何删除智能图层为可编辑状态
  11. simulink解微分方程
  12. c语言错误 cout不明确,C++ error: cout 不明确的符号
  13. 天气系统(环境系统)_SLG项目开发经验三
  14. 我的北漂在路上--------时不时的停下脚步思考
  15. adf输稿器是什么_ADF输稿器 多页复印法宝_多功能一体机_办公打印导购-中关村在线...
  16. 初夏小谈:结构体内存对齐详解
  17. 牛客 小米校招 计算题 单调栈 接雨水
  18. c7200-adventerprisek9.124-9.T.bin
  19. java Swing QQ登陆界面
  20. 问题 H: 逃出迷宫

热门文章

  1. 史上最大实体关系抽取数据集
  2. C++ 解决string转为char*中文乱码问题
  3. linux中spawn远程执行,Linux 远程执行命令,expect
  4. 嵌入式开发日记(8)——用python实现FIR滤波(未完待续)
  5. MATLAB求正态分布逆函数导数
  6. Object类型转Double类型
  7. 作业扣最少的分(要选好贪心目标)
  8. 思聪式吃完热狗,给微商秀:按键精灵实现微信群好友自动化添加!
  9. NASA的下一个十年(译)
  10. vant indexbar 做城市列表