题目
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
注意,给出的是相应编号的人所follow的人……
其实就是求有向图相应点i所能到达的距离小于L的点的数量。
算法时间要求相当宽3000ms,最简单的想法是按点为起点bfs探测树,任何follow距离设为1,记录深度到L时的点的数量,并输出。对N个点每个点探测1次,极限代价N*N*L。考虑到数字的规模,这个方法是可行的。通过一些具体的手法,可以使时间开销比较小,代码采用了这种方法。由于K没有限定,可能很大,所以干脆先把所有的N个点都求出结果,暂存,直接输出,当然用缓存会更好点。
比较标准的一种操作是求一下图中所有点间的距离(n^3),然后统计输出。代价比较大,不一定能AC。
代码:
#include <iostream> #include <cstdio> #include <vector> #include <deque> using namespace std; struct idd //探测用结构 { int id; //人的编号 int d; //深度 }; int main() { int n,l; cin>>n>>l; vector<int> *net=new vector<int> [n+1]; //存相应编号的人所可以forward到的对象 int *flag_tested=new int [n+1]; //存探测时相应编号的人的探测情况,用于加速。0表示未入队列,1入队里但未探测其follow,2已经探测其follow(2多余了) int *num=new int [n+1]; //记录相应编号的人L次后可以forward到的人的数量 int i,j,num_fol,fol; for(i=1;i<=n;i++) //输入数据 { scanf("%d",&num_fol); for(j=0;j<num_fol;j++) { scanf("%d",&fol); net[fol].push_back(i); } } deque<idd> test; //探测用队列,要用到clear idd temp; //临时结构 for(i=1;i<=n;i++) //暴力方法,先探测所用人,可以用缓存表优化 { num[i]=0; //初始化 for(j=1;j<=n;j++) flag_tested[j]=0; flag_tested[i]=1; //将初始点标记为入队,深度0,并入队 test.clear(); temp.id=i; temp.d=0; test.push_back(temp); while(!test.empty()) //bfs { if(test.front().d>=l) //到达深度限定,直接退出 break; if(flag_tested[test.front().id]!=2) //去除已经探测过的点(2的判断多余了……) { for(j=0;j<net[test.front().id].size();j++) //探测可以到达的点,累加,入队 { if(flag_tested[net[test.front().id][j]]==0) { temp.id=net[test.front().id][j]; temp.d=test.front().d+1; flag_tested[net[test.front().id][j]]=1; test.push_back(temp); num[i]++; } } flag_tested[test.front().id]=2; } test.pop_front(); } } int k,id; //输出 cin>>k; for(i=0;i<k;i++) { scanf("%d",&id); printf("%d\n",num[id]); } delete [] net; delete [] flag_tested; delete [] num; return 0; }