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& ...
随机推荐
- 【2018ACM/ICPC网络赛】沈阳赛区
这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...
- 解决在python中进行CGI编程时无法响应的问题
问题:我期望的效果是,后端解析脚本后,将结果返回给我,而不是将代码返回给我或者是让我下载文件. 参考地址:https://blog.csdn.net/C_chuxin/article/details/ ...
- spring在普通类中获取session和request
在使用spring时,经常需要在普通类中获取session,request等对像.比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2 ...
- vue使用axios提交formdata格式的数据
参考: https://www.cnblogs.com/qwert1/p/8909455.html https://blog.csdn.net/qq_42984640/article/details/ ...
- Win32SDK应用程序
转自:https://blog.csdn.net/jxf_ioriyagami/article/details/1486626 1 说在前面 由于VC6及MFC的特点,我们许多人从标准C++学习 ...
- 前端常用的库和实用技术之JavaScript面向切面编程
Aspect Oriented Programming(AOP)面向切面编程是一个比较热门的话题. AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程 中的某个步骤或阶段,以 ...
- 【JZOJ2867】Contra
description 偶然间,chnlich 发现了他小时候玩过的一个游戏"魂斗罗",于是决定怀旧.但是这是一个奇怪的魂斗罗 MOD. 有 N 个关卡,初始有 Q 条命. 每通过 ...
- mysql 数据库 内容的增删改查
/*所有字段插入值*//*注意插入值数目要与字段值一致*/INSERT INTO student VALUES(1,'熊大','123','2019-10-18',1200);INSERT INTO ...
- 校园商铺-1开发准备-3 Eclipse与maven的联合配置
1. JDK安装地址: 2.maven安装地址: 3.maven配置 注意:settings.xml文件极容易出现格式错误 4.tomcat修改端口 我本地启动了其他服务,占用了8080端口,因此需要 ...
- NPM一Node包管理和分发工具
NPM 全称 Node Package Manager Node包管理和分发工具,可以把NPM理解为前端的Maven 我们通过npm可以很方便地下载js库,管理前端工程 最近版本的node.js已经集 ...