PAT--1076. Forwards on Weibo (30)(图的bfs遍历)
来源:互联网 发布:知乎经典好贴 编辑:程序博客网 时间:2024/05/18 09:06
Description
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 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.
Output Specification:
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.
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遍历,求L层内的follower个数。
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>using namespace std;const int maxn = 1000 + 10;vector<int> G[maxn];int layer[maxn]; // 记录节点的层次int N, L, k;void bfs(int id){ queue<int> Q; Q.push(id); int ans = 0; fill(layer, layer + maxn, -1); layer[id] = 0; while(!Q.empty()){ int u = Q.front(); Q.pop(); if(layer[u] >= L) break; for(int i = 0; i < G[u].size(); ++i){ int v = G[u][i]; if(layer[v] == -1){ layer[v] = layer[u] + 1; Q.push(v); ans++; } } } cout << ans << endl;}int main(){#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endif // ONLINE_JUDGE cin >> N >> L; for(int i = 1; i <= N; ++i){ cin >> k; for(int j = 0; j < k; ++j){ int v; cin >> v; G[v].push_back(i); } } cin >> k; for(int i = 0; i < k; ++i){ int id; cin >> id; bfs(id); } return 0;}
- PAT--1076. Forwards on Weibo (30)(图的bfs遍历)
- 1076. Forwards on Weibo (30)-PAT甲级真题(图的遍历bfs)
- PAT 1076. Forwards on Weibo (BFS + 剪枝)
- 1076. Forwards on Weibo (30) -- BFS
- PAT (Advanced) 1076. Forwards on Weibo (30)
- PAT A 1076. Forwards on Weibo (30)
- PAT-1076. Forwards on Weibo (30)
- pat 1076. Forwards on Weibo (30)
- PAT 1076. Forwards on Weibo (30)
- PAT 1076. Forwards on Weibo (30)
- PAT 1076. Forwards on Weibo (30)
- 【PAT】 1076. Forwards on Weibo (30)
- 1076. Forwards on Weibo (30) PAT
- PAT(A) - 1076. Forwards on Weibo (30)
- 1076. Forwards on Weibo (30) PAT 甲级
- PAT-A-1076. Forwards on Weibo (30)
- 【PAT甲级】1076. Forwards on Weibo (30)
- Pat(A) 1076. Forwards on Weibo (30)
- 第4章基于PSO并融合海事规则的已知动态路径规划方法
- 商业结算中的本票、支票、汇票
- 第十六周 项目1--验证算法(3)--冒泡排序
- C++实现二叉搜索树(二叉排序树)模板类
- STM32学习笔记7——浮点数四舍五入
- PAT--1076. Forwards on Weibo (30)(图的bfs遍历)
- Java 抽象类与oop三大特征
- dubbo 入门教程 基于zookeeper
- 初入开发组(4)--一个程序员的成长史(19)
- 机器学习和数据挖掘推荐书单及简介
- Codeforces Round #378 (Div. 2) D 贪心
- 模仿了一个夸张的商品倒计时效果,设计的精简当然也很丑,只是为了检测自己说明问题
- flume学习(四):Flume Channel Selectors使用
- 小白学《神经网络与深度学习》笔记之四-深度学习的常用方法(2)