【PAT甲级真题】- Forwards on Weibo (30)

张开发
2026/4/17 19:26:13 15 分钟阅读

分享文章

【PAT甲级真题】- Forwards on Weibo (30)
题目来源Forwards on Weibo (30)注意点下标从 1 开始题目描述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.输入描述: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.输出描述: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.输入例子:7 3 3 2 3 4 0 2 5 6 2 3 1 2 3 4 1 4 1 5 2 2 6输出例子:4 5思路简介广搜深度记录链式前向星建图然后记录深度广搜即可在 PAT 当中出现很多次了记录深度也很简单num 记录当前层有几个元素high 记录深度每次取出num--当num0时说明遍历完一层high此时队列的长度即下一层的大小numq.size()这里注意一下一个人转发了之后他的所有关注者都会看见只有其中没转发过的人才会转发遇到的问题小标从 1 开始vector存在下标越界的问题要多开一个空间或者下标减 1Edge数组一开始开小了应该开N*N代码/** * https://www.nowcoder.com/pat/5/problem/4306 * 广搜 */#includebits/stdc.husingnamespacestd;constintN10007;structEdge{intto,next;Edge():to(-1),next(-1){}Edge(intto,intnext):to(to),next(next){}}e[N*N];inthead[N],cnt0;voidadd(intu,intv){e[cnt]{v,head[u]};head[u]cnt;}intn,l;voidinput(){memset(head,-1,sizeof(head));cinnl;for(inti0;in;i){intm;cinm;for(intj0;jm;j){intv;cinv;add(v,i1);}}}voidsolve(){intk;cink;queueintq;vectorintvis(n1);q.push(k);intres0,high0,num1;vis[k]1;while(!q.empty()highl){intuq.front();q.pop();num--;for(intihead[u];i!-1;ie[i].next){if(!vis[e[i].to]){res;vis[e[i].to]1;q.push(e[i].to);}}if(!num){high;numq.size();}}coutres\n;}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//fstream in(in.txt,ios::in);cin.rdbuf(in.rdbuf());input();intT1;cinT;while(T--){solve();}return0;}

更多文章