PATA1076-Forwads On Weibo

題目描述

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 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 trigger, 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

參考代碼

#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;
const int maxn = 1010;

int L,N;
int G[maxn][maxn] = {0};
//bool vis[maxn] = {false};
struct node
{
    //頂點(diǎn)編號(hào)和層號(hào)
    int v,layer;
    node(int _v,int _layer):v(_v),layer(_layer){}
};

void BFS(int u,int& sum,bool inq[])
{
    queue<node> Q;
    Q.push(node(u,0));
    inq[u] = true;
    while(!Q.empty())
    {
        node top = Q.front();
        Q.pop();
        //printf("第%d層,正在訪問%d\n",top.layer,top.v);
        //vis[top] = true;
        //如果在規(guī)定的層內(nèi)
        if(top.layer<L)
            for(int i=1;i<=N;i++)
            {
                if(G[i][top.v]>0  && inq[i] == false)
                {
                    Q.push(node(i,top.layer+1));
                    inq[i] = true;
                    sum ++;
                    //printf("第%d層,sum=%d\n",top.layer,sum);
                }
            }
    }
}

int main()
{
    bool mid[maxn] = {false};
    bool inq[maxn] = {false};
    scanf("%d %d",&N,&L);
    int k,temp;
    for(int i=0;i<N;i++)
    {
        scanf("%d",&k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&temp);
            G[i+1][temp] = 1;
        }
    }
    scanf("%d",&k);
    for(int i=0;i<k;i++)
    {
        scanf("%d",&temp);
        int sum=0;
        BFS(temp,sum,inq);
        printf("%d\n",sum);
        //數(shù)組賦值
        memcpy(inq,mid,maxn*sizeof(bool));
    }

}






?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,220評(píng)論 0 23
  • 24留不住錢的意識(shí)補(bǔ)充 很多人的內(nèi)在力量不穩(wěn)定,我值不值、配不配,做賊心虛的能量,我有這個(gè)錢,我對(duì)不對(duì)?覺得自己不...
    貴州沈彥宏閱讀 241評(píng)論 0 0
  • 本文原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處。歡迎關(guān)注我的 簡(jiǎn)書 ,關(guān)注我的專題 Android Class 我會(huì)長(zhǎng)期堅(jiān)持為大家收錄簡(jiǎn)...
    MeloDev閱讀 1,296評(píng)論 8 33
  • 敏捷開發(fā)對(duì)于個(gè)體的要求很高,因?yàn)樵谝粋€(gè)確定的時(shí)間跨度內(nèi),比如兩周內(nèi)出一個(gè)版本,如果沒有相當(dāng)?shù)拈_發(fā)經(jīng)驗(yàn)、訓(xùn)練和能力,...
    michael_jia閱讀 467評(píng)論 0 0

友情鏈接更多精彩內(nèi)容