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 (<=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
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
int data, layer;
}node;
vector<node> G[];
int visit[];
int N, L, K, Mi;
int bfs(int vt){
int cnt = -;
node first = {vt, };
visit[vt] = ;
queue<node> Q;
Q.push(first);
while(Q.empty() == false){
node temp = Q.front();
Q.pop();
cnt++;
int len = G[temp.data].size();
if(temp.layer < L){
for(int i = ; i < len; i++){
if(visit[G[temp.data][i].data] == ){
visit[G[temp.data][i].data] = ;
G[temp.data][i].layer = temp.layer + ;
Q.push(G[temp.data][i]);
}
}
}
}
return cnt;
}
int main(){
scanf("%d%d", &N, &L);
for(int i = ; i <= N; i++){
scanf("%d", &Mi);
for(int j = ; j < Mi; j++){
int temp;
scanf("%d", &temp);
node nd = {i, };
G[temp].push_back(nd);
}
}
scanf("%d", &K);
int vt, ans;
for(int i = ; i < K; i++){
for(int j = ; j < ; j++)
visit[j] = ;
scanf("%d", &vt);
ans = bfs(vt);
printf("%d\n", ans);
}
cin >> N;
return ;
}
总结:
1、题意:求据距离点A的距离不超过L的节点的个数(A本身不计入)。
2、bfs的visit数组记录的是是否曾加入过队列,而非是否被访问。
3、本题是有向图,最好用邻接表存储。此外,题目给出的数据是A关注的人的列表,而微博消息应该是从关注列表里的人到A的方向,注意不要弄错图的边。
4、由于dfs的原则是访问过的节点就不再访问,所以求得的节点有可能不是最短的转发距离,从而有可能漏掉一些情况。如图,当使用深搜,L = 2。从A开始,A->B->C,距离达到了2开始回溯到A,而由于C已被visit标记,所以无法再次被访问,D距离A也是2但却被遗漏。
A1076. Forwards on Weibo的更多相关文章
- 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甲级——A1076 Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- PAT_A1076#Forwards on Weibo
Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...
- 1076 Forwards on Weibo (30 分)
1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...
- PAT 1076 Forwards on Weibo[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- 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 ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
随机推荐
- 数组中元素累加 reduce
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- Mysql如何快速插入100万条记录?
1.java程序拼接insert带多个value,使一次提交多个值. 2.插入数据之前先删除索引(注意主键不能删除),然后插入数据,最后重建索引 3.可以设置手动commit,用来提高效率 4.使用批 ...
- PyCharm的使用
1.pycharm的下载和安装 首先,去jetbrains官网https://www.jetbrains.com/pycharm/download/#section=windows 中下载最新版的pr ...
- 刪除nodejs
https://www.cnblogs.com/fighxp/p/7410235.html https://www.cnblogs.com/fighxp/p/7411608.html
- Create an Azure SQL database in the Azure portal
Create a SQL database An Azure SQL database is created with a defined set of compute and storage res ...
- EOS搭建
http://mp.weixin.qq.com/s/RcDFCFCWpMIIAshJMqkxxw ,地址实效,请关注公众号 一下引用,只是技术记录交流 目前来讲,学习任何EOS相关技术的资料都来自ht ...
- Nginx upstream变量
log_format varups '$upstream_addr $upstream_connect_time $upstream_header_time $upstream_response_ti ...
- Android 控件绑定封裝
最近刚开始写android 随便记录一下,以后还会修改 绑定ListView,Spinner 先创建绑定项: BaseItem public class BaseItem { public BaseI ...
- 大学jsp实验5request,response
1.request对象的使用 (1)编写一个包含有表单的JSP页面form.jsp,其中包含可以输入姓名和出生地的文本框,提交表单后在另一个页面中显示用户提交的姓名和出生地.请写出相应代码: form ...
- Docker容器安装
使用脚本安装 Docker 1.使用 sudo 或 root 权限登录 Centos. 2.确保 yum 包更新到最新. $ sudo yum update 3.执行 Docker 安装脚本. $ c ...