Pat(A) 1076. Forwards on Weibo (30)
来源:互联网 发布:淘宝借钱逾期 编辑:程序博客网 时间:2024/06/01 07:57
原题目:
原题链接:https://www.patest.cn/contests/pat-a-practise/1076
1076. Forwards on Weibo (30)
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 6Sample Output:
4
5
题目大意
给定微博中的人员关注信息,求一个用户发出一条信息后,在直接或间接(L层内)看到的人的个数,最多L层。
输入第一行为N(用户数)、L(层数)。
后N行表示第i个用户所关注的用户id。
再一行第一个数M为求的人的个数,后M个数表示用户id。
输出为M个数,表示L层内能看到的用户数。
解题报告
用一个图G表示关注情况。
对每个求的用户bfs搜索,搜索L层。用visited表示访问与否。
注意事项:
- 每次bfs,visited都要进行初始化。
代码
#include "iostream"#include "queue"#include "vector"using namespace std;vector<bool> visited;queue<int> que;int G[1000 + 5][1000 + 5];int N,L;void init(){ cin>>N>>L; fill(G[0],G[0]+ 1005*1005,0); int m,i,j; int k; for(i = 1; i <= N; i ++){ cin>>m; for(j = 1; j <= m; j++){ cin>>k; G[i][k] = 1; } }}void bfs(int root){ visited.clear(); visited.resize(N+1,false); while(!que.empty()){ que.pop(); } int level = 1; int num=1,nextnum = 0,cou = 0,ans=0; que.push(root); visited[root] = true; int a; int i; while(!que.empty()){ if(level > L) break; cou ++; a = que.front(); que.pop(); for(i = 1; i <= N; i++){ if(G[i][a] && !visited[i]){ visited[i] = true; ans ++; que.push(i); nextnum ++; } } if(cou == num){ num = nextnum; nextnum = 0; cou = 0; level ++; } } cout<<ans<<endl;}void print_G(){ int i,j; for(i = 1;i <= N; i++) cout<<"\t"<<i; cout<<endl; for(i = 1;i <= N; i++) cout<<"-------\t"; cout<<endl; for(i = 1; i <= N; i++){ cout<<i<<"|"; for(j=1; j <= N; j++) cout<<"\t"<<G[i][j]; cout<<endl; }}int main(){ init(); //print_G(); int k,root; cin>>k; for(int i = 0; i < k; i++){ cin>>root; bfs(root); } //system("pause");}
- PAT A 1076. Forwards on Weibo (30)
- PAT(A) - 1076. Forwards on Weibo (30)
- PAT-A-1076. Forwards on Weibo (30)
- Pat(A) 1076. Forwards on Weibo (30)
- PAT-A-1076. Forwards on Weibo (30)-2
- PAT (Advanced) 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
- 1076. Forwards on Weibo (30) PAT 甲级
- 【PAT甲级】1076. Forwards on Weibo (30)
- PAT 1076. Forwards on Weibo (30)
- PAT 甲级 1076. Forwards on Weibo (30)
- PAT 1076. Forwards on Weibo
- spring boot入门(三) 热部署
- ubuntu16.04安装jdk以及详细jdk环境配置教程,亲测没毛病。
- Spark学习笔记(25)Spark Streaming的JobGenerator源码图解
- log4j怎样配置才能打印出执行语句所在的类名及行号
- linux 编译Jrtplib
- Pat(A) 1076. Forwards on Weibo (30)
- Spark学习笔记(26)在DStream的Action操作之外也可能产生Job操作
- 坚持了半个月刷CCF的算法题(第十四天)
- hbase扫描优化-扫描缓存-cacheing-timetout-retries-batch
- Spark学习笔记(27)详解Spark Streaming的运行日志
- 07 java的object类和内部类, 匿名类和匿名对象
- java中反射的机制
- Spark学习笔记(28)深入理解Spark 2.x中的Structured Streaming
- 嗯哼~2017美团点评前端面试(一年工作经验)