Program design final exam review topic five: tree structure

Foreword: Since time is running out. . . Subsequent topics will be topics plus code, there may be a little analysis. . . I really can't finish the review ~~~

 

The fifth topic: tree structure .

And check the set and minimum spanning tree algorithm: kruskal. Two important algorithms.

Relevant topic: CSP 2018-12-T4.

First of all, we must grasp these elements: undirected graph, edge weight, and minimum spanning tree.

The original meaning of this question is to require the largest edge in the smallest spanning tree.

First, let's talk about the execution process of kruskal: suitable for undirected graphs, first select the smallest edge weight from the graph, put it into a set, and remove the edge from the original graph. Next, the edge with the smallest edge weight is always taken from the graph, but there is a condition that the extracted edge cannot form a loop. If a loop is formed, then this edge should be removed and then found in the graph. Until all the edges in the set form a loop-free tree, this tree is the minimum spanning tree.

The largest side was taken out for the last time.

Code:

#include<iostream>
#include<algorithm>
using namespace std;

#define MAX 100005
struct edge
{
	int x, y;
	int w;
}e[MAX];
int fa[MAX];
int ans;
int cmp(edge a, edge b)//排序函数
{
	if (a.w != b.w)
		return a.w<b.w;
	else
	{
		return a.x<b.x;
	}
}
void make_set(int x)//初始化节点
{
	fa[x] = x;
}
int find(int x)//查找父节点
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void union_set(int x, int y, int w)//合并节点
{
	fa[y]=x;
	if (ans<w)
	{
		ans = w;
	}
}
int main()
{
	int x, y, w;
	int m, n, root;//n是点,m是边
	cin >> n >> m >> root;
	for (int i = 0; i<m; i++)
	{
		cin >> x >> y >> w;
		e[i].x = x;
		e[i].y = y;
		e[i].w = w;
		make_set(x);
		make_set(y);
	}
	sort(e, e + m, cmp);
	ans = 0;
	for (int i = 0; i<m; i++)
	{
		x = find(e[i].x);
		y = find(e[i].y);
		w = e[i].w;
		if (x != y)
		{
			union_set(x, y, w);
		}
	}
	cout << ans << endl;
	return 0;
}

This code relies on "dots" to determine whether it constitutes a circuit of the graph.

This chapter also follows the CSP topic and arranges CSP 2018-12-T2 assignments, together with the code.

topic:

Problem Description

  By the time school was over, Xiaoming had already planned his own route home and was able to predict the time it took to pass through various sections. At the same time, Xiaoming saw the indication status of all the traffic lights passing on the road at the time of departure through the "wisdom light" terminal installed in the school. Please help me calculate the time it takes for Xiaoming to go home this time.

Input format

  The first line of input contains three positive integers r, y, and g separated by spaces, indicating the setting of traffic lights. None of these three numbers exceed 10 ^ 6.

  The second line of the input contains a positive integer n, which represents the total number of road segments and the number of traffic lights passing by Xiao Ming.

  The next n lines each contain two integers k and t separated by spaces. k = 0 means that after passing a section of road, it will take t seconds, where t does not exceed 10 ^ 6; when k = 1, 2, 3, it means the departure time, and the traffic lights here are red and yellow , Green light, and the number displayed on the countdown display card is t, where t will not exceed r, y, g respectively.

Output format

  A number is output, indicating the time spent by Xiaoming going home from school this time.

Problem Solution: It is a long time sequence established according to the sequence of traffic lights to judge the traffic lights at the intersection when the time is running. Each crossing will increase the time.

Pay attention to the order of traffic lights: yellow light, red light, green light

Code:

#include<iostream>
using namespace std;
int r,y,g;
int tim(int x)
{
	if(x>y+r) return 0;
	else return y+r-x;
}

int main()
{
	//int r,y,g;
	cin>>r>>y>>g;
	int n;
	cin>>n;
	int k,t;
	long long sum=0;
	while(n--)
	{
		cin>>k>>t;
		if(k==0) sum+=t;
		else if(k==1) sum+=tim((y+r-t+sum)%(y+r+g));
		else if(k==2) sum+=tim((y-t+sum)%(y+r+g));
		else sum+=tim((y+r+g-t+sum)%(y+r+g));
	}
	cout<<sum<<endl;
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

Published 6 original articles · liked 0 · visits 184

Guess you like

Origin blog.csdn.net/morning_zs202/article/details/92802859