Pat(A) 1084. Broken Keyboard (20)
来源:互联网 发布:html 炫酷展示页源码 编辑:程序博客网 时间:2024/05/16 18:30
原题目:
原题链接:https://www.patest.cn/contests/pat-a-practise/1084
Broken Keyboard (20)
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 1084. Broken Keyboard (20)
- PAT-A 1084. Broken Keyboard (20)
- PAT-A-1084. Broken Keyboard (20)
- Pat(A) 1084. Broken Keyboard (20)
- PAT(A)1084. Broken Keyboard
- PAT-A 1084. Broken Keyboard
- pat 1084. Broken Keyboard (20)
- PAT 1084. Broken Keyboard (20)
- PAT 1084. Broken Keyboard (20)
- pat 1084. Broken Keyboard (20)
- PAT 1084. Broken Keyboard (20)
- 【PAT】1084. Broken Keyboard (20)
- 1084. Broken Keyboard (20) PAT
- PAT 1084. Broken Keyboard
- PAT 1084. Broken Keyboard
- 【PAT】1084. Broken Keyboard
- pat 1084. Broken Keyboard
- PAT Broken Keyboard (20)
- 红黑树和二叉树
- centos6.5 网卡/etc/sysconfig/network: No such file or directory
- 如何使用RestTemplate访问restful服务
- JavaSE 循环结构
- Python中*和**的使用
- Pat(A) 1084. Broken Keyboard (20)
- JSON.parse问题(转)
- Atitit.每周计划日程表 流程表 v9 r829.docx
- Atitit 常见每日流程日程日常工作.docx v8 verampmimp 签到amy 天气情况检查amy 晨会,每天或者隔天am 每日计划(项目计划,日计划等。am
- 【LeetCode】645. Set Mismatch
- Struts2工作原理与Struts2工作流程
- ubuntu 配置java环境变量 修改/etc/environment 导致,登录界面进不去了
- Atitit.月度计划日程表 每月流程表v5
- HDU 3046 Pleasant sheep and big big wolf(SAP,Dinic模板)