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 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 (≤), the number of users; and L (≤), 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]
(≤) 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.
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
使用广度优先遍历BFS
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
bool people[][];
int N, L, K;
int BFS(int v)//广度优先搜索
{
int num = ;
vector<int>level(N + , );
vector<bool>visit(N + , false);
queue<int>q;
q.push(v);
visit[v] = true;
while (!q.empty())
{
v = q.front();
q.pop();
for (int i = ; i <= N; ++i)
{
if (visit[i] == false && people[v][i] == true && level[v] < L)
{
num++;
visit[i] = true;
level[i] = level[v] + ;
q.push(i);
}
}
}
return num;
} int main()
{
cin >> N >> L;
fill(people[], people[] + * , false);
for (int i = ; i <= N; ++i)
{
int a, m;
cin >> m;
for (int j = ; j <= m; ++j)
{
cin >> a;
people[a][i] = true;//请记住,存的是a的粉丝
}
}
cin >> K;
vector<int>test(K);
for (int i = ; i < K; ++i)
{
int a;
cin >> a;
cout << BFS(a) << endl;//使用广度优先搜索,即使用层序遍历
}
return ;
}
使用深度遍历DFS
#include <iostream>
#include <vector>
using namespace std;
int N, L, K;
vector<int> graph[];//图
bool visit[];//visit表示是否已被访问,person用于最后统计
int layer[];//储存每个结点被遍历到时的层数
void DFS(int v, int level)
{//深度优先遍历
visit[v] = true;//当前结点置为已访问
layer[v] = level;//更新被遍历时所处层数
if (level != L)//还没有遍历到层数上限
for (int i : graph[v])//遍历当前结点能够到达的结点
if (!visit[i] || layer[i] > level + )//这个节点以前从未访问过或者这个节点当前被访问时的层数<layer数组中对应的层数
DFS(i, level + );//继续深度优先遍历
}
int main()
{
scanf("%d%d", &N, &L);
for (int i = ; i <= N; ++i)
{
int num, a;
scanf("%d", &num);
while (num--)
{
scanf("%d", &a);
graph[a].push_back(i);
}
}
scanf("%d", &K);
while (K--)
{
fill(visit + , visit + N + , false);
fill(layer + , layer + N + , -);
int a, num = ;
scanf("%d", &a);
DFS(a, );
for (int i = ; i < N + ; ++i)//遍历layer数组,元素>0的即为符合条件的人,进行递增
num += layer[i] > ? : ;
printf("%d\n", num);//输出
}
return ;
}
PAT甲级——A1076 Forwards on Weibo的更多相关文章
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 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 m ...
- PAT A1076 Forwards on Weibo (30 分)——图的bfs
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- PAT甲级 1124. Raffle for Weibo Followers (20)
1124. Raffle for Weibo Followers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT甲级——A1124 Raffle for Weibo Followers
John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...
- A1076. Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
- PAT_A1076#Forwards on Weibo
Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- 移动端dialog组件
移动端dialog组件 dialogView是满足移动端下,用户自定义的dialog组件,API可扩展性强,使用便捷.现版本是基于jquery库编写的,在使用之前需要引入jquery库或者Zepto库 ...
- pointer && reference
关注点在于区别两者之间的不同. 我们可以从两者使用的场景进行区分: 1, 是否需要存在null的情况: YES-pointer NO-reference 如果确定不会存在null的情况,那么使用ref ...
- ARM GNU 常用汇编伪指令介绍
abort .abort: 停止汇编 .align absexpr1, absexpr2: 以某种对齐方式,在未使用的存储区域填充值. 第一个值表示对齐方式,4, 8,16 或 32. 第 二个表 ...
- 威布尔weibull distribution
data = wblrnd(0.5,0.8,100,1); 生成威布尔随机函数,尺寸参数为0.5,形状参数为0.8,生成数列100行,一列: parmhat = wblfit(data) 对data的 ...
- Google Projectsheet Planning 插件的WBS
生成 WBS的序列號 在 Sldebar中的 "WBS" 按鈕: "< WBS" 取消下級目錄 "WBS >" 生成下級目錄 G ...
- leetcode-47-全排列②
题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = ...
- C/C++ ShowWindow()
{ ShowWindow(HWND,0);//不显示窗口 }
- kafka-node+socket.io 测试配置
1.安装需要插件 npm install express npm install socket.io npm install kafka-node 2.kafkatest.js文件 var exp ...
- python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载——CentOS-02安装——CentOS-03配置操作
http://www.xitongzhijia.net/soft/24315.html http://www.downxia.com/downinfo/4574.html .
- react diff 极简版
为什么react这么快呢 ? 因为react用了虚拟DOM: 但是每次虚拟DOM转真实DOM不也是很浪费性能吗 ? nice,所以关键点在Diff算法这里,去对比新旧DOM树,而后通过补丁去更新到真实 ...