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 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 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
#include <stdio.h>
#include <algorithm>
#include <set>
//#include <string.h>
#include <vector>
//#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
const int inf = ;
int n,k,m,l; bool vis[maxn];
struct node{
int id,lvl;
};
vector<node> adj[maxn];
int bfs(int root){
fill(vis,vis+maxn,false);
int num=;
queue<node> q;
node st;
st.id = root;
st.lvl = ;
q.push(st);
vis[root]=true;
while(!q.empty()){
node now = q.front();
q.pop();
int u=now.id;
for(int i=;i<adj[u].size();i++){
node next = adj[u][i];
next.lvl = now.lvl+;
if(vis[adj[u][i].id]==false && next.lvl<=l){
q.push(next);
vis[next.id]=true;
num++;
}
}
}
return num;
}
int main(){
scanf("%d %d",&n,&l);
for(int i=;i<=n;i++){
scanf("%d",&k);
node user;
user.id = i;
for(int j=;j<k;j++){
scanf("%d",&m);
adj[m].push_back(user);
}
}
scanf("%d",&k);
for(int j=;j<k;j++){
scanf("%d",&m);
int res=bfs(m);
printf("%d\n",res);
}
}
注意点:图的多次bfs,要注意初始化vis,和dfs不同的是bfs的vis是入队一次就要设为true,而不是访问了才设为true,否则会多次访问到。记录层数需要结构体,和A1106 Lowest Price In Supply Chain 不同,1106需要两个队列来完成一层层保存,因为他是二叉树,而图会存在环,两个队列也可以,不过这题没必要。
PAT A1076 Forwards on Weibo (30 分)——图的bfs的更多相关文章
- 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甲级】1076 Forwards on Weibo (30 分)
题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...
- PAT 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- 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[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
随机推荐
- Matlab arenstorf problem
% right-hand side of arenstorf problem function yDot = arenstorf(t,y) global mu muHat % unpack y u1 ...
- Ashampoo Driver Updater - 阿香婆驱动安装
Ashampoo Driver Updater 让系统更完美 – 永远有最新的驱动,出错或旧的驱动是每个电脑系统的恶梦.时不时,驱动会丢失或不可避免的过时.Ashampoo Driver Update ...
- kubectl 常用命令总结
# 查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间 kubectl get pod kubectl get pod -n kube # 查看 RC 和 service ...
- 【代码笔记】Web-ionic-index创建侧边栏
一,创建侧边栏. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- css iphonex适配
/* iphonex适配 */ @media only screen and (device-width:375px) and (-webkit-device-pixel-ratio: 3) { . ...
- html中用href 实现点击链接弹出文件下载对话框
浏览器支持能够打开的格式,他都会默认直接在线打开(比如word或图片),不支持的格式,他就会弹出下载提示.最好是做成.rar格式.xlsx的文件.浏览器自带下载功能. <body> < ...
- 根据需要扩展java中的ThreadPoolExecutor
经常被重写的三个方法 ThreadPoolExecutor是可扩展的,通过查看源码可以发现,它提供了几个可以在子类化中改写的方法:beforeExecute,afterExecute,terminat ...
- python自动化开发-6-常用模块-续1
json和pickle模块:用于序列化的模块. 序列化:我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serializatio ...
- Testlink Testlink在Windows下的安装
Testlink在Windows下的安装 by:授客 QQ:1033553122 测试环境 testlink-1.9.14 下载地址:http://pan.baidu.com/s/1pLrcu ...
- Android 获取网络类型
获取网络类型 public class IntenetUtil { //没有网络连接 public static final int NETWORN_NONE = 0; //wifi连接 public ...