Blue Bridge Cup network analysis (java)

[Problem description]
Xiao Ming is doing a network experiment.
He set up n computers, called nodes, for sending, receiving and storing data.
Initially, all nodes are independent and there is no connection.
Xiao Ming can connect two nodes through a network cable, and the two nodes can communicate
with each other after the connection . If two nodes are connected by a network cable, they are called adjacent.
Xiao Ming sometimes tests the network at that time. He will send a message to a node, and the message will be sent
to each adjacent node, and then these nodes will be forwarded to their adjacent nodes until all directly
or indirectly adjacent nodes The nodes have all received the information. All sending and receiving nodes will store the information.
A piece of information is stored only once.
Given the process of Xiaoming's connection and testing, please calculate the size of the information stored in each node.
[Input format]
The first line of input contains two integers n, m, which respectively represent the number of nodes and the number of operations. The nodes are
numbered from 1 to n.
In the next m lines, three integers in each line represent an operation.
If the operation is 1 ab, it means that node a and node b are connected by a network cable. When a = b
, it means that a self-loop is connected, which has no substantial impact on the network.
If the operation is 2 pt, it means that a message of size t is sent on node p.
[Output format]
Output a line, containing n integers, separated by a space between adjacent integers, indicating in turn
the size of the information stored on node 1 to node n after the above operations are performed.

Problem-solving idea : talk about topic division, input the nodes according to the requirements, and store the named array, read the names in turn, and then analyze whether to connect the nodes or transmit data. If the nodes are connected, the knowledge of the linked list is used. To string together different nodes in both directions, if data is to be transmitted, it is to combine recursion and linked lists, and store the corresponding amount of data in each node in turn.

package 蓝桥杯;

import java.util.*;

public class 网络分析 {
    
    
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		//定义共有n个节点
		int n = in.nextInt();
		//定义共有m条操作
		int m = in.nextInt();
		List<wangLuoNode> node = new ArrayList<wangLuoNode>();
 		int[][] log = new int[m][3];
 		//创建节点
 		for(int i = 0;i < n;i++) {
    
    
			wangLuoNode next = new wangLuoNode(i,0);
			node.add(next);
 		}
		//输入操作
		for(int i = 0;i < m;i++) {
    
    
			for(int j = 0;j < 3;j++) {
    
    
				log[i][j] = in.nextInt();
			}
		}

		//查看节点信息量
		for(wangLuoNode temp:node) {
    
    
			System.out.println(temp.num+"号节点信息量为:"+temp.sum);
		}
		//执行命令
		for(int i = 0;i < m;i++) {
    
    
			//连接节点
			if(log[i][0] == 1) {
    
    
				if(log[i][1] != log[i][2]) {
    
    
					node.get(log[i][1]-1).node.add(node.get(log[i][2]-1));
					System.out.println();
					System.out.println(log[i][1]+"和"+log[i][2]+"建立连接了!");
					node.get(log[i][2]-1).node.add(node.get(log[i][1]-1));
					System.out.println();
					System.out.println(log[i][2]+"和"+log[i][1]+"建立连接了!");
				}
			}
			//传输数据
			else {
    
    
				int[] flag = new int[n];
				//给某个节点传输多少的量
				transhu(flag,node.get(log[i][1]-1),log[i][2]);
				//查看节点信息量
				System.out.println();
				for(wangLuoNode temp:node) {
    
    
					System.out.println(temp.num+"号节点信息量为:"+temp.sum);
				}
				System.out.println();
				System.out.println(log[i][1]+"传输了"+log[i][2]+"的信息!");
			}
		}
	}
	//进行遍历传输
	public static void transhu(int[] flag, wangLuoNode node,int text) {
    
    
		if(node.node.size() == 0) {
    
    
			node.sum += text;
			return;
		}
		for(int i = 0;i < node.node.size();i++){
    
    
			if(flag[node.node.get(i).num] != 1) {
    
    
				node.node.get(i).sum += text;
				flag[node.node.get(i).num] = 1;
			}else {
    
    
				continue;
			}
			transhu(flag,node.node.get(i),text);
		}
	}
}

class wangLuoNode{
    
    
	int num;
	int sum;
	List<wangLuoNode> node = new LinkedList<wangLuoNode>();
	public wangLuoNode(int num,int sum) {
    
    
		this.sum = sum;
		this.num = num;
	}
}

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/109059625