Some topics of the 12th Blue Bridge Cup group a provincial competition in 2021

Time:2022-5-13

Question D: Path

Total score of this question: 10 points

[problem description]
Xiaolan is very happy after learning the shortest path. He defines a special graph and hopes to find it
The shortest path in the.
The diagram of Xiaolan consists of 2021 nodes, numbered from 1 to 2021.
For two different nodes a and B, if the absolute value of the difference between a and B is greater than 21, there are two nodes
There is no edge connection between them; If the absolute value of the difference between a and B is less than or equal to 21, there is a line between the two points
Undirected edges with lengths of the least common multiple of a and B are connected.
For example, there is no edge connection between node 1 and node 23; There is a null line between node 3 and node 24
To the side, with a length of 24; There is an undirected edge between node 15 and node 25, with a length of 75.
Please calculate the shortest path length between node 1 and node 2021.
Tip: it is recommended to use computer programming to solve the problem.

[answer submission]
This is a question filled in with results. You just need to calculate the results and submit them. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

Personal answer: 10266837

Solution 1: dynamic programming, interval DP

import java.util.Arrays;

class Main{
	static int INF=0x3f3f3f3f;
	static int[][] dis=new int[2025][2025];
	public static void main(String[] args) {
		init();
		int n=2021;
		for(int len=2;len <= n;len++) {
			for(int i=1;i<= n-len+1;i++) {
				int j=i+len-1;
				for(int k=i;k<= j;k++) {
					dis[i][j]=Math.min(dis[i][j], dis[i][k]+dis[k][j]);
				}
			}
		}
		System.out.println(dis[1][n]);
	}
	private static void init() {
		// TODO Auto-generated method stub
		for(int i=0;i<2025;i++) {
			Arrays.fill(dis[i], INF);
		}
		for(int i=1;i<=2021;i++) {
			for(int j=1;j<=2021;j++) {
				if(Math.abs(i-j)>21) {
					continue;
				}else {
					dis[i][j]=dis[j][i]=Math.min(dis[i][j], gbs(i, j));
				}
			}
			dis[i][i]=0;
		}
	}
	private static int gbs(int a,int b) {
		// TODO Auto-generated method stub
		int at=a,bt=b;
		while(true) {
			int t=gcd(at, bt);
			if (t!=1) {
				at=at/t;
				bt=bt/t;
			}else {
				break;
			}
		}
		return at*b;
	}
	private static int gcd(int a,int b) {
		if (b==0) {
			return a;
		}
		return gcd(b, a%b);
	}
}

Solution 2: shortest path algorithm. SPFA is used here

To be added

Question e: loop count

Total score of this question: 15 points

[problem description]
Lanqiao college consists of 21 teaching buildings, numbered 1 to 21. For two teaching buildings a and B, when a and B are reciprocal, there is a corridor directly connected between a and B, which can be accessed in both directions, no
There is no directly connected corridor.
Xiao Lan is now in the first teaching building. He wants to visit each teaching building exactly once and finally return to the second building
How many different access schemes does a teaching building have? Two
Different access schemes refer to the existence of a certain I. Xiao Lan visited the teaching building I after visiting the teaching building I in the two access methods
Different teaching buildings.
Tip: it is recommended to use computer programming to solve the problem.

[answer submission]
This is a question filled in with results. You just need to calculate the results and submit them. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

According to the meaning of the code:Walking 2 3 1 and walking 3 2 1 is a methodTherefore, set storage can be used in this way

Personal answer: 881012367360

Method 1: DFS + pruning

I use DFS directly and can’t run for a hundred years,…

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *It takes several seconds to get the result
 * @author HQL
 */
public class _ Loop count{
	static ArrayList[] list;
	static Map set = new HashMap();
	public static void main(String[] args) {
		list = new ArrayList[21 + 1];
		for (int i = 0; i < list.length; ++i)
			list[i] = new ArrayList();
		for (int i = 1; i <= 21; ++i)
			for (int j = i + 1; j <= 21; ++j)
				if (gcd(i, j) == 1) {
					list[i].add(j);
					list[j].add(i);
				}
		System.out.println(dfs(21, 1, 0));
	}
	public static long dfs(int total, int cur, int m) {
		if (total == 0)
			return 1;
		long res = 0;
		for (int p : list[cur]) {		
            //Store by bit, real cow, so you can store it
            //P is the target teaching building and M is the teaching building that has been visited at present(
            //Assuming M = = 12, the corresponding binary is 00001100, then M indicates that the teaching building has been visited (No. 2 and No. 3)
			if ((m & (1 << p)) != (1 << p)) {
				String pm = p + "-" + m;
				long r = 0;
				if (set.containsKey(pm)) { 		// prune
					r = set.get(pm);
				} else {
					if (p != 1 || p == 1 && total == 1)
						r = dfs(total - 1, p, m | (1 << p));
					set.put(pm, r);
				}
				res += r;
			}
		}
		return res;
	}
	public static int gcd(int a, int b) {
		if (a == 0 || b == 0)
			return 0;
		return a % b == 0 ? b : gcd(b, a % b);
	}
}

Method 2: state compression DP

can’t…

Question F: minimum weight

Time limit: 1.0s memory limit: 512.0mb total score of this question: 15 points

[problem description]
You have a balance. Now you have to design a set of weights so that you can use these weights to weigh any weight
The weight of a positive integer less than or equal to n.
How many weights should this set of weights contain at least?
Note that the weight can be placed on both sides of the balance.

[input format]
The input contains a positive integer n.

[output format]
Output an integer representing the answer.

[sample input]
7

[sample output]
3

[example description]
The weights of the three weights are 1, 4 and 6, which can weigh all the weights from 1 to 7.
1 = 1;
2 = 6 − 4 (put 6 on one side and 4 on the other side of the balance);
3 = 4 − 1;
4 = 4;
5 = 6 − 1;
6 = 6;
7 = 1 + 6;
It is impossible to weigh all weights from 1 to 7 with less than 3 weights.

[evaluation case scale and agreement]
For all evaluation cases, 1 ≤ n ≤ 1000000000.

There is this problem in acwing. Find the law

Question G: left child right brother

Time limit: 3.0s memory limit: 512.0mb total score of this question: 20 points

[problem description]
For a multi fork tree, we can convert it into a binary tree through the representation of “left child and right brother”. If we think that the children of each node are disordered, the resulting binary tree may not be unique. In other words, each node can choose any child node as the left child and connect the right brothers in any order.
Given a multi tree with n nodes, the nodes are numbered from 1 to N, where node 1 is the root, and the number of the parent node of each node is smaller than its own number. Please calculate the maximum height of the binary tree transformed by the representation of “left child and right brother”. Note: only the root node has a tree height of 0.
For example, the following multi tree:

Some topics of the 12th Blue Bridge Cup group a provincial competition in 2021

There may be three different “left children and right brothers” (only three are listed here, not all of them)
express:

Some topics of the 12th Blue Bridge Cup group a provincial competition in 2021

The height of the last one is the highest, which is 4.

[input format]
The first line of input contains an integer n.
The following n − 1 lines contain an integer, which represents the parent node number of nodes 2 to N in turn.

[output format]
Output an integer to represent the answer.

[sample input]
5
1
1
1
2

[sample output]
4

[evaluation case scale and agreement]
For 30% of the evaluation cases, 1 ≤ n ≤ 20;
For all evaluation cases, 1 ≤ n ≤ 100000.

Acwing also has this problem. DFS search is enough

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Main{
	static int N=100000+5;
	static ArrayList[] lists=new ArrayList[N];
	static long res=0L;
	public static void main(String[] args) throws IOException {
		init();
		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
		String[] line=reader.readLine().split(" ");
		int n=Integer.parseInt(line[0]);
		for(int i=2;i<=n;i++) {
			line=reader.readLine().split(" ");
			int x=Integer.parseInt(line[0]);
			lists[x].add(i);
		}
		dfs(1,0);
		System.out.println(res);
	}
	private static void dfs(int cur, int len) {
		// TODO Auto-generated method stub
		if (lists[cur].size()==0) {
			res=Math.max(res, len);
			return;
		}
		int tonbei=lists[cur].size();
		for(int i=0;i();
		}
	}
}

Source:[https://blog.csdn.net/weixin_45640260/article/details/116377115]