码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1076. Forwards on Weibo

时间:2015-02-27 20:01:49      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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 }
View Code

 

PAT1076. Forwards on Weibo

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4303947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!