PAT甲级-11076 Forwards on Weibo (30 分)
发布日期:2022-02-10 08:10:58 浏览次数:8 分类:技术文章

本文共 926 字,大约阅读时间需要 3 分钟。

题目:
分析:BFS的使用,若用DFS会漏掉许多情况,比较麻烦。
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MAX 999999999int n,m,k,ds;struct Node{ vector
user; int layer;};int visi[10001];int cnt;Node node[10001];void bfs(int root){ queue
q; q.push(root); visi[root] = 1; while(!q.empty()) { int fro = q.front(); for(int i = 0; i < node[fro].user.size(); i++) if(!visi[node[fro].user[i]] && node[fro].layer < k){ node[node[fro].user[i]].layer = node[fro].layer + 1; q.push(node[fro].user[i]); visi[node[fro].user[i]] = 1; cnt ++; } q.pop(); }}int main(){ cin>>n>>k; for(int i = 1; i <= n; i++) { int p;cin>>p; for(int j = 1;j <= p ;j++) { int x; cin>>x; node[x].user.push_back(i); } } cin>>m; for(int i = 0; i < m; i++) { int x;cin>>x; cnt = 0; node[x].layer = 0; memset(visi, 0 , sizeof(visi)); bfs(x); cout<
<

转载地址:https://blog.csdn.net/qq_43567222/article/details/113599200 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT甲级-1074 Reversing Linked List (25 分)
下一篇:CentOs执行rz命令卡住-解决方法

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月11日 20时25分18秒