Hiho1041 question about national day travel search

Source: Internet
Author: User
Tags bitset

Question 3: National Day travel time limit: 256 ms single point time limit: Ms memory limit: MB
Description

Xiao hi and Xiao Ho are planning to travel to country A during the National Day. Country A's cross-city traffic is quite distinctive: It has n cities (numbered 1-N) in total, and there are exactly n-1 roads between cities to form a tree-shaped road network. Little hi plans to start from the capital of country A (City 1) and traverse all cities by car, in addition, each road is exactly twice -- once and once -- so that the scenery on both sides of the road will not be missed.


What makes Xiao Hi worried is that his little friend Xiao ho hopes to travel to M cities in a specific order. For example, traveling to these three cities in the order of 3-2-5. (Specifically, it is required that the first arrival of City 3 is earlier than the first arrival of City 2, and the first arrival of City 2 is earlier than the first arrival of city 5 ).


Xiao HI wants to know if there is a self-driving order that meets the requirements of Xiao ho.

Input

The first line of the input is an integer T (1 <= T <= 20), representing the number of test data.

The first row of each group of data is an integer N (1 <= n <= 100), representing the number of cities.

Then there are two integers A and B (1 <= A, B <= N) in each row of N-1, indicating that there is a highway connection between AB.

The next row contains an integer m (1 <= m <= N)

The last line contains M integers, indicating the travel sequence that small Ho wants.

Output

Yes or no, indicating whether there is a self-driving order that meets the requirements of small ho.

Sample Input
271 21 32 42 53 63 733 7 271 21 32 42 53 63 733 2 7
Sample output
YESNO
Well, the first time I wrote a hiho question, a new OJ website.

This is Microsoft's prediction competition. The website is a bit lazy and uses previous questions to piece up a game. However, there are currently not many questions on the website.


This is the third question, which should be a deep search question. Ah, I am miserable, but I still failed to finish it on time. I finally made a reference to the online program and wrote it myself. It seems that the fire is not enough. No wonder you cannot enter the Google interview. Continue to work hard.


I am currently looking for a job and want to find a technical management job. I am worried that I cannot do what I want.

An unknown school is really inconvenient and unexpected. Once it passes through, it will feel reasonable. Everything is human, so let go of your mind.

Though Alexander, he still needs to keep learning.

I rejected the Netease operation test, and rejected the test of jumei. If you are looking for a prestigious school, you don't want to play with me. I really don't want to play with you. I am the perfect second to kill the interviewer's algorithm questions, even if you cannot find a small bug, you can reject it. I have served you!

Since my school is unknown, I will go to a small company that is not so well-known. With my enthusiasm, I believe I can!


This is a very skillful deep search that requires optimization. It is also very clever to use bit operations for optimization.

The main idea is to eliminate search, that is, to give two sets of advanced applications of algorithms for intersection.

The idea of finding an intersection is simple, that is, sorting, and then searching again.


Here is a feasible sequence set in the tree:

Acceleration: The getchild function is used to pre-process the subnodes under a node. After processing, you can determine whether a specific subnode exists within O (1.

Then eliminateall is the elimination of search ideas mentioned above, and the code is not long. However, there are many details that need to be carefully studied, and the key points in the Code are marked out.


# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h >#include <stack >#include <queue >#include <set> # include <map >#include <bitset> using namespace STD; const int max_n = 101; vector <int> graadj [max_n]; // adjacent table int arr [max_n]; int gramat [max_n] [max_n]; // matrix bitset <max_n> haschild [max_n]; void getchild (INT u = 1, int par =-1) {haschild [U] [u] = 1; // is your child? Integrate yourself with your children. For (INT I = 0; I <(INT) graadj [u]. size (); I ++) {int v = graadj [u] [I]; If (V = par) continue; // prevents loops, do not go back to Father's Day. getchild (v, U); haschild [u] | = haschild [v]; // use bitset acceleration to determine the number of children on a node} // use the global variables gramat and graadjbool eliminatall (int A [], Int & ID, int m, int u = 1, int par =-1) {If (ID <M & A [ID] = u) ID ++; If (ID = m) return true; while (ID <m) // key point, which can be cyclically searched for subnodes {int nexta = A [ID]; int curindex = ID; For (INT I = 0; I <(INT) graadj [u]. size (); I ++) {int v = graadj [u] [I]; If (V = par) continue; // exclude the parent node, prevent loop if (haschild [v] [nexta] & gramat [u] [v]) {gramat [u] [v] = 0; // key point: bridge breaking, you do not need to repeat the search path if (eliminatall (A, ID, M, V, u) return true ;}// key point: to prevent infinite loops, exit if (ID = curindex) break when there is no answer; // a corresponding Vertex a [ID] is not found, and return the previous layer} return false;} int main () {int t, n, a, B, M; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); for (INT I = 1; I <= N; I ++) {graadj [I]. clear (); haschild [I]. reset ();} memset (gramat, 0, sizeof (gramat); For (INT I = 1; I <n; I ++) {scanf ("% d", & A, & B); graadj [A]. push_back (B); graadj [B]. push_back (a); gramat [a] [B] = gramat [B] [a] = 1;} scanf ("% d", & M ); for (INT I = 0; I <m; I ++) {scanf ("% d", arr + I) ;}getchild (); int id = 0; if (eliminatall (ARR, ID, m) puts ("yes"); else puts ("no") ;}return 0 ;}



Hiho1041 question about national day travel search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.