Pat(A) 1084. Broken Keyboard (20)

来源:互联网 发布:html 炫酷展示页源码 编辑:程序博客网 时间:2024/05/16 18:30

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1084

Broken Keyboard (20)


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

输入第一行为N(用户数)、L(层数)。

后N行表示第i个用户所关注的用户id。

再一行第一个数M为求的人的个数,后M个数表示用户id。

输出为M个数,表示L层内能看到的用户数。

解题报告

用一个图G表示关注情况。
对每个求的用户bfs搜索,搜索L层。用visited表示访问与否。
注意事项:

  1. 每次bfs,visited都要进行初始化。

代码

#include "iostream"#include "queue"#include "vector"using namespace std;vector<bool> visited;queue<int> que;int G[1000 + 5][1000 + 5];int N,L;void init(){    cin>>N>>L;    fill(G[0],G[0]+ 1005*1005,0);    int m,i,j;    int k;    for(i = 1; i <= N; i ++){        cin>>m;        for(j = 1; j <= m; j++){            cin>>k;            G[i][k] = 1;        }    }}void bfs(int root){    visited.clear();    visited.resize(N+1,false);    while(!que.empty()){        que.pop();    }    int level = 1;    int num=1,nextnum = 0,cou = 0,ans=0;    que.push(root);    visited[root] = true;    int a;    int i;    while(!que.empty()){        if(level > L)            break;        cou ++;        a = que.front();        que.pop();        for(i = 1; i <= N; i++){            if(G[i][a] && !visited[i]){                visited[i] = true;                ans ++;                que.push(i);                nextnum ++;            }        }        if(cou == num){            num = nextnum;            nextnum = 0;            cou = 0;            level ++;        }    }    cout<<ans<<endl;}void print_G(){    int i,j;    for(i = 1;i <= N; i++)        cout<<"\t"<<i;    cout<<endl;    for(i = 1;i <= N; i++)        cout<<"-------\t";    cout<<endl;    for(i = 1; i <= N; i++){        cout<<i<<"|";        for(j=1; j <= N; j++)            cout<<"\t"<<G[i][j];        cout<<endl;    }}int main(){    init();    //print_G();    int k,root;    cin>>k;    for(int i = 0; i < k; i++){        cin>>root;        bfs(root);    }    //system("pause");}