#include <bits/stdc++.h>
using namespace std;
struct prisoner {
int b, w;
};
const int MAX_N = 1.2e5;
const int MAX_M = MAX_N;
int parent[MAX_N + 1], depth[MAX_N + 1], bedroom[MAX_N + 1], workroom[MAX_N + 1], in[MAX_N + 1];
vector <int> edges[MAX_N + 1], g[MAX_M + 1];
prisoner prisoners[MAX_M + 1];
void dfs( int u, int p ) {
depth[u] = depth[p] + 1;
parent[u] = p;
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfs( v, u );
}
}
int main() {
int t;
cin >> t;
while ( t-- ) {
int n, m;
cin >> n;
for ( int v = 1; v <= n; v++ ) {
edges[v].clear();
workroom[v] = 0;
bedroom[v] = 0;
}
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++ ) {
g[i].clear();
in[i] = 0;
}
for ( int i = 1; i <= m; i++ )
cin >> prisoners[i].b >> prisoners[i].w;
dfs( 1, 0 );
for ( int i = 1; i <= m; i++ ) {
bedroom[prisoners[i].b] = i;
workroom[prisoners[i].w] = i;
}
for ( int i = 1; i <= m; i++ ) {
int u = prisoners[i].b, v = prisoners[i].w;
if ( depth[u] < depth[v] )
swap( u, v );
vector <int> b, w;
while ( depth[u] > depth[v] ) {
if ( bedroom[u] != 0 )
b.push_back( bedroom[u] );
if ( workroom[u] != 0 )
w.push_back( workroom[u] );
u = parent[u];
}
while ( u != v ) {
if ( bedroom[u] != 0 )
b.push_back( bedroom[u] );
if ( workroom[u] != 0 )
w.push_back( workroom[u] );
if ( bedroom[v] != 0 )
b.push_back( bedroom[v] );
if ( workroom[v] != 0 )
w.push_back( workroom[v] );
u = parent[u];
v = parent[v];
}
if ( bedroom[u] != 0 )
b.push_back( bedroom[u] );
if ( workroom[u] != 0 )
w.push_back( workroom[u] );
for ( int j: b ) {
if ( i == j )
continue;
g[j].push_back( i );
}
for ( int j: w ) {
if ( i == j )
continue;
g[i].push_back( j );
}
}
for ( int i = 1; i <= m; i++ ) {
sort( g[i].begin(), g[i].end() );
g[i].resize( unique( g[i].begin(), g[i].end() ) - g[i].begin() );
for ( int j: g[i] )
in[j]++;
}
queue <int> q;
for ( int i = 1; i <= m; 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 == m ? "Yes\n" : "No\n");
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
18 ms |
8780 KB |
Output is correct |
5 |
Correct |
38 ms |
9308 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
7 ms |
8796 KB |
Output is correct |
9 |
Correct |
128 ms |
12304 KB |
Output is correct |
10 |
Correct |
205 ms |
21252 KB |
Output is correct |
11 |
Correct |
12 ms |
8796 KB |
Output is correct |
12 |
Correct |
86 ms |
9668 KB |
Output is correct |
13 |
Correct |
224 ms |
50260 KB |
Output is correct |
14 |
Correct |
282 ms |
64800 KB |
Output is correct |
15 |
Runtime error |
4380 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
4 ms |
8540 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8636 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8544 KB |
Output is correct |
11 |
Correct |
5 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8632 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
4 ms |
8540 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8636 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8544 KB |
Output is correct |
11 |
Correct |
5 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8632 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8548 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
8736 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
8540 KB |
Output is correct |
20 |
Correct |
3 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8536 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8632 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
4 ms |
8540 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8636 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8544 KB |
Output is correct |
11 |
Correct |
5 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8632 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8548 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
8736 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
8540 KB |
Output is correct |
20 |
Correct |
3 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8536 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8632 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8648 KB |
Output is correct |
29 |
Correct |
6 ms |
8624 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
5 ms |
8536 KB |
Output is correct |
32 |
Correct |
4 ms |
8664 KB |
Output is correct |
33 |
Correct |
5 ms |
8536 KB |
Output is correct |
34 |
Correct |
4 ms |
8536 KB |
Output is correct |
35 |
Correct |
6 ms |
8660 KB |
Output is correct |
36 |
Correct |
3 ms |
8664 KB |
Output is correct |
37 |
Correct |
3 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
4 ms |
8540 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8636 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8544 KB |
Output is correct |
11 |
Correct |
5 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8632 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8548 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
8736 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
8540 KB |
Output is correct |
20 |
Correct |
3 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8536 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8632 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8648 KB |
Output is correct |
29 |
Correct |
6 ms |
8624 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
5 ms |
8536 KB |
Output is correct |
32 |
Correct |
4 ms |
8664 KB |
Output is correct |
33 |
Correct |
5 ms |
8536 KB |
Output is correct |
34 |
Correct |
4 ms |
8536 KB |
Output is correct |
35 |
Correct |
6 ms |
8660 KB |
Output is correct |
36 |
Correct |
3 ms |
8664 KB |
Output is correct |
37 |
Correct |
3 ms |
8540 KB |
Output is correct |
38 |
Correct |
131 ms |
12116 KB |
Output is correct |
39 |
Correct |
205 ms |
21276 KB |
Output is correct |
40 |
Correct |
82 ms |
11088 KB |
Output is correct |
41 |
Correct |
69 ms |
9932 KB |
Output is correct |
42 |
Correct |
74 ms |
11348 KB |
Output is correct |
43 |
Correct |
46 ms |
10112 KB |
Output is correct |
44 |
Correct |
15 ms |
9052 KB |
Output is correct |
45 |
Correct |
72 ms |
14396 KB |
Output is correct |
46 |
Correct |
76 ms |
14212 KB |
Output is correct |
47 |
Correct |
88 ms |
17084 KB |
Output is correct |
48 |
Correct |
89 ms |
16976 KB |
Output is correct |
49 |
Correct |
72 ms |
14416 KB |
Output is correct |
50 |
Correct |
92 ms |
14452 KB |
Output is correct |
51 |
Correct |
67 ms |
15208 KB |
Output is correct |
52 |
Correct |
68 ms |
15184 KB |
Output is correct |
53 |
Correct |
16 ms |
9304 KB |
Output is correct |
54 |
Correct |
78 ms |
14164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8648 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
12 ms |
8808 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
39 ms |
9048 KB |
Output is correct |
14 |
Correct |
48 ms |
9300 KB |
Output is correct |
15 |
Correct |
44 ms |
9368 KB |
Output is correct |
16 |
Correct |
73 ms |
14764 KB |
Output is correct |
17 |
Correct |
157 ms |
20828 KB |
Output is correct |
18 |
Correct |
331 ms |
40196 KB |
Output is correct |
19 |
Correct |
76 ms |
14928 KB |
Output is correct |
20 |
Correct |
81 ms |
15016 KB |
Output is correct |
21 |
Correct |
84 ms |
14956 KB |
Output is correct |
22 |
Correct |
181 ms |
21200 KB |
Output is correct |
23 |
Correct |
126 ms |
21460 KB |
Output is correct |
24 |
Correct |
144 ms |
21460 KB |
Output is correct |
25 |
Correct |
143 ms |
21324 KB |
Output is correct |
26 |
Correct |
141 ms |
21196 KB |
Output is correct |
27 |
Correct |
139 ms |
19940 KB |
Output is correct |
28 |
Correct |
137 ms |
19932 KB |
Output is correct |
29 |
Correct |
130 ms |
20068 KB |
Output is correct |
30 |
Correct |
100 ms |
17320 KB |
Output is correct |
31 |
Correct |
103 ms |
17252 KB |
Output is correct |
32 |
Correct |
103 ms |
17208 KB |
Output is correct |
33 |
Correct |
114 ms |
17352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
18 ms |
8780 KB |
Output is correct |
5 |
Correct |
38 ms |
9308 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
7 ms |
8796 KB |
Output is correct |
9 |
Correct |
128 ms |
12304 KB |
Output is correct |
10 |
Correct |
205 ms |
21252 KB |
Output is correct |
11 |
Correct |
12 ms |
8796 KB |
Output is correct |
12 |
Correct |
86 ms |
9668 KB |
Output is correct |
13 |
Correct |
224 ms |
50260 KB |
Output is correct |
14 |
Correct |
282 ms |
64800 KB |
Output is correct |
15 |
Runtime error |
4380 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |