Blue Bridge Cup Java-B - 9th finals - E_ Version branch

Posted by bufhal on Tue, 08 Feb 2022 07:24:13 +0100

Blue Bridge Cup Java-B - 9th finals - E_ Version branch

Title:

Xiao Ming is responsible for maintaining a strange project of the company. The code of this project has been branching, but never merging. At present, the code of this project has N versions, numbered 1~N, of which version 1 is the original version. Except for version 1, the code of other versions happens to have a direct parent version; That is, these N versions form a tree structure with 1 as the root. The following figure shows a possible version tree:
  1
  /  \
2   3
|   /   \
5  4   6

Now Xiao Ming needs to often check whether version x is the ancestor of version y. Can you help Xiao Ming?

input

The first line contains two integers N and Q, representing the total number of versions and the total number of queries.
The following N-1 lines contain two integers u and V, representing version u and the direct parent version of version v.
Then line Q, each line containing two integers x and y, represents asking whether version x is the ancestor of version y.
For 30% of data, 1 < = n < = 1000 1 < = q < = 1000
For 100% data, 1 < = n < = 100000 1 < = q < = 100000

output

For each query, the output YES or NO represents whether x is the ancestor of y.
[sample input]
6 5
1 2
1 3
2 5
3 6
3 4
1 1
1 4
2 6
5 2
6 4
[sample output]
YES
YES
NO
NO
NO
Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Problem solving ideas:

1 - train of thought analysis:

Why put thinking analysis in front? Because most people may think of merging and searching sets when they see this problem, but I solved it with a string, which involves a little bit of the idea of merging and searching sets.
We need to get the parent node of each node. For the sample input, the diagram is the diagram presented at the beginning:
  1
  /  \
2   3
|   /   \
5  4   6
We can think like this: I know that 2 is the parent of 5 and 1 is the parent of 2. Because the node is written from top to bottom, when we get the data of 1 and 2, we can write 21 to node 2 (don't ask why, look down ~ ~), and then when we get 1 and 3, we can write 31 to node 3, After obtaining 2.5, we can write 521 to node 5. The source of this 521 is node 5 itself, plus the value of node 2. In this way, we have the final result
Node 1: 1
Node 2: 21
Node 3: 31
Node 5: 521
Node 4: 431
Node 6: 631
In this way, if we want to judge whether node 2 is the parent of node 4, we only need to query whether the string contains 2 in node 4. Obviously, it is not
Next, let's take a look at how to judge in the sample input
The node of 1 ﹐ 1 is 1 ﹐ contains 1 ﹐ returns YES
YES of node 1
The node of 2,6,6 is 631, and NO is returned without 2
The node of 5 2 , 2 is 21 , excluding 5 , returns NO
The node of 6, 4 and 4 is 431 without 6, and returns NO
Is it very simple? With the idea, the next step is the creation of variables and difficulty analysis. Here, in addition to D_ There will be a little problem when sorting out the console acquisition in the toy (still described in this chapter), so there is no difficulty.

1 - variable creation:

(1). First, according to the input of the console, we definitely need to shape the variables m and n. The acquisition method here is D_ The way of getting variables in sorting toys is consistent, so we need an additional string array variable str

(2). The string array variable strr used to get each row of data

(3). Integer variables chile and did for indexing (I don't seem to know how to spell Daddy)

(4). The integer array variable result used to store the result

(5). The string array variable arr used to store the parent of the node

2 - problem solving difficulties:

Fine node: console acquisition part
If we write as follows

package Blue Bridge Cup training set;

import java.util.Scanner;

public class Console acquisition {
	public static void main(String[] args)
	{
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		int x = in.nextInt();
		for(int i=0;i<x;i++)
		{
			System.out.println("row"+i);
			String str = in.nextLine();
		}
	}
}
Get instance from console:
4
row0
row1
123
row2
456
row3
789

There is no doubt that we will always input one less line. I still don't know the specific reason. I understand that this may be because x doesn't encounter carriage return when getting the integer value. When we hit carriage return, it runs directly into for, so we can get the value directly from line 1 instead of line 0
Solution: we directly let the process of obtaining the value of x also be obtained through one line. Because shaping cannot obtain one line of data, we added string variables. The specific implementation is as follows (this is also the detailed explanation of the third point of creating variables):

package Blue Bridge Cup training set;

import java.util.Scanner;

public class Console acquisition {
	public static void main(String[] args)
	{
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		String s1 = in.nextLine();
		int x = Integer.valueOf(s1);
		for(int i=0;i<x;i++)
		{
			System.out.println("row"+i);
			String str = in.nextLine();
		}
	}
}
Get instance from console:
4
row0
This is zero
row1
This is one
row2
This is two
row3
This is three

Code implementation:

public class e_Version branch {
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		String[] str = in.nextLine().split(" ");
		int n = Integer.valueOf(str[0]);
		int m = Integer.valueOf(str[1]);
		int[] result = new int[m];
		int child = 0;
		int did = 0;
		String[] arr = new String[n+1];
		getvalue(arr);
		String[] strr = new String[2];
		for(int i=0;i<n-1;i++)
		{
			strr = in.nextLine().split(" ");
			child = Integer.valueOf(strr[1]);
			did = Integer.valueOf(strr[0]);
			arr[child]=arr[child]+"->"+arr[did];
		}
		for(int i=0;i<m;i++)
		{
			strr = in.nextLine().split(" ");
			child = Integer.valueOf(strr[1]);
			did = Integer.valueOf(strr[0]);
			if(arr[child].contains(strr[0]))
			{
				result[i]=1;
			}else 
			{
				result[i]=0;
			}
		}
		for(int x:result)
		{
			if(x==1)
			{
				System.out.println("YES");
			}else 
			{
				System.out.println("NO");
			}
		}
	}
	public static void getvalue(String[] arr) 
	{
		for(int i=0;i<arr.length;i++)
		{
			arr[i]=String.valueOf(i);
		}
	}
}

The code may be a little cumbersome. If you have a better understanding, please give me some advice. ^^

Summary:

In fact, it's just a question of thinking. Don't think it's too difficult. I think it's simple for this question to appear in the fifth question. I hope to continue to refuel~~~

Topics: Java