PAT - 1076 Forwards on Weibo (30 分) 图的BFS遍历

PAT甲级真题 1076 Forwards on Weibo (30分) C++实现(图的广度优先遍历,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... 阅读详情

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.

读这一句话读了好久才读懂,i是从1开始递增的,M[i]代表第i个数follow的people, user_list[i]则代表是M[i]个用户,user[i] follow的用户。这里给出了三个数组,M[]是总数量数组,user_list[]是follower数组,存储着user follow的每一个用户,user[i]代表某一个用户。

其实意思很简单,先给出第i个数follow的用户总数量,再分别列出这些用户。注意,是M[i] follow的用户。

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

这道题基本上是自己直接写出来就AC的,非常的激动!!!! 

  1. BFS与DFS的区别: BFS是在队列里面循环,DFS是一层一层进去递归。
  2. Node里面加一个参数,并在每一层入队的时候直接更新。
  3. 由于是有向图,输入的时候指定方向为G[tmp][i]代表,tmp更新的时候,i会收到通知,这样比较直观(与follow的方向相反,是i follow tmp)
  4. 在每次BFS开始前,要重置inq数组和totalcnt。
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

const int maxn = 1010;
int G[maxn][maxn] = {0};
bool inq[maxn] = {false};
int N, K;
int totalcnt = 0;
struct Node{
    int node, layer;
    Node(int _node, int _layer): node(_node), layer(_layer){}
};

void BFS(Node node){
    queue<Node> q;
    q.push(node);
    inq[node.node] = true;
    while(!q.empty()){
        int front = q.front().node;
        int curlayer = q.front().layer;
        if(curlayer >= K)
            break;
        q.pop();
        for(int i = 1 ;i <= N; i++){
            if(inq[i] == false && G[front][i] == 1 ){
                q.push(Node(i, curlayer + 1));
                inq[i] = true;
                totalcnt++;
            }
        }
    }
}


int main(){
    cin>>N>>K;
    for(int i = 1; i <= N; i++){
        int num;
        cin>>num;
        for(int j = 0; j < num;j++){
            int tmp;
            cin>>tmp;
            G[tmp][i] = 1;
        }
    }
    int outNum;
    cin>>outNum;
    for(int i = 0; i < outNum; i++)
    {
        int tmp;
        cin>>tmp;
        
       // for(int i = 1; i <= N;i++) //注意是数字序号是从1开始计数的!!! ‘AC的一瞬间,全身激动!!!太爽了’
        //    inq[i] = false;
        memset(inq, 0, sizeof(inq));
        totalcnt = 0;
        
        BFS(Node(tmp, 0));
        cout<<totalcnt<<endl;
        }
    return 0;
}

 

Mac M系列芯片用户必看:UTM虚拟机跑麒麟V10安装达梦DM8全流程避坑指南 本文为Mac M系列芯片用户提供了一份详尽的UTM虚拟机安装麒麟V10并部署达梦数据库DM8的实战指南。文章深入分析了在ARM架构下选择UTM而非其他虚拟化方案的原因,并详细拆解了从系统安装、SPICE工具配置、麒麟V10深度调优到DM8安装与避坑的全流程,重点解决了ARM环境下的兼容性与配置难题,帮助开发者高效搭建国产化开发测试环境。 阅读详情

相关推荐

ubuntu 18.04 + Kimera slam 安装与demo测试

环境:opencv 3.4.12,其他的都是按照文档下载的。 首先进行组件的安装 一、Kimera-VIO 安装参考文档:https://github.com/MIT-SPARK/Kimera-VIO/blob/master/docs/kimera_vio_install.md 按照文档装就好,在安装过程中碰到如下问题: 0、make 过程很卡,不要用所有的核 1、cmake过程中卡下载 ORBvoc.zip ,手动下载这个文件,解压,将里面的3个文件,放到/Kimera-VIO/vocabu.

dna葡萄糖的博客 3630

【PAT】A1076 Forwards on Weibo (30分)(图的遍历bfs)

1076 Forwards on Weibo (30分) 题目链接 法一:邻接矩阵法 #include<iostream> #include<queue> using namespace std; const int maxn = 1010; int Graph[maxn][maxn] = {0}; int n, l; //邻接矩阵版本 struct Node{...

醉酒诗人的博客 1035

python:numpy分享(保姆级教程)

创建数组:array和ndarray、rand()、randn()、arange()、randint()、uniform()astype()基础函数:ceil()、floor()、rint()、argmax()、argmin()、cumsum()、cumprod()numpy 是数据分析必不可少的第三方库,具有高性能的特点,多用于做数组分析。内置函数:ceil()、floor()、rint()主要用于数组计算,支持多维数组。array和ndarray。比较函数:all、any。去重函数:unique。

sulin123123的博客 1733

PAT A1076 Forwards on Weibo (30分)【图的BFS遍历】

目录代码反思参考文档 今天也是为了cc,努力奋斗的一天ヾ(≧▽≦*)o 代码 //注意数组下标是从1开始的 #include<stdio.h> #include<vector> #include<queue> using namespace std; //基本单位,里面包含节点值和层数 struct Node{ int v; int layer;...

yc_cy1999的博客 379

从BUCK-BOOST到反激:一个‘拆电感’的视角,轻松理解多路输出交叉调整

本文从BUCK-BOOST电路到反激变换器的演变入手,通过‘拆解电感’的独特视角解析多路输出交叉调整机制。深入探讨了反激电路的能量暂存方式、传递路径变化及电压关系,并结合具体参数对比揭示其特性。针对多路输出中的电压平衡问题,提供了绕组排布优化、元器件选型等实战技巧,帮助工程师有效提升电源系统的稳定性与效率。

weixin_33738578的博客 437

AutoCAD2014与ObjectARX开发环境搭建实战指南

本文提供了一份详细的AutoCAD2014与ObjectARX开发环境搭建实战指南。内容涵盖从AutoCAD2014、Visual Studio 2010及ObjectARX 2014 SDK的版本匹配与环境准备,到Wizard安装、编码问题解决、Visual Studio配置,并最终通过一个完整的绘图命令开发示例,手把手引导开发者创建并加载第一个可运行的ObjectARX插件,为后续深入开发奠定坚实基础。

weixin_28475533的博客 288

PAT (Advanced Level) Practice 1076 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 ...

一只小羊 233

QQ的TEA填充算法C#实现 By Red_angelX

注:本人只是从LUMA QQ的 Source Code里面把相应Java语言翻译成C#,纯技术研究,并没有对TC产品做任何逆向分析,不承担任何法律责任!转载请保持本文完整性网上有c/c++,vb,delphi,java,perl各种版本的tea填充算法,唯独没有C#的,这让我这种狂热喜爱C#的人如何承受,于是,花3天时间看了各种代码经历无数失败的挫折终于用C#完成了该填充算法,废话不多说,直接

Red_angelX的专栏 1万+

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 may have many followers, and may follow many other users as well. Hence a social network is formed wi...

qq_29762941的博客 273

STM32驱动AS5600磁性旋转位置传感器模块

文章目录简介引脚定义I2C接口写数据读数据从指定的地址开始读数据驱动程序cubeMX配置驱动程序`bsp_as5600.h``bsp_as5600.c`测试程序参考资料 简介 AS5600 是一款易于编程的磁性旋转位置传感器,具有高分辨率 12 位模拟或 PWM 输出。 这种非接触式系统测量径向磁化轴上磁铁的绝对角度。 该 AS5600 专为非接触式电位计应用而设计,其坚固的设计消除了任何同质外部杂散磁场的影响。 引脚定义 I2C接口 7位设备地址为0x36(二进制表示为0110110) 支持标准模式和

ApexYuan 1万+

第二节:Vben Admin 登录逻辑梳理和对接后端准备

第一节,我们已经配置了前端环境,运行起来了我们的Vben Admin的项目。本节内容,我们先熟悉下项目的目录和文件,然后开始准备对接后端程序路由在 src/router页面在 sr/views本节前端登录逻辑梳理完成了,后端只要在/api/login 接口编写返回逻辑,前后段就可以打通了,具体详情我们下一节介绍。

cuiwin的专栏 5084

【PAT甲级】1076 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. Wh...

feng_zhiyu的博客 582

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

PAT 甲级 1076 Forwards on Weibo(30)

PAT算法思路及笔记

ElvInR的博客 1256

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

PAT甲级 1076 Forwards on Weibo(30)

PAT甲级真题1076 Forwards on Weibo详解附AC代码

2301_79601095的博客 1216

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

[C++] PAT 1076 Forwards on Weibo (30分)

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 题解: 该题是图的BFS, 1.题目中有层数限制,则将访问标志数组vis[],用访问层数标志结点是否被访问,注意每次进行BFS前将其初始化 #include <iostream> #include <queue> using namespace std; const int MAXV = 1010; int n,G.

快乐de约定的博客 2027

1076. Forwards on Weibo (30)-PAT甲级真题(图的遍历bfs)

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 wit

柳婼 の blog 2098

【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 Forwards on Weibo (30 分)题解

\quad这个一个标准的图BFS的过程,从图中给定的一个顶点u开始,记录与u距离(层数)不超过L的顶点个数,用队列模拟BFS即可。 #include <iostream> #include <vector> #include <queue> #include <cstring> using namespace std; const int max...

qq_40438165的博客 1570

PAT Advanced Level 1076. Forwards on Weibo (30)

【来源】 1076. Forwards on Weibo (30) 【分析】 本题给出微博用户的关注情况,在假定某一用户的粉丝看到某条微博一定会转发的情况下,求可能被转发的次数。 抽象一下就是一个限定了层数的广度优先搜索(BFS)。 基本的数据结构是以vector为元素类型的一个vector。该vector的index和用户id相关,index所对应的元素为此id的粉丝们。 给定发

harmony的专栏 1629

PAT 甲级 1076  Forwards on Weibo

1076 Forwards on Weibo (30 point(s)) 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 fo...

漫浸天空的雨色的备忘录~ 622

PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)

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

蔡军帅 507

【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

PAT1076 Forwards on Weibo

原文:我的个人博客 原题链接 1076 Forwards on Weibo 考点   图的遍历,广度优先遍历 思路   简单的广搜就可以解决。在广搜的过程中记录所在的层次。 代码 #include <bits/stdc++.h> using namespace std; vector<int> net[10010]; bool visit[10010]; ...

我是没有梦想的小白 217

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-ADVANCED1076——Forwards on Weibo

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

清风阁 332

PAT甲级 —— 1076 Forwards on Weibo (30分)

题目链接: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..

佚失的诗篇 238
上一篇: PAT - 1034 Head of a Gang (30 分) 图的DFS
下一篇: PAT甲级 - 1003 Emergency (25 分) Dijstra和BF算法
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值