#include <bits/stdc++.h>
using namespace std;
struct prisoner {
int b, w;
};
const int MAX_N = 1.2e5;
const int MAX_M = MAX_N;
const int MAX_LOG_N = 16;
const int MAX_V = MAX_M + MAX_N * (MAX_LOG_N + 1) + 1;
int parent[MAX_LOG_N + 1][MAX_N + 1], depth[MAX_N + 1], vert[MAX_M], vert1[MAX_LOG_N + 1][MAX_N + 1], vert2[MAX_LOG_N + 1][MAX_N + 1], in[MAX_V];
vector <int> edges[MAX_N + 1], g[MAX_V];
prisoner prisoners[MAX_M + 1];
void dfs( int u, int p ) {
depth[u] = depth[p] + 1;
parent[0][u] = p;
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfs( v, u );
}
}
int findParent( int v, int d ) {
if ( d < 0 )
return 0;
for ( int l = MAX_LOG_N; l >= 0; l-- ) {
if ( d >= (1 << l) ) {
d -= (1 << l);
v = parent[l][v];
}
}
return v;
}
int main() {
int t;
cin >> t;
while ( t-- ) {
int n, m;
cin >> n;
for ( int v = 1; v <= n; v++ )
edges[v].clear();
for ( int i = 0; i < n - 1; i++ ) {
int u, v;
cin >> u >> v;
edges[u].push_back( v );
edges[v].push_back( u );
}
cin >> m;
for ( int i = 1; i <= m; i++ )
cin >> prisoners[i].b >> prisoners[i].w;
dfs( 1, 0 );
int V = 1;
for ( int i = 1; i <= m; i++ )
vert[i] = V++;
for ( int l = 0; l <= log2( n ); l++ ) {
for ( int v = 1; v <= n; v++ ) {
vert1[l][v] = V++;
vert2[l][v] = V++;
}
}
for ( int v = 0; v < V; v++ )
g[v].clear(), in[v] = 0;
for ( int l = 1; l <= log2( n ); l++ ) {
for ( int v = 1; v <= n; v++ ) {
parent[l][v] = parent[l - 1][parent[l - 1][v]];
g[vert1[l - 1][v]].push_back( vert1[l][v] );
g[vert1[l - 1][parent[l - 1][v]]].push_back( vert1[l][v] );
g[vert2[l][v]].push_back( vert2[l - 1][v] );
g[vert2[l][v]].push_back( vert2[l - 1][parent[l - 1][v]] );
}
}
for ( int i = 1; i <= m; i++ ) {
g[vert[i]].push_back( vert1[0][prisoners[i].b] );
g[vert2[0][prisoners[i].w]].push_back( vert[i] );
}
for ( int i = 1; i <= m; i++ ) {
int u, v;
u = prisoners[i].b, v = prisoners[i].w;
if ( findParent( v, depth[v] - depth[u] ) == u )
u = findParent( v, depth[v] - depth[u] - 1 );
else
u = parent[0][u];
if ( depth[u] < depth[v] )
swap( u, v );
for ( int l = log2( n ); l >= 0; l-- ) {
if ( depth[parent[l][u]] >= depth[v] ) {
g[vert1[l][u]].push_back( vert[i] );
u = parent[l][u];
}
}
for ( int l = log2( n ); l >= 0; l-- ) {
if ( parent[l][u] != parent[l][v] ) {
g[vert1[l][u]].push_back( vert[i] );
g[vert1[l][v]].push_back( vert[i] );
u = parent[l][u];
v = parent[l][v];
}
}
g[vert1[0][u]].push_back( vert[i] );
u = prisoners[i].b, v = prisoners[i].w;
if ( findParent( u, depth[u] - depth[v] ) == v )
v = findParent( u, depth[u] - depth[v] - 1 );
else
v = parent[0][v];
if ( depth[u] < depth[v] )
swap( u, v );
for ( int l = MAX_LOG_N; l >= 0; l-- ) {
if ( depth[parent[l][u]] >= depth[v] ) {
g[vert[i]].push_back( vert2[l][u] );
u = parent[l][u];
}
}
for ( int l = MAX_LOG_N; l >= 0; l-- ) {
if ( parent[l][u] != parent[l][v] ) {
g[vert[i]].push_back( vert2[l][u] );
g[vert[i]].push_back( vert2[l][v] );
u = parent[l][u];
v = parent[l][v];
}
}
g[vert[i]].push_back( vert2[0][u] );
}
for ( int i = 0; i < V; i++ ) {
for ( int j: g[i] )
in[j]++;
}
queue<int> q;
for ( int i = 0; i < V; i++ ) {
if ( in[i] == 0 )
q.push( i );
}
int pasi = 0;
while ( !q.empty() ) {
int u = q.front();
q.pop();
pasi++;
for ( int v: g[u] ) {
in[v]--;
if ( in[v] == 0 )
q.push( v );
}
}
cout << (pasi == V ? "Yes\n" : "No\n");
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
68188 KB |
Output is correct |
2 |
Correct |
18 ms |
67932 KB |
Output is correct |
3 |
Correct |
16 ms |
63940 KB |
Output is correct |
4 |
Correct |
40 ms |
70512 KB |
Output is correct |
5 |
Correct |
70 ms |
70744 KB |
Output is correct |
6 |
Correct |
17 ms |
74328 KB |
Output is correct |
7 |
Correct |
16 ms |
74332 KB |
Output is correct |
8 |
Correct |
18 ms |
74332 KB |
Output is correct |
9 |
Correct |
126 ms |
86916 KB |
Output is correct |
10 |
Runtime error |
157 ms |
175052 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
67928 KB |
Output is correct |
2 |
Correct |
13 ms |
64000 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Incorrect |
18 ms |
74332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
67928 KB |
Output is correct |
2 |
Correct |
13 ms |
64000 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Incorrect |
18 ms |
74332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
67928 KB |
Output is correct |
2 |
Correct |
13 ms |
64000 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Incorrect |
18 ms |
74332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
67928 KB |
Output is correct |
2 |
Correct |
13 ms |
64000 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Incorrect |
18 ms |
74332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
68100 KB |
Output is correct |
2 |
Correct |
13 ms |
65880 KB |
Output is correct |
3 |
Correct |
15 ms |
68188 KB |
Output is correct |
4 |
Correct |
13 ms |
63836 KB |
Output is correct |
5 |
Correct |
27 ms |
68192 KB |
Output is correct |
6 |
Correct |
15 ms |
72536 KB |
Output is correct |
7 |
Correct |
15 ms |
72284 KB |
Output is correct |
8 |
Incorrect |
14 ms |
67932 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
68188 KB |
Output is correct |
2 |
Correct |
18 ms |
67932 KB |
Output is correct |
3 |
Correct |
16 ms |
63940 KB |
Output is correct |
4 |
Correct |
40 ms |
70512 KB |
Output is correct |
5 |
Correct |
70 ms |
70744 KB |
Output is correct |
6 |
Correct |
17 ms |
74328 KB |
Output is correct |
7 |
Correct |
16 ms |
74332 KB |
Output is correct |
8 |
Correct |
18 ms |
74332 KB |
Output is correct |
9 |
Correct |
126 ms |
86916 KB |
Output is correct |
10 |
Runtime error |
157 ms |
175052 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |