#include<bits/stdc++.h>
using namespace std;
int n , m , st [200005][21] , dp [200005] , s [200005] , t [200005] , vis [200005] , ff;
vector < int > gr [200005] , grr [200005];
void dfs ( int x , int p )
{
dp [x] = dp [p] + 1;
st [x][0] = p;
for ( auto u : gr [x] )
{
if ( u == p ) continue;
dfs ( u , x );
}
}
void dfss ( int x )
{
vis [x] = 1;
for ( auto u : grr [x] )
{
if ( vis [u] == 1 )
{
ff = 1;
continue;
}
else if ( vis [u] == 2 ) continue;
else dfss ( u );
}
vis [x] = 2;
}
int lca ( int x , int y )
{
if ( dp [x] > dp [y] ) swap ( x , y );
int dif = dp [y] - dp [x];
for ( int i = 0 ; i <= 20 ; i ++ )
{
if ( ( dif & ( 1 << i ) ) ) y = st [y][i];
}
if ( x == y ) return x;
for ( int i = 20 ; i >= 0 ; i -- )
{
if ( st [x][i] != st [y][i] )
{
x = st [x][i];
y = st [y][i];
}
}
return st [x][0];
}
int inbet ( int x , int y , int z )
{
if ( dp [z] > dp [x] && dp [z] > dp [y] ) return 0;
int aa = lca ( x , y );
if ( dp [aa] > dp [z] ) return 0;
int a = lca ( x , z );
int b = lca ( y , z );
if ( a == z || b == z ) return 1;
return 0;
}
int main()
{
int q;
cin >> q;
while ( q -- )
{
ff = 0;
cin >> n;
for ( int i = 1 ; i <= n ; i ++ )
{
gr [i] . clear ();
grr [i] . clear ();
dp [i] = 0;
vis [i] = 0;
for ( int j = 0 ; j <= 20 ; j ++ ) st [i][j] = 0;
}
for ( int i = 0 ; i < n - 1 ; i ++ )
{
int x , y;
cin >> x >> y;
gr [x] . push_back ( y );
gr [y] . push_back ( x );
}
dfs ( 1 , 0 );
for ( int i = 1 ; i <= 20 ; i ++ )
{
for ( int j = 1 ; j <= n ; j ++ ) st [j][i] = st [ st [j][ i - 1 ] ][ i - 1 ];
}
int fff = 0;
cin >> m;
for ( int i = 1 ; i <= m ; i ++ ) cin >> s [i] >> t [i];
for ( int i = 1 ; i <= m ; i ++ )
{
for ( int j = 1 ; j <= m ; j ++ )
{
if ( i == j ) continue;
if ( inbet ( s [i] , t [i] , s [j] ) )
{
if ( inbet ( s [i] , t [i] , t [j] ) ) fff = 1;
grr [i] . push_back ( j );
}
if ( inbet ( s [i] , t [i] , t [j] ) )
{
if ( inbet ( s [i] , t [i] , s [j] ) ) fff = 1;
grr [j] . push_back ( i );
}
}
}
if ( fff )
{
cout << "No" << endl;
continue;
}
for ( int i = 1 ; i <= m ; i ++ )
{
if ( vis [i] == 0 ) dfss ( i );
}
if ( ff ) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
/*
1
4
1 2
2 3
3 4
2
1 3
4 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
36 ms |
9624 KB |
Output is correct |
5 |
Correct |
50 ms |
9700 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
27 ms |
9804 KB |
Output is correct |
9 |
Correct |
616 ms |
13896 KB |
Output is correct |
10 |
Correct |
160 ms |
32548 KB |
Output is correct |
11 |
Correct |
26 ms |
9840 KB |
Output is correct |
12 |
Correct |
382 ms |
10868 KB |
Output is correct |
13 |
Execution timed out |
5047 ms |
35832 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
10 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
8 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9724 KB |
Output is correct |
13 |
Correct |
6 ms |
9756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
10 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
8 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9724 KB |
Output is correct |
13 |
Correct |
6 ms |
9756 KB |
Output is correct |
14 |
Correct |
6 ms |
9616 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
8 ms |
9740 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
7 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9660 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
9 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
9684 KB |
Output is correct |
27 |
Correct |
8 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
10 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
8 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9724 KB |
Output is correct |
13 |
Correct |
6 ms |
9756 KB |
Output is correct |
14 |
Correct |
6 ms |
9616 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
8 ms |
9740 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
7 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9660 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
9 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
9684 KB |
Output is correct |
27 |
Correct |
8 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9684 KB |
Output is correct |
29 |
Correct |
27 ms |
9820 KB |
Output is correct |
30 |
Correct |
38 ms |
9684 KB |
Output is correct |
31 |
Correct |
25 ms |
9720 KB |
Output is correct |
32 |
Correct |
17 ms |
9712 KB |
Output is correct |
33 |
Correct |
8 ms |
9684 KB |
Output is correct |
34 |
Correct |
82 ms |
9788 KB |
Output is correct |
35 |
Correct |
107 ms |
9968 KB |
Output is correct |
36 |
Correct |
63 ms |
9888 KB |
Output is correct |
37 |
Correct |
32 ms |
9904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
10 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
8 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9724 KB |
Output is correct |
13 |
Correct |
6 ms |
9756 KB |
Output is correct |
14 |
Correct |
6 ms |
9616 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
8 ms |
9740 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
7 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9660 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
9 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
9684 KB |
Output is correct |
27 |
Correct |
8 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9684 KB |
Output is correct |
29 |
Correct |
27 ms |
9820 KB |
Output is correct |
30 |
Correct |
38 ms |
9684 KB |
Output is correct |
31 |
Correct |
25 ms |
9720 KB |
Output is correct |
32 |
Correct |
17 ms |
9712 KB |
Output is correct |
33 |
Correct |
8 ms |
9684 KB |
Output is correct |
34 |
Correct |
82 ms |
9788 KB |
Output is correct |
35 |
Correct |
107 ms |
9968 KB |
Output is correct |
36 |
Correct |
63 ms |
9888 KB |
Output is correct |
37 |
Correct |
32 ms |
9904 KB |
Output is correct |
38 |
Correct |
594 ms |
13860 KB |
Output is correct |
39 |
Correct |
150 ms |
32748 KB |
Output is correct |
40 |
Correct |
1518 ms |
12872 KB |
Output is correct |
41 |
Correct |
1833 ms |
11820 KB |
Output is correct |
42 |
Correct |
1919 ms |
12988 KB |
Output is correct |
43 |
Correct |
66 ms |
11688 KB |
Output is correct |
44 |
Correct |
1736 ms |
10264 KB |
Output is correct |
45 |
Correct |
254 ms |
25660 KB |
Output is correct |
46 |
Correct |
226 ms |
25864 KB |
Output is correct |
47 |
Correct |
187 ms |
28464 KB |
Output is correct |
48 |
Correct |
186 ms |
28516 KB |
Output is correct |
49 |
Correct |
185 ms |
25856 KB |
Output is correct |
50 |
Correct |
243 ms |
25844 KB |
Output is correct |
51 |
Correct |
104 ms |
26592 KB |
Output is correct |
52 |
Correct |
104 ms |
26540 KB |
Output is correct |
53 |
Correct |
1465 ms |
11008 KB |
Output is correct |
54 |
Correct |
215 ms |
25772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
30 ms |
9644 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9712 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
65 ms |
9788 KB |
Output is correct |
13 |
Correct |
716 ms |
10308 KB |
Output is correct |
14 |
Correct |
355 ms |
10476 KB |
Output is correct |
15 |
Correct |
593 ms |
10372 KB |
Output is correct |
16 |
Execution timed out |
5057 ms |
26168 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
36 ms |
9624 KB |
Output is correct |
5 |
Correct |
50 ms |
9700 KB |
Output is correct |
6 |
Correct |
8 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
27 ms |
9804 KB |
Output is correct |
9 |
Correct |
616 ms |
13896 KB |
Output is correct |
10 |
Correct |
160 ms |
32548 KB |
Output is correct |
11 |
Correct |
26 ms |
9840 KB |
Output is correct |
12 |
Correct |
382 ms |
10868 KB |
Output is correct |
13 |
Execution timed out |
5047 ms |
35832 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |