【C/C++】1076. Forwards on Weibo (30)

本文介绍了一个模拟微博用户间转发行为的算法实现,通过构建有向图并运用广度优先搜索算法来计算一篇微博在特定层级内可能达到的最大转发量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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 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



题目源于:https://www.patest.cn/contests/pat-a-practise/1076
题意:微博在中国非常盛行,我们可以被其他人所关注,也可以关注其他人(注此处应用有向图),我们只能浏览被关注者所发表的微博。问题:当一位用户发表一篇微博时,他的每一位粉丝都会转发其内容,而粉丝的粉丝也会转发,以此类推,问,L次后,这篇微博的内容会被多少人浏览?
输入N,L(N为微博用户数,L为关系层数)。然后输入每个用户(随后第i行对应对第i位)的关注的用户数目n,再输入n位被关注者的id(对应行号i)。最后,输入数字K,同时,输入K位你要查询用户的ID(即序号)。
输出要查询的用户每发一篇微博,经过L轮转发后的浏览次数。
思路分析:通过输入构建有向图,经过广度优先搜索BFS,遍历经过L层的粉丝。


#include <iostream>
using namespace std;
#define ElementType int
#define WeightType int
#define vertex int
#define MaxData 1001

typedef struct {
  int Ne;
  int Nv;
  WeightType G[MaxData][MaxData];
}GNode  ;
typedef GNode * Graph;

typedef struct {
  vertex v1, v2;
  WeightType weight;
}Enode;
typedef Enode * Edge;

Graph createGraph(int size);
void insertEdge(Graph map, Edge e);
Graph buildGraph(int n);
int BFS(int num, Graph map, bool * visited,int d);

typedef struct {
  ElementType * data;
  int front, rear;
  int size;
}QNode;
typedef QNode * queue;
queue createQueue(int size);
void enqueue(queue q, ElementType data);
ElementType dequeue(queue q);
bool isFull(queue q);
bool isEmpty(queue q);


int main()
{
  int nv; cin >> nv;
  int d; cin >> d;
  Graph map = new GNode;
  map = buildGraph(nv);
  bool* visited = new bool[nv];
  for (int i = 0; i < nv; i++) visited[i] = false;
  int n; cin >> n;
  for (int i = 0; i < n; i++) {
    int index; cin >> index;
    int count = BFS(index, map, visited,d);
    cout << count << endl;
    for (int i = 0; i < nv; i++) visited[i] = false;

  }
      return 0;
}

Graph createGraph(int size) {
  Graph map = new GNode;
  map->Nv = size;
  map->Ne = 0;
  for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++)
      map->G[i][j] = 0;
  return map;
}
void insertEdge(Graph map, Edge e) {
  map->G[e->v1][e->v2] = e->weight;
//  map->G[e->v1][e->v2] = e->weight;
}

Graph buildGraph(int n){
  Graph map = new GNode;
  //int nv; cin >> nv;
  map = createGraph(n);
  for (int i = 0; i < n; i++) {
    int m; cin >> m;
    Edge e=new Enode;
    for (int j = 0; j < m; j++) {  //v1为粉丝,v2为关注者
      e->v1 = i;
      int v2; cin >> v2; v2--;
      e->v2 = v2;
      e->weight = 1;
      insertEdge(map, e);
    }
  }
  return map;
}
queue createQueue(int size) {
  queue q = new QNode;
  q->size = size;
  q->front = q->rear = 0;
  q->data = new ElementType[size];
  return q;
}

void enqueue(queue q, ElementType data) {
  if (isFull(q))return;
  else {
    if (q->size == 1) {
      q->rear = (q->rear + 1) % 2;
      q->data[q->rear] = data;
    }
    else {
      q->rear = (q->rear + 1) % q->size;
      q->data[q->rear] = data;
    }
  }
}

ElementType dequeue(queue q) {
  if (isEmpty(q)) return -1;
  else {
    if (q->size == 1) q->front = (q->front + 1) % 2;
    else q->front = (q->front + 1) % q->size;
    return q->data[q->front];
  }
}
bool isFull(queue q) {
  if (q->size == 1) {
    if (q->front == q->rear) return false;
    else return true;
  }
  else if ((q->rear + 1) % q->size == q->front)return true;
  else return false;
}

bool isEmpty(queue q) {
  if (q->rear == q->front) return true;
  return false;
}
int BFS(int num, Graph map, bool * visited,int d) {
  
  visited[--num] = true;
  int count = 0;
  queue q = new QNode;
  q = createQueue(map->Nv);
  enqueue(q, num);
  int last = num, level = 0;
  int tail=0;
  while (!isEmpty(q)) {
    ElementType tmp = dequeue(q);
    count++;
    for (int i = 0; i < map->Nv; i++) {
      if (visited[i]==false && map->G[i][tmp] == 1)//查找关注了tmp的所有用户
      {
        enqueue(q, i);
        //count++;
        visited[i] = true;
        tail = i;
      }
    }
    if (last == tmp) {
      level++;
      last = tail;
    }
    if (level == d+1) break;
  }
  return count-1;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值