#1041: National Day Travel time limit: 1000ms single point time limit: 1000ms memory limit: 256MB description
Little Hi and Little ho prepare to travel to country a during the national day. The Inter-city traffic of country A is more distinctive: it has n cities (numbered 1-n); there is a n-1 highway connecting the city to form a tree-shaped road network. The little Hi plan starts from the capital of Country A (city 1th), driving through all the cities, and passing through every highway two times-----so that the scenery on both sides of the road will not be missed.
The trouble with little hi is that his little buddy, Little Ho, wants to travel in a certain order in one of the M cities. For example, travel through the 3 cities in the order of 3-2-5. (specifically required: the first time to reach City 3rd than the first time to reach the city of 2nd early, and the first time to reach the city of 2nd than the first time to reach the city of 5th early).
Little hi wants to know if there is a self-driving order to meet the requirements of small ho. input
The first line of input is an integer T (1<=t<=20), which represents the number of test data.
The first row of each set of data is an integer n (1 <= n <= 100) representing the number of cities.
The n-1 line is followed by a row of two integers A and B (1 <= A, b <= N), indicating a road connection between AB.
The following line contains an integer m (1 <= m <= N)
The last line contains m integers, indicating the order in which small Ho wishes to travel. Output
Yes or no, indicates whether there is a self-driving order to meet the requirements of small ho. Sample input
2
7
1 2
1 3
2 4
2 5
3 6
3 7
3
3 7 2
7
1 2
1 3
2 4
2 5
3 6
3 7
3
3 2 7
Sample output
YES
NO
Test Instructions: A non-loop tree, starting with 1, determines whether the specified points can be traversed sequentially, and only two times per side.
idea: 1 is the starting point, the Bitset container is saved with a node as the root node, the subtree can reach all the nodes, and then from the first order point to find, DFS arrival process will walk through the road "eliminate", indicating "return" when the time has passed, so the search for the next order point will not go these ways, So you can tell if you can go through all the order points.
# include <iostream> # include <cstdio> # include <cstring> # include <vector> # include <bitse
T> # include <algorithm> # define MAXN 103 using namespace std;
bitset<maxn>f[maxn];
vector<int>v[maxn];
int A[MAXN], R[MAXN][MAXN], CNT, flag, T, N, M;;
void Son_tree (int cur, int pre)//the point at which each subtree can reach {f[cur][cur] = 1;
for (int i=0; i<v[cur].size (); ++i) {int dot = v[cur][i];
if (dot = = pre) continue;
Son_tree (dot, cur);
F[cur] |= F[dot];
}} void Dfs (int cur, int pre) {if (CNT < m && cur = = a[cnt]) ++cnt;
if (cnt = = m) {flag = 1;
Return
The while (CNT < m)//cnt refers to the array subscript of the sequence point that is being performed {int next = a[cnt];
int now = CNT;
for (int i=0; i<v[cur].size (); ++i) {int dot = v[cur][i];
if (dot = = pre) continue;
if (F[dot][next] && R[cur][dot])//The subtree can reach the target point and the path is unobstructed (not past).
{ R[cur][dot] = 0;
DFS (dot, cur);
Break
}} if (cnt = = now) break;
}} int main () {scanf ("%d", &t);
while (t--) {scanf ("%d", &n);
memset (r, 0, sizeof (r));
for (int i=1; i<=n; ++i) {v[i].clear ();
F[i].reset ();
} for (int i=0; i<n-1; ++i) {int x, y;
scanf ("%d%d", &x,&y);
R[x][y] = r[y][x] = 1;
V[x].push_back (y);
V[y].push_back (x);
} scanf ("%d", &m);
for (int i=0; i<m; ++i) scanf ("%d", &a[i]);
CNT = flag = 0;
Son_tree (1,-1);
DFS (1,-1);
if (flag) puts ("YES");
Else puts ("NO");
} return 0;
}