PAT1076. Forwards on Weibo

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
思路:DFS容易出现超时,而且需要注意个别情况。因此此题用BFS较好。另外需要注意一点就是follow and followed的关系

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <queue>
 5 #include <cstring>
 6 using namespace std;
 7 #define MAX 1010
 8 bool G[MAX][MAX];
 9 int N,L;
10 struct Node
11 {
12     int id;
13     int layer;
14 };
15 bool visited[MAX]={
16     false
17 };
18 int person;
19 //DFS超时
20 /*
21 void DFS(int index,int level)
22 {
23
24     if(level>L)
25        return ;
26     if(level!=0)
27       person++;
28     visited[index]=true;
29     for(int i=1;i<=N;i++)
30     {
31         if(!visited[i]&&G[index][i])
32         {
33             DFS(i,level+1);
34         }
35     }
36 }*/
37 void BFS(int query)
38 {
39     queue<Node> Q;
40     Node tem;
41     tem.id=query;
42     tem.layer=0;
43     Q.push(tem);
44     visited[query]=true;
45     while(!Q.empty())
46     {
47         Node now=Q.front();
48         person++;
49         Q.pop();
50         for(int i=1;i<=N;i++)
51         {
52             if(!visited[i]&&G[now.id][i])
53             {
54                 if(now.layer+1<=L)
55                 {
56                     visited[i]=true;
57                     Node next;
58                     next.id=i;
59                     next.layer=now.layer+1;
60                     visited[i]=true;
61                     Q.push(next);
62                 }
63             }
64         }
65     }
66     person--;
67 }
68 int main(int argc, char *argv[])
69 {
70     int query;
71     scanf("%d%d",&N,&L);
72     for(int i=1;i<=N;i++)
73     {
74         int count;
75         scanf("%d",&count);
76         for(int j=1;j<=count;j++)
77         {
78             int tem;
79             scanf("%d",&tem);
80             G[tem][i]=true;
81         }
82     }
83     scanf("%d",&query);
84     for(int i=0;i<query;i++)
85     {
86         int number;
87         scanf("%d",&number);
88         person=0;
89         memset(visited,false,sizeof(visited));
90         if(!visited[number])
91             BFS(number);
92         printf("%d\n",person);
93     }
94     return 0;
95 }

时间: 2024-08-01 20:59:52

PAT1076. Forwards on Weibo的相关文章

PAT1076. Forwards on Weibo(标准bfs模板)

//标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 #include<cstdio>#include<vector>#include<queue>#include<algorithm>using namespace std;const int maxn=1001;struct node{ int level; ve

PAT1076. Forwards on Weibo (30)

使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using namespace std; int n; int l; int i,j; int m,k; int follower[1001][1001]; vector<int> user_list[1001]; set<int> user_s; void queryU(int a,int depth

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: 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 hi

PAT 1076. Forwards on Weibo (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1076 此题考查图的遍历操作: 本来我个人觉得可以用dfs的,但是不知何故,有两个case没有过,贴出代码,望指出错误之处: #include <cstdio> #include <map> #include <vector> #include <memory.h> using namespace std; const int NUM=1001; bool

1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B 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 follo

1076. Forwards on Weibo (30)

时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

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

PAT 1076 Forwards on Weibo

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

PAT A1076 Forwards on Weibo (30 分)——图的bfs

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