news 2026/4/17 19:26:12

【PAT甲级真题】- Forwards on Weibo (30)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【PAT甲级真题】- Forwards on Weibo (30)

题目来源

Forwards on Weibo (30)

注意点

  • 下标从 1 开始

题目描述

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.

输入描述:

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

输出描述:

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.

输入例子:

7 3 3 2 3 4 0 2 5 6 2 3 1 2 3 4 1 4 1 5 2 2 6

输出例子:

4 5

思路简介

广搜+深度记录

链式前向星建图,然后记录深度广搜即可,在 PAT 当中出现很多次了
记录深度也很简单

  • num 记录当前层有几个元素
  • high 记录深度
  • 每次取出num--num=0时说明遍历完一层high++,此时队列的长度即下一层的大小,num=q.size()

这里注意一下,一个人转发了之后,他的所有关注者都会看见,只有其中没转发过的人才会转发,

遇到的问题

  1. 小标从 1 开始,vector存在下标越界的问题,要多开一个空间或者下标减 1
  2. Edge数组一开始开小了,应该开N*N

代码

/** * https://www.nowcoder.com/pat/5/problem/4306 * 广搜 */#include<bits/stdc++.h>usingnamespacestd;constintN=1000+7;structEdge{intto,next;Edge():to(-1),next(-1){}Edge(intto,intnext):to(to),next(next){}}e[N*N];inthead[N],cnt=0;voidadd(intu,intv){e[cnt]={v,head[u]};head[u]=cnt++;}intn,l;voidinput(){memset(head,-1,sizeof(head));cin>>n>>l;for(inti=0;i<n;++i){intm;cin>>m;for(intj=0;j<m;++j){intv;cin>>v;add(v,i+1);}}}voidsolve(){intk;cin>>k;queue<int>q;vector<int>vis(n+1);q.push(k);intres=0,high=0,num=1;vis[k]=1;while(!q.empty()&&high<l){intu=q.front();q.pop();num--;for(inti=head[u];i!=-1;i=e[i].next){if(!vis[e[i].to]){res++;vis[e[i].to]=1;q.push(e[i].to);}}if(!num){high++;num=q.size();}}cout<<res<<'\n';}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());input();intT=1;cin>>T;while(T--){solve();}return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 19:24:12

JiYuTrainer:极域电子教室破解终极方案,三分钟重获学习自由

JiYuTrainer&#xff1a;极域电子教室破解终极方案&#xff0c;三分钟重获学习自由 【免费下载链接】JiYuTrainer 极域电子教室防控制软件, StudenMain.exe 破解 项目地址: https://gitcode.com/gh_mirrors/ji/JiYuTrainer 你是否曾在课堂上遇到过这样的困境&#xff1f…

作者头像 李华
网站建设 2026/4/17 19:19:18

工具使用-Markdown入门

我们写技术文档的时候&#xff0c;经常用的就是**world**&#xff0c;然后可以转换为**pdf**。写CSDN和微信公众的时候&#xff0c;在线编辑器算是**富文本**的&#xff0c;使用htmlcss的**网页技术**实现&#xff0c;在各个编辑平台间复制的通用性不强&#xff0c;并且调格式很…

作者头像 李华
网站建设 2026/4/17 19:18:51

CarSim输入模块模式解析:REPLACE、ADD与MULTIPLY的实战应用

1. CarSim输入模块模式基础解析 第一次接触CarSim的输入模块时&#xff0c;我也被REPLACE、ADD和MULTIPLY这三种模式搞得一头雾水。经过几个项目的实战&#xff0c;我发现理解这三种模式的区别对仿真结果的影响至关重要。简单来说&#xff0c;这三种模式决定了从Simulink导入的…

作者头像 李华
网站建设 2026/4/17 19:16:29

【Python实战】用Friedman与Nemenyi检验绘制算法性能对比临界图(CD图)

1. 为什么需要算法性能对比临界图&#xff1f; 在机器学习领域&#xff0c;我们经常需要比较不同算法在相同数据集上的表现。你可能遇到过这样的困惑&#xff1a;算法A在准确率上比算法B高0.5%&#xff0c;这个差异真的有意义吗&#xff1f;还是说只是随机波动&#xff1f;这时…

作者头像 李华
网站建设 2026/4/17 19:15:06

如何高效配置阅读APP书源:专业用户的终极指南

如何高效配置阅读APP书源&#xff1a;专业用户的终极指南 【免费下载链接】Yuedu &#x1f4da;「阅读」自用书源分享 项目地址: https://gitcode.com/gh_mirrors/yu/Yuedu 还在为小说阅读APP频繁出现的"书源失效"提示而烦恼吗&#xff1f;是否厌倦了在不同小…

作者头像 李华