PTA 1076 Forwards on Weibo

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构2020年春AC代码与题目解析(C语言)

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

题目分析

这道题如果做过PTA mooc里面的六度空间题目的话,实现思路其实几乎是完全一样的。

首先,这道题目的意思是给定一个类似于微博的粉丝订阅网络,每个人都可以关注任何其他用户,每个人都可以转发关注的用户发布或转发的内容,求某个用户的某次发布的内容的潜在转发量。

关键点分析

这道题其实主要就是两个要点:

  1. 如何把整个订阅网络(一个有向图)存储起来?
  2. 如何计算某个用户的潜在转发量?

这里因为一个订阅网络中有大量的用户,但每个用户关注数离总用户数通常有较大差距,使用邻接矩阵来存储该网络的话会浪费大量存储空间,因此此处使用邻接表来存储这个有向图。

计算某个用户的转发量其实类似于广度遍历,以该用户结点为中心,一圈圈(每一圈为一层)的计算可能的转发用户数。以题目中给出的例子说明:查询用户2的潜在转发量且只查询3层时,这个发布内容的转发过程可以视为:用户2->用户1->用户4->用户5和6,潜在转发包括用户1、4、5和6,共4个

实现

使用到了三个数据结构:存储有向图的邻接表、用于广度遍历的队列以及标记某个用户是否访问过的visited标记数组

对应于题目的两个要点,程序主要函数步骤也有两步:读数据建图的BuildGraph和计算转发量的CalForwards


这里我按照比较规范的方式实现了一个邻接表和一个队列,因此代码略繁,可以简化成代码较少的版本。(用C来写真是要不停重新造轮子,费劲)

#include <stdio.h>
#include <stdlib.h>

#define MaxVertexNum 1001

/* 无向图的邻接表定义——开始 */
typedef int Vertex;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode {
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode {
    int Nv;
    AdjList G;
};
typedef PtrToGNode LGraph;

LGraph CreateGraph( int VertexNum )
{
    Vertex V;
    LGraph Graph;

    Graph = (LGraph)malloc(sizeof(struct GNode));
    Graph->Nv = VertexNum;

    for (V = 1; V <= Graph->Nv; ++V)
        Graph->G[V].FirstEdge = NULL;

    return Graph;
}

void DestoryGraph( LGraph Graph )
{
    Vertex V;
    PtrToAdjVNode Node;
    for (V = 1; V <= Graph->Nv; ++V) {
        while (Graph->G[V].FirstEdge) {
            Node = Graph->G[V].FirstEdge;
            Graph->G[V].FirstEdge = Node->Next;
            free(Node);
        }
    }
    free(Graph);
}

void InsertEdge(LGraph Graph, Vertex V, Vertex W)
{
    PtrToAdjVNode NewNode;

    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->AdjV = W;
    NewNode->Next = Graph->G[V].FirstEdge;
    Graph->G[V].FirstEdge = NewNode;
}
/* 无向图的邻接表定义——结束 */

/* 队列定义开始 */
#define MaxSize 1001
#define ERROR -1
typedef int Position;
struct QNode {
    int *Data;     /* 存储元素的数组 */
    Position Front, Rear;  /* 队列的头、尾指针 */
};
typedef struct QNode *Queue;

Queue CreateQueue()
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (int *)malloc(MaxSize * sizeof(int));
    Q->Front = Q->Rear = 0;
    return Q;
}

void DestoryQueue( Queue Q )
{
    if (Q->Data) free(Q->Data);
    free(Q);
}

int IsFull( Queue Q )
{
    return ((Q->Rear+1)%MaxSize == Q->Front);
}

void Enqueue( Queue Q, int X )
{
    if ( IsFull(Q) ) return;
    else {
        Q->Rear = (Q->Rear+1)%MaxSize;
        Q->Data[Q->Rear] = X;
    }
}

int IsEmpty( Queue Q )
{
    return (Q->Front == Q->Rear);
}

int Dequeue( Queue Q )
{
    if ( IsEmpty(Q) ) return ERROR;
    else  {
        Q->Front =(Q->Front+1)%MaxSize;
        return  Q->Data[Q->Front];
    }
}
/* 队列定义结束 */
int L;
Vertex visited[MaxVertexNum] = {0};
void setZero(int VertexNum)
{
    Vertex V;
    for (V = 1; V <= VertexNum; ++V)
        visited[V] = 0;
}

LGraph BuildGraph( int VertexNum )
{
    LGraph Graph;
    Vertex V, W;
    int m, i;

    Graph = CreateGraph(VertexNum);
    for (V = 1; V <= Graph->Nv; ++V) {
        scanf("%d", &m);    // user[V] follows m people
        for (i = 0; i < m; ++i) {
            scanf("%d", &W);
            InsertEdge(Graph, W, V);    // Note: userV follows userW denotes <W, V> in graph
        }
    }
    return Graph;
}

int BFS(LGraph Graph, Vertex V, int levelNum)
{
    int count, level;
    Vertex tail, last; Queue Q;
    PtrToAdjVNode Node;
    Q = CreateQueue();
    setZero(Graph->Nv);

    visited[V] = 1; count = 0;
    level = 0; last = V; tail = V;
    Enqueue(Q, V);
    while(!IsEmpty(Q)) {
        V = Dequeue(Q);
        for (Node = Graph->G[V].FirstEdge; Node; Node = Node->Next) {
            if (!visited[Node->AdjV]) {
                visited[Node->AdjV] = 1;
                Enqueue(Q, Node->AdjV); ++count;
                tail = Node->AdjV;
            }
        }
        if (V == last) {
            ++level; last = tail;
        }
        if (level == levelNum) break;
    }

    DestoryQueue(Q);
    return count;
}

void CalForwards(LGraph Graph, int levelNum)
{
    int i, k, count;
    Vertex V;
    scanf("%d", &k);
    for (i = 0; i < k; ++i) {
        scanf("%d", &V);
        count = BFS(Graph, V, levelNum);
        printf("%d\n", count);
    }
}

int main()
{
    int userNum, levelNum;
    LGraph Graph;
    scanf("%d %d", &userNum, &levelNum);
    Graph = BuildGraph(userNum);
    CalForwards(Graph, levelNum);
    DestoryGraph(Graph);

    return 0;
}

发布了14 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zhuiyisinian/article/details/105249299