方法一:

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=1005;
struct Node{int id; int layer;
};
bool vis[maxn]={false};
vector<Node> m[maxn];
int n,l,no,foll=0;void bfs(int i){//bool vis[maxn]={false}; //或者在主函数的最后输入循环中加上:  menset(vis,false,sizeof(vis)); queue<Node> q;Node pre;pre.id=i;pre.layer=0;q.push(pre);vis[pre.id]=true;while(!q.empty()){Node now=q.front();q.pop();int u=now.id;for(int j=0;j<m[u].size();j++){Node next=m[u][j];next.layer=now.layer+1;if(vis[next.id]==false&&next.layer<=l){foll++;q.push(next);vis[next.id]=true;}}}
}//void bfstrave(){//  for(int i=1;i<=n;i++){//      if(vis[i]==false){//          bfs(i);
//      }
//  }
//}int main(){Node user;int k;scanf("%d%d",&n,&l);for(int i=1;i<=n;i++){user.id=i;scanf("%d",&k);for(int j=0;j<k;j++){int id;scanf("%d",&id);m[id].push_back(user);}}scanf("%d",&k);for(int i=0;i<k;i++){memset(vis,false,sizeof(vis));scanf("%d",&no);bfs(no);printf("%d\n",foll);foll=0;}return 0;
}

方法二

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1005;
struct Node{int id; int layer;
};vector<Node> m[maxn];
int n,l,no,foll=0;void bfs(int i){bool vis[maxn]={false}; //或者在主函数的最后输入循环中加上:  memset(vis,false,sizeof(vis)); queue<Node> q;Node pre;pre.id=i;pre.layer=0;q.push(pre);vis[pre.id]=true;while(!q.empty()){Node now=q.front();q.pop();int u=now.id;for(int j=0;j<m[u].size();j++){Node next=m[u][j];next.layer=now.layer+1;if(vis[next.id]==false&&next.layer<=l){foll++;q.push(next);vis[next.id]=true;}}}
}//void bfstrave(){//  for(int i=1;i<=n;i++){//      if(vis[i]==false){//          bfs(i);
//      }
//  }
//}int main(){Node user;int k;scanf("%d%d",&n,&l);for(int i=1;i<=n;i++){user.id=i;scanf("%d",&k);for(int j=0;j<k;j++){int id;scanf("%d",&id);m[id].push_back(user);}}scanf("%d",&k);for(int i=0;i<k;i++){scanf("%d",&no);bfs(no);printf("%d\n",foll);foll=0;}return 0;
}

1076 Forwards on Weibo——最后用menset函数相关推荐

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

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

  2. PAT A级 1076 Forwards on Weibo (超详细BFS做法)

    一切杀不死我的,都将使我强大! 1076 Forwards on Weibo(30 分) Weibo is known as the Chinese version of Twitter. One u ...

  3. 1076 Forwards on Weibo

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

  4. 1076 Forwards on Weibo (30 分)

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

  5. 1076. Forwards on Weibo 解析

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

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

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

  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. 普朗特迈耶稀疏波_埃里克·迈耶的访谈

    普朗特迈耶稀疏波 I've always wanted to interview Eric Meyer. His early CSS books are a big reason this blog ...

  9. PAT (Advanced Level) Practice 题解代码 - II (1051-1100)

    PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...

最新文章

  1. 按下回车表示确定提交
  2. 工作流引擎 Activiti 万字详细进阶
  3. hust sci列表
  4. windows7内存诊断工具有用吗_Win7怎么使用自带工具进行内存检测?
  5. Serverless 在大规模数据处理的实践
  6. 源 arm_arm和X86处理器性能简单测试
  7. 使用envi对图像进行对比度拉伸并保存
  8. c语言怎样设计程序界面,「分享」C语言如何编写图形界面
  9. IMO船舶材料防火安全规范
  10. Ubuntu下SMPlayer播放器安装配置以及常用快捷键记录
  11. go语言中文乱码gbk转UTF8
  12. 自制简易浏览器(Python)
  13. 传说中的100句子记忆7000单词(51-100句)
  14. 带你深入浅出学STM32
  15. stlink故障修复
  16. java学习思维导图
  17. 宠物医院称可给怀孕宠物剖腹产 医生一对一护理
  18. 46个不得不知的生活小常识
  19. gn编译webrtc介绍
  20. 小学生也能看懂的海伦公式推导

热门文章

  1. 摄像头识别黑烟加框处理(测试)
  2. Truncated Power Method for Sparse Eigenvalue Problems
  3. 树莓派+阿里云轻松智能家居DIY-app
  4. 关于“显示器驱动程序已停止响应并且已成功恢复”的解决方案
  5. 基于sqlite的android数据库编程,Android编程之SQLite数据库操作方法详解
  6. CAD批量打图精灵更新至9.5.3,支持使用Adobe PDF虚拟打印机以创建高质量的PDF文件。
  7. 没有公网IP,3个路由器就能解决连锁视频监控
  8. 致初级软件开发人员并自勉
  9. MySQL DBA的修炼与未来(参考篇)
  10. CSS3解析抖音 LOGO制作