pat a1076 微博转发人数_广搜

PAT甲级 1076 Forwards on Weibo 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 vi 阅读详情

一、问题描述

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

在微博中,每个用户都可能被若干个其他用户关注。而当该用户发布一条信息时,他的关注者就可以看到这条信息并选择是否转发它,且转发的信息也可以被转发者的关注者再次转发,但同一用户最多只转发该信息一次(信息的最初发布者不会转发该信息)。现在给出N个用户的关注情况(即他们各自关注了哪些用户)以及一个转发层数上限L,并给出最初发布消息的用户编号,求在转发层数上限内消息最多会被多少用户转发。

二、算法实现

#include<cstdio> 
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int maxv=1010;

struct Node
{
	int id;
	int layer;
};

vector<Node> adj[maxv];
bool inq[maxv]={false};

int BFS(int s,int L)
{
	int numForward=0;
	queue<Node> q;
	Node start;
	start.id=s;
	start.layer=0;
	q.push(start);
	inq[start.id]=true;
	while(!q.empty())
	{
		Node topNode=q.front();
		q.pop();
		int u=topNode.id;
		for(int i=0;i<adj[u].size();i++)
		{
			Node next=adj[u][i];
			next.layer=topNode.layer+1;
			//未加入过队列,且层次不超过L 
			if(inq[next.id]==false&&next.layer<=L)
			{
				q.push(next);
				inq[next.id]=true;
				numForward++;
			}			
		}
	}	
	return numForward;
}

int main()
{
	Node user;
	int n,L,numFollow,idFollow;
	scanf("%d%d",&n,&L);  //节点个数,层次上限 
	for(int i=1;i<=n;i++)
	{
		user.id=i;
		scanf("%d",&numFollow);
		for(int j=0;j<numFollow;j++)
		{
			scanf("%d",&idFollow);
			adj[idFollow].push_back(user);
		}
	}
	int numQuery,s;
	scanf("%d",&numQuery);
	for(int i=0;i<numQuery;i++)
	{
		memset(inq,false,sizeof(inq));
		scanf("%d",&s);
		int numForward=BFS(s,L);
		printf("%d\n",numForward);		
	}
	return 0;
}

 

通达信+大智慧数据同步存调DLL 数据同步存调DLL(通达信版+大智慧版) 阅读详情

相关推荐

英飞凌TC3xx实战:PWM中点触发ADC采集+DMA自动搬运完整配置流程(基于ILLD库)

本文详细介绍了在英飞凌TC3xx系列微控制器上使用ILLD库实现PWM中点触发ADC采集并通过DMA自动搬运数据的完整配置流程。该方案适用于电机控制、电源管理等需要高精度定时采集的场景,通过GTM生成PWM信号、ADC采集和DMA传输实现全硬件自动化流程,显著提升系统效率。

weixin_42536058的博客 535

PAT1076图的遍历(微博转发人数)

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...

qq_40512890的博客 310

Cesium实战:全球海洋动态水面与自定义区域波浪效果实现

本文详细介绍了在Cesium中实现全球海洋动态水面与自定义区域波浪效果的实战方法。通过解析核心概念Primitive与Geometry,并深入讲解Water材质的关键参数调节,指导开发者快速创建逼真的动态海洋。文章重点阐述了如何利用多边形坐标定义不规则图形区域,实现精准的局部水域波浪效果,并提供了性能优化与视觉提升的实用技巧。

weixin_29256771的博客 501

PAT_A1076#Forwards on Weibo

Source: PAT A1076Forwards on Weibo(30分) Description: 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. H...

weixin_34043301的博客 157

从一根USB线缆说起:深入浅出聊聊那些影响信号完整性和EMC性能的‘小细节’(双绞、屏蔽、360度搭接全解析)

本文深入探讨了USB线缆中影响信号完整性和EMC性能的关键细节,包括双绞线绞距、屏蔽层设计和360度搭接工艺。通过实验数据和实用技巧,揭示了这些‘小细节’如何显著影响高频信号质量和电磁兼容性,为工程师提供了从选购到系统设计的全面指南。

weixin_30824479的博客 440

3D Slicer的学习(二) 图像的分割

先对不同层级标注,然后点击Fill between slices -> Initialize -> Apply拖动层级颜色区域就会变成连续自适应的动态变化区域。如图,点击Level tracing,在图像上放置鼠标,会显示可能想要选中区域的轮廓,点击鼠标右键会将其显示为蓝色。然后点击Grow from seeds -> Initialize -> Apply,显示3D之后会自动扩展到其他位置的相同部位,从而展示整个部位。如图为copy之后,将黄色区域的颜色复制到原本的绿色区域。

tuweibazong的博客 6085

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

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 763

机器人控制入门:用SAC算法让机械臂学会抓取(PyBullet仿真教程)

本文详细介绍了如何使用SAC算法在PyBullet仿真环境中训练机械臂完成抓取任务。通过强化学习中的软Actor-Critic(SAC)算法,结合熵最大化机制和双Q网络架构,有效解决了机械臂控制中的探索难题。教程包含环境搭建、算法原理、状态空间设计、奖励函数优化等核心内容,并提供了实用的训练技巧和参数调优指南。

weixin_29091105的博客 364

PAT A1076 Forwards on Weibo

一、题目大意 PAT A1076 给出 n 个微博用户的关注情况以及一个转发层数上限 L,并给出最初发布消息的用户编号,求在转发层数上限内消息最多会被多少用户转发。 二、解题思路 很明显要用 BFS 模拟一层一层的转发过程,结点要存储结点编号以及层数。 这里使用邻接矩阵来存储有向图,某个结点的邻接表存储的是其粉丝的编号,处理数据时应当注意。参考代码如下。 三、参考代码 #include...

Armstrong的博客 239

别再瞎调参数了!用MATLAB fmincon优化傅里叶级数,为你的机器人设计“完美”激励轨迹

本文详细介绍了如何利用MATLAB的fmincon优化器设计机器人激励轨迹,通过优化傅里叶级数参数,解决传统试错法的低效问题。文章涵盖了目标函数设计、约束条件转化及优化算法选择,帮助工程师实现高精度的系统辨识和动力学参数优化。

weixin_42613501的博客 242

手把手教你用CH582的USB0/1双接口玩转CherryUSB协议栈(附完整代码)

本文深入解析CH582双USB接口硬件架构,详细讲解如何通过CherryUSB协议栈实现双接口协同工作,包括中断处理设计、端点资源分配和复合设备描述符配置。提供HID+CDC复合设备实战案例,并分享高级调试技巧与性能优化方法,帮助开发者充分利用CH582的USB0/1双接口潜力。

weixin_42525637的博客 264

[PAT A1076]Forwards on Weibo

[PAT A1076]Forwards on Weibo 题目描述 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 form...

欢迎来到我的编程小屋 655

强化学习原理及应用作业之动态规划算法【SYSU_2023SpringRL】

本次实践作业将在以下环境进行: 该环境由一个 6×6 网格组成,其中黄色圆圈为智能体出发点,黑色格子为无法通过的墙壁,若智能体向着墙壁方向移动,则会停留在原地,地图边界的移动同理。带有黄色边框的格子为终止状态,即若智能体行动至此状态则整个 episode 结束。红色和绿色表示当前该状态的奖励值,奖励越高则绿色越深,奖励越低则红色越深。状态空间:36(0-35 这 36 个整数,左下角为 0,向右 +1,向上 +6)动作空间:4(0-3 这 4 个整数分别代表左、右、上、下)奖励函数:普通格子 -0.1,到

zik的博客 1398

1076. Forwards on Weibo (30)

题目链接:http://www.patest.cn/contests/pat-a-practise/1076 题目: 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. H

Apie_CZX的专栏 2868

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT A 1076 Forwards on Weibo

题目要求计算微博用户在限定转发层数内的最大潜在转发量。给定用户之间的关注关系,当某用户发微博时,其直接和间接关注者(不超过L层)会转发,需统计符合条件的转发总数。 方法:使用广度优先搜索(BFS)遍历用户网络。从查询用户出发,逐层扩展,记录每个用户的转发层数,统计不超过L层的用户数量。每次查询前重置访问状态和层数记录。 输入:用户数N和转发层数L,接着N行每行描述用户的关注列表,最后是查询列表。 输出:每个查询用户的最大转发量。 示例: 输入中的用户2在最多3层转发时有4人转发,用户6有5人转发。代码通过B

whdehcy的博客 663

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

第五天PAT-A1076 Forwards on Weibo算法解析及详细注释

A1076 Description: 众所周知,微博是中国版的推特。用户在微博上可能有许多粉丝,也可能粉许多人。因此就构建起了一个社交网络。当一个用户发一条微博时,他的所有粉丝可以看到并转发他的微博,也可以被这些粉丝再次转发。现在给出一个社交网络,请计算对于一个用户而言,其潜在的最大可能转发量,假设只有N层间接粉丝会被计算。 Input: 每组测试一个测试样例; 对于每个测试,首行给出两个正数:用户数N<=1000,可被计算到的简介粉丝层数L<=6。假设所有用户被编号为1-N; 然后N行分别给

giggle66的博客 311

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

PAT-1076. Forwards on Weibo (30)

消失了好久。。我又回来了...

very_caiing 1639

PAT 甲级 1076 Forwards on Weibo(30)

PAT算法思路及笔记

ElvInR的博客 1256

【PAT A1076】Forwards on Weibo

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...

没有胡子的猫Asimov 211

PAT-ADVANCED1076——Forwards on Weibo

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805392092020736 题目描述: 题目翻译: 1076 微博转发 众所周知,微博是中国版的推特。微博上的一个用户可能有很多...

清风阁 332

PAT A 1076(甲级)

1076 Forwards on Weibo(30 分) 作者: CHEN, Yue 单位: 浙江大学 时间限制: 3000 ms 内存限制: 64 MB 代码长度限制: 16 KB Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may f...

CrazyOnes的博客 632

PAT 甲级 A1076 Forwards on Weibo

一个微博里面有n(7)个用户,每个用户可以相互关注,用户关注列表里面的人转发说说,用户可以接收得到(有向图,被关注(发说说)->关注者),给出转发层次L(3),转发的层次不能超过L,然后下列给出第i个人的关注列表(依据这个可以建造图),最后一行给出k个人,这k个人是编写说说的人,求在不超过转发层次的情况下,转发的人数有多少(给出连通块内的一个元素求连通块内元素个数)。①题目给出的是用户i的关注列表,关注表内的用户发信息,用户i是接收方,②每个连通块应该做到互不影响。因此是被关注->关注。...

m0_51711089的博客 255

1076 Forwards on Weibo

题目来源:PAT (Advanced Level) Practice 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

weixin_51032998的博客 149

[PAT-A 1076]Forwards on Weibo

考察图的遍历(BFS) 题目大意: 一个用户发布一条信息,他的关注这就可以看到这条信息并且选择是否转发,同一用户最多转发一次,信息发布者不会转发 给出n个用户的关注情况,以及他们关注了那些用户,以及一个转发上限L,应给出最初发布消息的用户编号,查询在转发层数内消息有多少会被转发 思路: 建立图,X关注了Y,建立Y->X的有向边,表示Y发布的消息可以被X转发 如样例输入中第二行 3234代表...

RegulusF的博客 211

【PAT】A1076 Forwards on Weibo【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 ...

elegant coder 991

PAT甲级刷题之路——1076

一道有向图论题 这道题之间在看晴神的《算法笔记》时做过,现在再做一遍吧,多熟练熟练 PAT甲级 1076 Forwards on Weibo (30分) 1076传送门 原题如下 这次直接用截图的方式附题,果然方便很多呢,也很清楚: 题目意思 ...

qq_43677396的博客 493
上一篇: 图的广搜
下一篇: Dijkstra求最短距离
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值