PAT a1076题解
来源:互联网 发布:算法设计与分析好难 编辑:程序博客网 时间:2024/05/30 21:58
#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;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;}
阅读全文
1 0
- PAT a1076题解
- PAT-A1076
- PAT A1076. Forwards on Weibo (30)
- pat-a1076. Forwards on Weibo (30)
- PAT 1001-1010 题解
- PAT 1011-1020 题解
- PAT 1021-1030 题解
- PAT 1031-1040 题解
- PAT 1041-1050 题解
- PAT 1051-1060 题解
- PAT(basic level)题解
- PAT乙级题解
- PAT题解目录
- PAT甲级题解目录
- PAT乙级题解目录
- PAT甲级1001题解
- PAT甲级1002题解
- PAT b1001-1005题解
- Effective Objective-C 2.0 Tips 总结 Chapter 3 & Chapter 4
- jsp中格式化long类型的时间戳
- javascript-事件绑定和普通事件
- 同户型同楼层的房子 为什么房价差那么多?
- RH124-第八节-系统服务控制
- PAT a1076题解
- 初识Django
- python+spaCy 进行简易自然语言处理
- PAT a1077题解
- 房价真的会降吗?五大原因告诉你真相
- 远程报错:不属于指定网络。请验证计算机名称和您尝试连接到域。
- PAT a1078题解
- vscode编辑器——编译调试小问题
- 游戏开发行业,前景如何?需要哪些技术?