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
**题目大意:**当一个用户发微博时,所有粉丝都能浏览并转发,然后可以被转发者的粉丝继续转发;给定一个网络环境,试计算一个用户的最大转发量(只计算L层粉丝);
输入:第一行:N和L;余下N行,第i个用户的粉丝数+粉丝id列表;最后一行,查询几个用户,id分别为什么;
代码如下:
import java.util.Scanner;
/**
* 建立图关系
* 查有多少粉丝(有向图:横查关注,竖查粉丝)
*/
public class Main {
private int maxLayer;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Main self = new Main();
MyGraph graph = self.handleInput(input);
int temp = input.nextInt();
for (int i = 0; i < temp; i++) {
int res = self.BFS(input.nextInt()-1, graph);
System.out.println(res);
graph.resetVisited();
}
}
//处理输入
public MyGraph handleInput(Scanner input){
MyGraph graph = new MyGraph(input.nextInt());
this.maxLayer = input.nextInt();
for (int i = 0; i < graph.getN(); i++) {
int count = input.nextInt();
for (int j = 0; j < count; j++) {
graph.getGraph()[i][input.nextInt()-1] = 1;
}
}
return graph;
}
//广度优先搜索
public int BFS(int node, MyGraph graph){
MyQue que = new MyQue(graph.getN());
graph.getVisited()[node] = true;
que.addQue(node);
int count = 0;
int last = node;
int layer = 0;
int tail = node;
while (que.getLen() > 0){
int temp = que.leaveQue();
for (int i = 0; i < graph.getN(); i++) {
if (!graph.getVisited()[i] && graph.getGraph()[i][temp] == 1){
que.addQue(i);
graph.getVisited()[i] = true;
count++;
tail = i;
}
}
if (last == temp){
layer++;
last = tail;
}
if (layer == this.maxLayer){
break;
}
}
return count;
}
}
//图对象
class MyGraph{
private int n;
private int[] nodes;
private int[][] graph;
private boolean[] visited;
public MyGraph(int n) {
this.n = n;
this.graph = new int[this.n][this.n];
this.visited = new boolean[this.n];
init();
}
private void init(){
this.nodes = new int[this.n];
for (int i = 0; i < this.n; i++) {
this.nodes[i] = i;
}
}
public void resetVisited(){
for (int i = 0; i < this.visited.length; i++) {
this.visited[i] = false;
}
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int[][] getGraph() {
return graph;
}
public void setGraph(int[][] graph) {
this.graph = graph;
}
public boolean[] getVisited() {
return visited;
}
public void setVisited(boolean[] visited) {
this.visited = visited;
}
public int[] getNodes() {
return nodes;
}
public void setNodes(int[] nodes) {
this.nodes = nodes;
}
}
class MyQue{
private int[] que;
private int top;
private int rear;
public MyQue(int len) {
this.que = new int[len];
this.top = -1;
this.rear = -1;
}
public void addQue(int node){
que[++top] = node;
}
public int leaveQue(){
return que[++rear];
}
public int getLen(){
return top-rear;
}
}