PAT 1076 forwards on Weibo

Source: Internet
Author: User

Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and could follow many other users as well. Hence A social network is formed with followers relations. When a user makes a post to Weibo, all his/her followers can view and forward his/her post, which can then be forwarded AG Ain by their followers. Now given a social network, you is supposed to calculate the maximum potential amount of forwards for any specific user, Assuming that is only L levels of indirect followers is counted.

Input Specification:

Each input file contains the 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 is counted. Hence it is assumed and 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, followed by User[i]. It's guaranteed that no one can follow oneself. All the numbers is separated by a space.

Then finally a positive k are given, followed by K- UserID' s for query.

Output Specification:

For all UserID, you is supposed to print on one line the maximum potential amount of forwards this user can tri GER, assuming that's everyone who can view the initial post would forward it once, and that's only L levels of indirect followe RS is counted.

Sample Input:
7 33 2 3 402 5 62 3 12 3 41 41 52 2 6
Sample Output:
45

A solution seen on the understanding
#include <iostream>#include<cstdio>#include<vector>using namespacestd;Const intMAXN = ++5;intN, L, K;intV[MAXN], H[MAXN];//The v array is used to record the access state (visited) of each vertex, and the H array is used to record the number of layers in each vertexvector<int>E[MAXN];intMain () {scanf ("%d%d", &n, &m);  for(inti =1; I <= N; ++i) {intx, y; scanf ("%d", &x);  while(x--) {scanf ("%d", &y);    E[y].push_back (i); //equivalent to the adjacency table}} scanf ("%d", &j);  for(intK =1; K <= K; ++k) {intsrc; scanf ("%d", &src); V[SRC]= k;//replace the visited array so that v[src] = k does not need to empty the visited array every timeH[SRC] =0; Vector<int>Q;        Q.push_back (SRC);  for(inti =0; I < q.size (); ++i) {//equivalent to queue, use I to record front position            intx =Q[i]; if(H[x] >=L) Break;  for(intj =0; J < E[x].size (); ++j) {inty =E[x][j]; if(V[y]! =k) {V[y]=K; H[y]= H[x] +1;                Q.push_back (y); }}} printf ("%d\n", Q.size ()-1); }    return 0;}

PAT 1076 forwards on Weibo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.