【PAT】【Advanced Level】1076. Forwards on Weibo (30)
来源:互联网 发布:路由器绑定mac 编辑:程序博客网 时间:2024/06/07 07:38
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 33 2 3 402 5 62 3 12 3 41 41 52 2 6Sample Output:
45
https://www.patest.cn/contests/pat-a-practise/1076
https://www.nowcoder.com/pat/5/problem/4306
思路:
一开始用的是DFS。。费了好长时间,PAT上可过,牛客网出错。
后来发现正解是BFS,加上层数判断
据说还可以用floyd等。。有待研究
CODE:
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;typedef struct S{int sno;int sum;int maxp;int gra[6];int f;};S stu[10010];int sc[6];bool cmp(S a,S b){if (a.f==b.f){if (a.sum==b.sum){if (a.maxp==b.maxp){return a.sno<b.sno;}elsereturn a.maxp>b.maxp;}elsereturn a.sum>b.sum;}elsereturn a.f>b.f;}int main(){int n,m,r;scanf("%d %d %d",&n,&m,&r);for (int i=1;i<=n;i++){stu[i].sum=0;stu[i].sno=i;stu[i].f=0;stu[i].maxp=0;for (int j=1;j<=m;j++){stu[i].gra[j]=-1;}}for (int i=1;i<=m;i++)scanf("%d",&sc[i]);for (int i=0;i<r;i++){int a,b,c;scanf("%d %d %d",&a,&b,&c);if (c!=-1) stu[a].f=1; else c++;stu[a].gra[b]=max(c,stu[a].gra[b]);}for (int i=1;i<=n;i++){for (int j=1;j<=m;j++){if (stu[i].gra[j]!=-1){stu[i].sum+=stu[i].gra[j];if (stu[i].gra[j]==sc[j]) stu[i].maxp++;}}}sort(stu+1,stu+1+n,cmp);int mc=1;for (int i=1;i<=n;i++){if (stu[i].f==0) break;if (i==1 || stu[i].sum!=stu[i-1].sum) mc=i;printf("%d %05d %d ",mc,stu[i].sno,stu[i].sum);for (int j=1;j<=m;j++){if (stu[i].gra[j]!=-1) printf("%d",stu[i].gra[j]);elseprintf("-");if (j!=m) printf(" ");}printf("\n");}return 0;}
- PAT Advanced Level 1076. Forwards on Weibo (30)
- 【PAT】【Advanced Level】1076. Forwards on Weibo (30)
- PAT (Advanced) 1076. Forwards on Weibo (30)
- PAT (Advanced Level) Practise 1076 Forwards on Weibo (30)
- PAT (Advanced Level) Practise 1076 Forwards on Weibo (30)
- PAT (Advanced Level) 1076. Forwards on Weibo (30) 微博转发 BFS
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1076(Forwards on Weibo)
- PAT A 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
- PAT(A) - 1076. Forwards on Weibo (30)
- 1076. Forwards on Weibo (30) PAT 甲级
- 关于for循环的优化问题
- 简单的opencv GUI库
- ViewStub Gone的区别
- spark学习笔记:flatMap()(API操作报错)
- 第2章 空间配接器(allocator)
- 【PAT】【Advanced Level】1076. Forwards on Weibo (30)
- (CodeForces
- 阿里云CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)
- C语言学习笔记——关于 .obj 文件的理解
- 年中总结--一切才只是刚刚开始
- 数据结构 求链表的长度以及用冒泡排序的算法对链表中的值进行排序
- Mac输入框消失解决
- 创建购物车实体类,模拟购物车功能
- Linux基础操作整理