Problem A. Fractional addition

Title description

Topic link

Find 2-a+2-b, where a and b are positive integers, and the result should be expressed as the simplest fraction.

Input format

The first row is the number of test data groups T (1<=T<=400). Note that any two sets of test data are independent of each other.
Each group of test data is a row, including two integers a and b (2<=a, b<=20).

Output format

For each group of test data, output the results in one line, with the numerator and denominator separated by "/".

sample input

 two 2 4 3 2

sample output

 5/16 3/8

Analysis of ideas

  • It may not be taken into account that it is reduced to the simplest fraction. At first, I thought it was impossible to simplify! The following is an even number, and the above is an even number+1 must be an odd number. How can we simplify it It was later discovered that the above may also be 1+1, that is, a=b

AC code

 package bupt; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0;  i < num; i++) { int a = scanner.nextInt(); int b =scanner.nextInt(); int max= Math.max(a, b); int min = Math.min(a, b); int down = (int) Math.pow(2, max); int up = (int) Math.pow(2, max-min); up = up + 1; while (up%2==0){ up/=2; down/=2; }//For example, 1/4+1/4 needs to be reduced to the simplest fraction System.out.println(up + "/" + down); } } }

Problem B. Minimum Heap

Title description

Topic link

Given a weighted binary tree, please determine whether it is a minimum heap.
A binary tree is a minimum heap, if and only if for any node on the tree, its weight value is less than or equal to the ownership value in the subtree with it as the root.

Input format

The first line of input data is an integer T (1<=T<=100), representing the number of groups of test data.
For each set of test data:
The first line is an integer N (1<=N<=100), representing the number of nodes in the tree.
The next line contains N positive integers, and the ith integer valuei (1<=valuei<=1000) represents the weight value of the point numbered i.
Next, in N-1 row, there are two integers u and v (1<=u, v<=N, u!=v) in each row, indicating that node u is the parent node of node v.
The test data ensures that the given tree must be a binary tree, and node 1 is the root node of the tree.

Output format

For each group of test data, output Yes if the given tree is a minimum heap, or output No.

sample input

 three one ten three 10 5 3 1 2 1 3 five 1 2 3 4 5 1 3 1 2 2 4 2 5

sample output

 Yes No Yes

Analysis of ideas

  • This problem is because only to determine whether it is the minimum heap, it does not need to represent the tree, and only needs to store the weight of each vertex. Then judge whether the starting point weight value is less than the ending point weight value while inputting and changing.
  • The general OJ questions have the following representations for common trees Father representation (There is only one father for a node in the naming tree, which is called the parental representation. I took it.), Child representation Child brother representation
  • For binary tree representation.
Father representation

It is a one-dimensional array. The element serial number of the array represents the vertex number, and the value represents the parent node number of the vertex.

AC code

 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0;  i < num; i++) { int N = scanner.nextInt();// the number of vertexes; int[] vetexs = new int[101]; for (int j = 1;  j <= N; j++) { //input weight of vertexes vetexs[j] = scanner.nextInt(); } boolean flag = true; for (int j = 0;  j < N-1; j++) { //input relationship of edges int parent = scanner.nextInt(); int child = scanner.nextInt(); if (vetexs[parent] > vetexs[child]) { if (flag) { flag = false; } } } if (flag) { System.out.println("Yes"); }else { System.out.println("No"); } }     scanner.close(); } }

Problem C. Process Management

Title description

Topic link

In the operating system, process management is a very important work. Each process has a unique process ID (PID). Each process can start a child process. At this time, we call it the parent process of its child process. In addition to the process with PID 0, each process has one and only one parent process. In this task, you need to maintain the three basic operations of the operating system in real time:

FORK PID1 PID2: The process identified as PID1 started a child process identified as PID2.
KILL PID: End the process identified as PID. Please note that all PID sub processes will end at the same time. If the PID is a nonexistent or ended process, no action will be taken.
QUERY PID: Query whether the process identified as PID still exists.
In the initial state, the system only starts the process with PID 0, and in any case
The journey will not end.

Input format

The first line of input is an integer T (T<=50), representing the number of data groups input.
The first line of each group of test data is an integer N (1<=N<=100), representing the number of operations.
Each next N lines, each line gives each operation according to the above description. Input to ensure that all processes have different PID, and a process will not be restarted after completion. All PID are integers between [1100].

Output format

For each QUERY query, the output is Yes if the process exists and No if it does not exist

sample input

 two five FORK 0 1 QUERY 1 KILL 1 QUERY 1 QUERY 2 one QUERY 0

sample output

 Yes No No Yes
Reference link: BOJ Problem Solving Report-268 Process Management

Analysis of ideas

  • KILL is the difficulty and pitfall of this question. KILL PID, The child process of the child process also needs to be KILL. therefore One way Each process has a member variable of the child process list. When FORK, the current process is chained along the direction of the parent process to the list of child processes of each parent process along the way. (I do this) Another method is to delete recursively when KILL (can't understand!)
  • such as FORK 0 1 Because Java cannot format input, only output See details below.
  • The list remove method. The remove (int index) and remove (Object o) functions are completely different. The first remove is easy to understand. It is deleted according to the index. The second is to Object o And List<Object>list Use the equals function for loop matching, so you must override the equals function. Object. equals (Object o) This function also has many considerations. Let me list two articles:

(Loop through to delete an element, and you can also use listIterator. remove() to delete the current element)

next() And nextLine() Input difference

nextInt() Similar functions and next() Consistent characteristics.

  • next() Valid input (carriage return, line feed, TAB, and space are all invalid inputs) must be made before the input can be ended (otherwise, it is always in the status of waiting for input), and then the current input (carriage return, line feed, TAB, and space) can be ended using the ending symbol
  • nextLine() : There is no constraint above, but there is only one ending symbol, namely carriage return, so you can enter a string containing spaces. But this function will receive carriage return.

Although there is one ending symbol for these two functions Enter n , but not the same:

  • Next () After entering, \n Will not disappear (either filtered by the next next () or NextLine() receive ),
  • The nextLine() will disappear directly after entering, so the next nextLine() Can receive input normally, but not enter

For example, what I entered first five In fact, the input is 5\n this \n As nextInt() Terminator, but \n It will not disappear if nextLine() Will be received.

Therefore, before the operation conditions are input circularly, use the following line to receive the carriage return that does not disappear, and then nextLine() Receives an action string with spaces. Finally, use split ("") to separate the string.

 scanner.nextLine();// Receive enter symbol

AC code

 import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Scanner; public class Process { public static Scanner scanner; public List<Aprocess> list = new ArrayList<Aprocess>(); public static void main(String[] args) { scanner = new Scanner(System.in); int num  = scanner.nextInt(); for (int i = 0;  i < num; i++) { Process process = new Process(); process.handle();         } } public void handle(){ int N = scanner.nextInt(); list.add(new Aprocess("0","0")); scanner.nextLine();// Receive enter symbol for (int j = 0;  j < N; j++) { String operation = scanner.nextLine(); String[] array = operation.split(" "); if (array[0].equals("QUERY")) { int index = queryPidInList(array[1]); if (index != - 1) { System.out.println("Yes"); }else{ System.out.println("No"); } }else if (array[0].equals("FORK")) { forkPidInList(array[2],array[1]); }else if (array[0].equals("KILL")) { killPidInList(array[1]); } } } public int queryPidInList(String pid){ for (ListIterator<Aprocess> iterator = list.listIterator();  iterator.hasNext();)  { Aprocess aprocess = (Aprocess) iterator.next(); if (pid.equals(aprocess.PID)) { return iterator.nextIndex() -1; } } return -1; } public void forkPidInList(String pid,String parent){ int parentIndex = queryPidInList(parent); if (parentIndex != - 1) { Aprocess newAprocess = new Aprocess(pid, parent); list.add(newAprocess); while(true){ if (list.get(parentIndex). PID.equals("0")) { break; } list.get(parentIndex).childPid.add(newAprocess); parentIndex = queryPidInList(list.get(parentIndex).parentPid); } } } public void killPidInList(String pid){ if (! pid.equals("0")) { int pidIndex = queryPidInList(pid); if (pidIndex!=- 1) { List<Aprocess> lStrings = list.get(pidIndex).childPid; for (Iterator<Aprocess> iterator = lStrings.iterator();  iterator.hasNext();)  { Aprocess aprocess = (Aprocess) iterator.next(); list.remove(aprocess); } list.remove(pidIndex); }         } } class Aprocess{ String PID; String parentPid; List<Aprocess> childPid = new ArrayList<Aprocess>(); public Aprocess() { } public Aprocess(String pid){ this.PID = pid; this.parentPid = "0"; } public Aprocess(String pid,String parent){ this.PID = pid; this.parentPid = parent; } @Override public boolean equals(Object obj) { if (this. PID.equals(((Aprocess)obj). PID)) { return true; } return false; } } }

Problem D. Network transmission

Title description

Topic link

Efficient interconnection and intelligent transmission of networks are important measures to improve the mapping efficiency of massive user service requests. In this task, you need to use the least transmission time to send a specific data source to a specified network node.
The given network contains N nodes (numbered from 1 to N), of which node 1 is the data source. There are M undirected edges (u, v, w) in the network, which means that a transmission line connects node u and node v, and the average time of data passing through this transmission line is w. Due to the limitation of the transmission mechanism, when a node receives data, it can only select a node interconnected with it and forward the data to that node. Node 1 will only send data once during initialization, but it can act as a forwarding node during transmission.
There are k target nodes in the network. You need to calculate the minimum time required for the data to be transmitted from Node 1 to all K nodes. Note that the target node can be transmitted in any order, and data can also pass through the same node multiple times.

Input format

The first line of input data is an integer T (T<=5), representing the number of groups of test data.
For each set of test data:
The first line is three positive integers N, M, K (2<=N<=1000, 1<=M<=N (N-1)/2, K<=10), representing the number of nodes, edges, and target nodes, respectively.
Next, there are three integers u, v, w (1<=u, v<=N, 0<=w<=1000,u!=v)。 Each transmission line is given as described above. At most one edge can be connected between any two network nodes.
The last line is K integers, giving the numbers of all target nodes. The numbers of all target nodes are between 2 and N.

Output format

For each group of test data, the shortest time for output data to be transmitted to all K target nodes.

sample input

 two 3 2 2 1 3 1 1 2 3 2 3 6 6 4 1 5 1 5 6 2 2 1 20 2 3 5 3 4 5 6 3 1 2 3 4 6

sample output

 five nineteen

Thinking analysis

I used this question for half a day The main problem is that the debugging is too poor.

  • Understand the question correctly. Node 1 will only send data once. So the shortest path is actually a Full arrangement In the inside, find the appropriate path.
  • To find the shortest path, the Freudian algorithm is said to timeout, so the Kth time Dijkstra algorithm is used
  • At the beginning, I used the set of triples to represent the graph. Triples are start end weight (weight value), which will result in Runtime error problems (Memory overrun is not an array or collection fetching overrun, but a memory overrun). Finally, the adjacency matrix is used to represent the graph.
Java full arrangement

Done recursively.

AC code

 import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0;  i < num; i++) { Main network = new Main(); network.handle(); } } public void handle() { MGraph mGraph = new MGraph(); mGraph.vertexNum = scanner.nextInt(); mGraph.edgesNum = scanner.nextInt(); mGraph.tartgetVertexNum = scanner.nextInt(); mGraph.initEmptyDist(); for (int i = 0;  i < mGraph.edgesNum; i++) { int start = scanner.nextInt(); int end = scanner.nextInt(); int weight = scanner.nextInt(); mGraph.edges[start][end] = weight; mGraph.edges[end][start] = weight; mGraph.dist[start][end] = weight; mGraph.dist[end][start] = weight; } for (int i = 0;  i < mGraph.tartgetVertexNum; i++) { mGraph.tartgetVetexes.add(scanner.nextInt()); } mGraph.Dijkstra(1); // output mGraph.calculateMinToTarger(); } class MGraph { int vertexNum; int edgesNum; int tartgetVertexNum; int[][] edges; List<Integer> tartgetVetexes = new ArrayList<Integer>(); int[][] dist; List<Integer> T = new ArrayList<Integer>(); int rootLength = -1; public void initEmptyDist() { dist = new int[vertexNum + 1][vertexNum + 1]; edges = new int[vertexNum + 1][vertexNum + 1]; for (int i = 1;  i <= vertexNum; i++) { for (int j = 1;  j <= vertexNum; j++) { dist[i][j] = -1; edges[i][j] = -1; } } } public void Dijkstra(int origin) { // init vertexes data for (int i = 1;  i <= vertexNum; i++) { if (i !=  origin) { T.add(i); } } while (T.size() !=  0) { int selectVertex = -1; int selectVertexInT = 0; int min = -1;// position is not corrent in the past time for (int i = 0;  i < T.size();  i++) { if (dist[origin][T.get(i)] != - 1 && (dist[origin][T.get(i)] < min || min == -1)) { min = dist[origin][T.get(i)]; selectVertex = T.get(i); selectVertexInT = i; } } // use this vertex to modify others dists for (int i = 0;  i < T.size();  i++) { int currentTargetVertex = T.get(i); int query = query(selectVertex,  currentTargetVertex); if (query != - 1) { int modified = min + query(selectVertex,  currentTargetVertex); if (dist[origin][currentTargetVertex] == -1 || dist[origin][currentTargetVertex] > modified) { dist[origin][currentTargetVertex] = modified; } } } T.remove(selectVertexInT); } } public int query(int start, int end) { int result = -1; if (edges[start][end]!= - 1) { result = edges[start][end]; } return result; } public void calculateMinToTarger() { for (int i = 0;  i < tartgetVetexes.size();  i++) { Dijkstra(tartgetVetexes.get(i)); } permutation(tartgetVetexes, 0,  tartgetVertexNum - 1); System.out.println(rootLength); } public void permutation(List<Integer> list, int from, int to) { if (to < from) return; if (from == to) { int tempRootLength = dist[1][list.get(0)]; for (int i = 1;  i < list.size();  i++) { tempRootLength += dist[list.get(i - 1)][list.get(i)]; } if (rootLength == -1 || rootLength > tempRootLength) { rootLength = tempRootLength; } } else { for (int i = from;  i <= to; i++) { swap(list, i, from); permutation(list, from + 1, to); swap(list, from, i); } } } public void swap(List<Integer> s, int i, int j) { int tmp = s.get(i); s.set(i, s.get(j)); s.set(j, tmp); } } class Edge{ public int end; public int weight; public Edge() { //TODO auto generated constructor stub } Edge(int end,int weight){ this.end = end; this.weight = weight; } } }

Code for runtime error problems

 import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0;  i < num; i++) { Main network = new Main(); network.handle(); } } public void handle() { MGraph mGraph = new MGraph(); mGraph.vertexNum = scanner.nextInt(); mGraph.edgesNum = scanner.nextInt(); mGraph.tartgetVertexNum = scanner.nextInt(); mGraph.initEmptyDist(); for (int i = 0;  i < mGraph.edgesNum; i++) { int start = scanner.nextInt(); int end = scanner.nextInt(); int weight = scanner.nextInt(); mGraph.SGroups.add(new ThreeGronp(start, end, weight)); mGraph.dist[start][end] = weight; mGraph.dist[end][start] = weight; } for (int i = 0;  i < mGraph.tartgetVertexNum; i++) { mGraph.tartgetVetexes.add(scanner.nextInt()); } mGraph.Dijkstra(1); // output mGraph.calculateMinToTarger(); } class MGraph { int vertexNum; int edgesNum; int tartgetVertexNum; List<ThreeGronp> SGroups = new ArrayList<ThreeGronp>();//  Information of // edges List<Integer> tartgetVetexes = new ArrayList<Integer>(); int[][] dist; List<Integer> T = new ArrayList<Integer>(); int rootLength = -1; public void initEmptyDist() { dist = new int[vertexNum + 10][vertexNum + 10]; for (int i = 1;  i <= vertexNum; i++) { for (int j = 1;  j <= vertexNum; j++) { dist[i][j] = -1; } } } public void Dijkstra(int origin) { // init vertexes data for (int i = 1;  i <= vertexNum; i++) { if (i !=  origin) { T.add(i); } } while (T.size() !=  0) { int selectVertex = -1; int selectVertexInT = 0; int min = -1;// position is not corrent in the past time for (int i = 0;  i < T.size();  i++) { if (dist[origin][T.get(i)] != - 1 && (dist[origin][T.get(i)] < min || min == -1)) { min = dist[origin][T.get(i)]; selectVertex = T.get(i); selectVertexInT = i; } } // use this vertex to modify others dists for (int i = 0;  i < T.size();  i++) { int currentTargetVertex = T.get(i); int query = query(selectVertex,  currentTargetVertex); if (query != - 1) { int modified = min + query(selectVertex,  currentTargetVertex); if (dist[origin][currentTargetVertex] == -1 || dist[origin][currentTargetVertex] > modified) { dist[origin][currentTargetVertex] = modified; } } } T.remove(selectVertexInT); } } public int query(int start, int end) { for (Iterator<ThreeGronp> iterator = SGroups.iterator();  iterator.hasNext();)  { ThreeGronp threeGronp = (ThreeGronp) iterator.next(); if ((threeGronp.start == start && threeGronp.end == end) || (threeGronp.start == end && threeGronp.end == start)) { return threeGronp.weight; } } return -1; } public void calculateMinToTarger() { for (int i = 0;  i < tartgetVetexes.size();  i++) { Dijkstra(tartgetVetexes.get(i)); } permutation(tartgetVetexes, 0,  tartgetVertexNum - 1); System.out.println(rootLength); } public void permutation(List<Integer> list, int from, int to) { if (to < from) return; if (from == to) { int tempRootLength = dist[1][list.get(0)]; for (int i = 1;  i < list.size();  i++) { tempRootLength += dist[list.get(i - 1)][list.get(i)]; } if (rootLength == -1 || rootLength > tempRootLength) { rootLength = tempRootLength; } } else { for (int i = from;  i <= to; i++) { swap(list, i, from); permutation(list, from + 1, to); swap(list, from, i); } } } public void swap(List<Integer> s, int i, int j) { int tmp = s.get(i); s.set(i, s.get(j)); s.set(j, tmp); } } class ThreeGronp { int start; int end; int weight; public ThreeGronp(int start, int end, int weight) { this.start = start; this.end = end; this.weight = weight; } } }
C++Version Guide https://blog.csdn.net/TQCAI666/article/details/86765736
Last modification: March 1, 2019
Do you like my article?
Don't forget to praise or appreciate, let me know that you accompany me on the way of creation.