PTAForwards on Weibo (30分)

    科技2022-08-10  110

    每个人都有崩溃的时候,就看你的抗压能力到底有多强,如果你的抗压能力强,有办法可以支撑到你能面对并且解决这些困难的话,你就没有问题。                                                                          ----喻言

    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 <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #include <numeric> #include<unordered_set> #include <climits>//INT_maxnn //#include<bits/stdc++.h> #define PP pair<ll,int> #define inf 0x3f3f3f3f #define INF 0x7fffffff; #define llinf 0x3f3f3f3f3f3f3f3fll #define dinf 1000000000000.0 #define PI 3.1415926 #define LL unsigned int #define wc 1e-8 typedef long long ll; using namespace std; int n,l,f[1010],s; struct node{ int p; int d; }; queue<node>q; vector<int>v[1005]; int num; int main(){ cin>>n>>l; for(int i=1;i<=n;i++){ cin>>num; for(int j=1;j<=num;j++){ int x; cin>>x; v[x].push_back(i); } } cin>>num; for(int i=1;i<=num;i++){ node a; cin>>a.p; s=0; a.d=0; memset(f,0,sizeof(f)); f[a.p]=1; q.push(a); while(!q.empty()){ a=q.front(); q.pop(); if(a.d>=l) continue; for(int j=0;j<v[a.p].size();j++){ int x=v[a.p].at(j); if(!f[x]){ f[x]++; s++; node b; b.d=a.d+1; b.p=x; q.push(b); } } } cout<<s<<endl; } return 0; }

     

    Processed: 0.009, SQL: 8