The 11th Blue Bridge Cup Provincial Competition - Network Analysis

network analysis

[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 do not have any connections.
Xiao Ming can connect the two nodes through the network cable, and after the connection, the two nodes can communicate
with each other. If two nodes are connected by a network cable, they are called adjacent.
Xiaoming sometimes tests the network at that time, he will send a message at a certain node, the message will be sent
to each adjacent node, and then these nodes will forward to their adjacent nodes, until all the directly
or indirectly adjacent nodes. Nodes have received the information. All sending and receiving nodes store the information.
A piece of information is only stored 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 represent the number of nodes and the number of operations respectively. Nodes are
numbered from 1 to n.
The next m lines, each with three integers, represent an operation.
If the operation is 1 ab, it means that the node a and the node b are connected by a network cable. When a = b
, it means that a self-loop is connected and has no substantial impact on the network.
If the operation is 2 pt, it means to send a message of size t on node p.
[Output format]
One line is output, including n integers. The adjacent integers are separated by a space, which in turn indicates
the size of the information stored on node 1 to node n after the above operations are performed.
Question J: Network Analysis 12
The 11th Blue Bridge Cup Competition Software Provincial Competition C/C++ University Group B
[Sample input]
4 8
1 1 2
2 1 10
2 3 5
1 4 1
2 2 2
1 1 2
1 2 4
2 2 1
[Sample output]
13 13 5 3
[Evaluation case scale and convention]
For 30% The evaluation case for 1 ≤ n ≤ 20, 1 ≤ m ≤ 100.
For 50% of the evaluation cases, 1 ≤ n ≤ 100, 1 ≤ m ≤ 1000.
For 70% of the evaluation cases, 1 ≤ n ≤ 1000, 1 ≤ m ≤ 10000.
For all evaluation cases, 1 ≤ n ≤ 10000, 1 ≤ m ≤ 100000, 1 ≤ t ≤ 100.
*/

DFS search (70%)

#include<bits/stdc++.h>
using namespace std;
int const N =1050;
int vis[N];//判定是否遍历过 
int d[N][N];//邻接表,d[i][j]表示i和j的连通性 
int num[N];
int i,j,k;
int n,m;
void dfs(int x,int y){
    
    
	num[x]+=y;
	vis[x]=1;
	//遍历当前与x结点联通的结点 
	for(i=1;i<=n;i++){
    
    
		if(!vis[i]&&d[x][i]){
    
    
			dfs(i,y);
		}
	}
}
int main(){
    
    
	cin>>n>>m;
	while(m--){
    
    
		int x,y,z;
		cin>>x>>y>>z; 
		if(x==1){
    
    
			d[y][z]=d[z][y]=1;//1表示联通 
		}
		else if(x==2){
    
    
			memset(vis,0,sizeof(vis));//每次都要重新遍历,所以要初始化 
			dfs(y,z);
		}
	}
	for(i=1;i<=n;i++)
		cout<<num[i]<<" ";
	return 0;
}

Reprinted: Diligent and diligent can make up for the poor
and find the details of the fun of the algorithm

Union check (100%)

#include<bits/stdc++.h>
int const N = 10010;
using namespace std;
int n,m;
int i,j,k; 
int num[N]; 
//表示为:N的大哥为f[N]这个值  范围  1<=f[N]<=n
int f[N];
//表示为:N的小弟数量为f[N]这个值  
int r[N]; 
// 对f[N]进行初始化,每个人是自己的大哥 
// 对r[N]进行初始化,每个人都有自己的小弟 
void init(){
    
    
	for(i=1;i<=n;i++){
    
    
		f[i]=i;
		r[i]=1; 
	}
}
//找自己 (x) 的大哥 
int find(int x){
    
    
	 return f[x]==x?x:(f[x]=find(f[x]));	
 } 
//x所在帮派和y所在帮派合并,那么x的所有人和y的所有人就是兄弟了
//但是呢?还是需要一个大哥,谁来做大哥呢?当然是小弟多的人做大哥呀 
void send(int x,int y){
    
    
	int tempx,tempy;
	//分别找到自己的总大哥 
	tempx = find(x);
	tempy = find(y);
	if(r[tempx]>=r[tempy]){
    
    
		//tempx的小弟人数多,那么tempy就并到tempx之下,成为tempy的小弟 
		f[tempy]=tempx; 
	}
	else{
    
    
		f[tempx]=tempy;
	}
	if(r[tempx]==r[tempy]&&tempx!=tempy){
    
    
		r[tempx]++;
	}
} 
void count(int x,int y){
    
    
	int tempx;
	tempx = find(x);
	for(i=1;i<=n;i++){
    
    
		if(tempx==find(i)){
    
    
			num[i]+=y;
		}
	}
} 
int main(){
    
    
	cin>>n>>m;
	init();
	while(m--){
    
    
		int x,y,z;
		cin>>x>>y>>z;
		if(x==1){
    
    
			send(y,z);
		}
		if(x==2){
    
    
			count(y,z);
		}
	}
	for(i=1;i<=n;i++){
    
    
		cout<<num[i]<<" ";
	}
	return 0;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735984&siteId=291194637