1076 Forwards on Weibo (30分) PAT

  1. 注意被关注和关注者别混淆
  2. 注意层次关系
  3. 注意结点是从1开始计数
#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
#include<map>

using namespace std;

const int maxn = 1010;

int BFS(int test, vector<int> fl[], int N, int L) {
    int num = 0;
    int inq[N+1] = {0};
    queue<int> q;
    q.push(test);
    inq[test] = 1;
    while(!q.empty()) {
        int temp = q.front();
        q.pop();
        // printf("%d ", temp);  // test
        for (int i = 0; i < fl[temp].size(); i++) {
            int temp2 = fl[temp][i];
            // printf("%d ", temp2);  // test
            if (inq[temp2] == 0) {
                q.push(temp2);
                inq[temp2] = inq[temp] + 1;
                if (inq[temp2] <= L+1)
                    num++;
            }
        }
        // printf("\n");  // test
    }
    return num;
}

int main() {
    int N, L, K;
    vector<int> fl[maxn];
    int num, follower;

    scanf("%d %d", &N, &L);
    for (int i = 1; i <= N; i++) {
        scanf("%d", &num);
        while(num--) {
            scanf("%d", &follower);
            fl[follower].push_back(i);
        }
    }
    scanf("%d", &K);
    int test;
    while(K--) {
        scanf("%d", &test);
        num = BFS(test, fl, N, L);
        printf("%d\n", num);
    }

    return 0;
}
发布了17 篇原创文章 · 获赞 0 · 访问量 2436

猜你喜欢

转载自blog.csdn.net/Ike_Lin/article/details/104598414