题目来源:pat A1076
日期:2023.10.10
第2天
题目:
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
代码长度限制
16 KB
时间限制
3000 ms
内存限制
64 MB
题解:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, L;
const int N = 1010; //最多1000名用户
/*
i号结点引导的链表(vector),存储的是
它能到达的顶点;如果需要维护额外的信息,
就让链表的元素是结构体。结构体里面带上编号
*/
struct Node{
int id;//编号
int level = 0;// 层数
};
vector<Node> Adj[N]; //用户的编号是从1-n
bool inq[N] = {0}; //是否被加入过队列中
int BFS(int u) {
queue<Node> q;
Node start;
start.id = u;
start.level = 0;
q.push(start);
inq[start.id] = 1;
int numForward = 0;//返回的答案
while(!q.empty()) {
Node topNode = q.front();
int v = topNode.id; //取出队首结点编号
q.pop();
//遍历整个顶点能到达的所有顶点,把能加进来的全部加到队列,能加到队列的顶点数就是答案
for(int i = 0; i < Adj[v].size(); i++) {
Node next = Adj[v][i];
next.level = topNode.level + 1; //BFS(),每一个顶点作为队头,要加入与他相邻的顶点时,遍历深度就加一
//已经确定有边,只要层数不大于L,而且未被加入过队列中
if(next.level <= L && inq[next.id] == 0) {
q.push(next);
inq[next.id] = 1;
numForward++;
}
}
}
return numForward;
}
int main(void) {
scanf("%d%d", &n, &L);
//建图
for(int i = 1; i <= n; i++) {
Node user;
user.id = i;
int num;
scanf("%d", &num); //i号用户关注的人数
for(int j = 0; j < num; j++) {
int id;
scanf("%d", &id);
// A关注了B,B的动态A可以看到,B到A建一条边
Adj[id].push_back(user);
}
}
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++) {
int userId; //发布动态的用户编号
scanf("%d", &userId);
//应该让所有顶点的层数都重新为0,加入队列的状态都为0
memset(inq, 0, sizeof(inq));
//得到消息转发数
int numForward = BFS(userId);
printf("%d\n", numForward);
}
return 0;
}
出现的问题:
1.BFS()函数中,第一步是新建一个队列,然后把传入顶点加入到新建的队列;
2.把传入顶点加入队列中,忘记设置这个顶点的状态为已加入队列;
3.while()循环之后,for()循环之前,忘记取出队头元素编号,导致一直使用第一个顶点(传入的参数);(在for循环中,始终找的是第一个顶点的邻接顶点);
4.函数忘记写返回值;
5.在对地图进行多次bfs()时,忘记初始化状态数组;