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 6Sample Output:
4 5
#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;
}