PAT(A) - 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

思路分析:题目背景是关于微博用户的粉丝数量。题意大概为给你一个图(有向的),还有一个L,L代表最大范围的层数,然户给出Q个待查询的用户,统计每个用户在小于等于L层内的粉丝数量。

样例说明:7 3就是这个图里有7个顶点,就是7个用户。3是最大范围的层数。

3 2 3 4是用户1的粉丝有2、3、4。注意这里是有向图,这句话意思就是2、3、4关注了用户1,但没有说明用户1关注了2、3、4。

0是用户2没有粉丝

.......

底下的依次类推。

最后一行给出一个待查询的人数Q。这里为2 2 6,意思就是要查询2个人,用户2和用户6在3层范围内的粉丝数量。


这里用BFS比较方便,建图就不用说了,主要是修改BFS函数,我的做法略微繁琐。由于要考虑在L层范围内,所以我的做法是保证队列里存放的都是相同层内的顶点(用户),然后依次出队,存进一个vector里,然后再遍历vector,找到下一层的顶点(用户),再存进队列。当然有更好的解决办法,但我觉得这么比较好理解,我脑海里第一个想法就是这么做的。虽然空间复杂度和时间复杂度都有增加,但也没超时(500MS)。题目给的时间很大有3000MS,可能Java要这么久吧。


#include 
#include 
#include 

#define MAX 1001

int MGraph[MAX][MAX];
int visit[MAX];

using namespace std;

void InitGraph( int n ) {
    for( int i = 1; i <= n; i++ )
        for( int j = 1; j <= n; j++ )
            MGraph[i][j] = 0;
}

void InsertEdge( int a, int b ) {
    MGraph[a][b] = 1;
}

int BFS( int curNode, int n, int l ) {
    queue q;

    q.push( curNode );      // 第一个结点入队
    visit[curNode] = 1;     // 标记访问过了

    int count = 0;          // 统计在有限层数内的粉丝数量
    for( int level = 0; level < l; level++ ) {
        vector vec;    // vector是每层粉丝的数量
        while( !q.empty() ) {   // 队列里存放的是每一层粉丝的编号,将其从队列依次取出放入vector
            int node = q.front();
            vec.push_back( node );
            //printf( "%d ", node );
            q.pop();
        }
        // 从上一层的粉丝中遍历(此时已在vector中),找到下一层的粉丝进入队列
        for( int i = 0; i < vec.size(); i++ ) {
            int node = vec[i];
            for( int j = 1; j <= n; j++ ) {
                if( !visit[j] && MGraph[node][j] == 1 ) {
                    q.push( j );
                    visit[j] = 1;
                    count++;
                }
            }
        }
    }
    return count;
}

int main() {
    //freopen( "123.txt", "r", stdin );
    int N, L, Q;
    scanf( "%d%d", &N, &L );

    InitGraph( N );     // 初始化邻接矩阵
    int k, m;
    for( int i = 1; i <= N; i++ ) {
        scanf( "%d", &k );
        for( int j = 0; j < k; j++ ) {
            scanf( "%d", &m );
            InsertEdge( m, i );     // 插入边,这里是有向的
        }
    }

    scanf( "%d", &Q );
    for( int i = 0; i < Q; i++ ) {
        memset( visit, 0, sizeof( visit[0] ) * ( N + 1 ) ); // 每次BFS前先把visit清空
        int q;
        scanf( "%d", &q );
        int count = BFS( q, N, L );
        printf( "%d\n", count );
    }

    return 0;
}


你可能感兴趣的:(----bfs,PAT)