#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 );
while ( depth[u] > depth[v] ) {
if ( bedroom[u] != 0 && bedroom[u] != i )
g[bedroom[u]].push_back( i );
if ( workroom[u] != 0 && workroom[u] != i )
g[i].push_back( workroom[u] );
u = parent[u];
}
while ( u != v ) {
if ( bedroom[u] != 0 && bedroom[u] != i )
g[bedroom[u]].push_back( i );
if ( workroom[u] != 0 && workroom[u] != i )
g[i].push_back( workroom[u] );
if ( bedroom[v] != 0 && bedroom[v] != i )
g[bedroom[v]].push_back( i );
if ( workroom[v] != 0 && workroom[v] != i )
g[i].push_back( workroom[v] );
u = parent[u];
v = parent[v];
}
if ( bedroom[u] != 0 && bedroom[u] != i )
g[bedroom[u]].push_back( i );
if ( workroom[u] != 0 && workroom[u] != i )
g[i].push_back( workroom[u] );
}
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 |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
17 ms |
9048 KB |
Output is correct |
5 |
Correct |
36 ms |
9300 KB |
Output is correct |
6 |
Correct |
3 ms |
8536 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
5 ms |
8792 KB |
Output is correct |
9 |
Correct |
121 ms |
12112 KB |
Output is correct |
10 |
Correct |
198 ms |
21356 KB |
Output is correct |
11 |
Correct |
11 ms |
8796 KB |
Output is correct |
12 |
Correct |
82 ms |
9796 KB |
Output is correct |
13 |
Correct |
195 ms |
50412 KB |
Output is correct |
14 |
Correct |
265 ms |
64208 KB |
Output is correct |
15 |
Runtime error |
3783 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8704 KB |
Output is correct |
6 |
Correct |
4 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 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 |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 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 |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8704 KB |
Output is correct |
6 |
Correct |
4 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 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 |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8544 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8540 KB |
Output is correct |
22 |
Correct |
3 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8536 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
3 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
3 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8620 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 |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8704 KB |
Output is correct |
6 |
Correct |
4 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 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 |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8544 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8540 KB |
Output is correct |
22 |
Correct |
3 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8536 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
3 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
3 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8620 KB |
Output is correct |
29 |
Correct |
6 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
3 ms |
8540 KB |
Output is correct |
33 |
Correct |
3 ms |
8540 KB |
Output is correct |
34 |
Correct |
5 ms |
8540 KB |
Output is correct |
35 |
Correct |
5 ms |
8536 KB |
Output is correct |
36 |
Correct |
3 ms |
8792 KB |
Output is correct |
37 |
Correct |
3 ms |
8540 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 |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
3 ms |
8704 KB |
Output is correct |
6 |
Correct |
4 ms |
8540 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 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 |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8544 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8540 KB |
Output is correct |
22 |
Correct |
3 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8536 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
3 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8676 KB |
Output is correct |
27 |
Correct |
3 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8620 KB |
Output is correct |
29 |
Correct |
6 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
3 ms |
8540 KB |
Output is correct |
33 |
Correct |
3 ms |
8540 KB |
Output is correct |
34 |
Correct |
5 ms |
8540 KB |
Output is correct |
35 |
Correct |
5 ms |
8536 KB |
Output is correct |
36 |
Correct |
3 ms |
8792 KB |
Output is correct |
37 |
Correct |
3 ms |
8540 KB |
Output is correct |
38 |
Correct |
122 ms |
12120 KB |
Output is correct |
39 |
Correct |
201 ms |
21328 KB |
Output is correct |
40 |
Correct |
82 ms |
11160 KB |
Output is correct |
41 |
Correct |
50 ms |
10128 KB |
Output is correct |
42 |
Correct |
70 ms |
11348 KB |
Output is correct |
43 |
Correct |
46 ms |
9976 KB |
Output is correct |
44 |
Correct |
13 ms |
9052 KB |
Output is correct |
45 |
Correct |
63 ms |
14396 KB |
Output is correct |
46 |
Correct |
79 ms |
14180 KB |
Output is correct |
47 |
Correct |
81 ms |
16980 KB |
Output is correct |
48 |
Correct |
82 ms |
17080 KB |
Output is correct |
49 |
Correct |
67 ms |
14324 KB |
Output is correct |
50 |
Correct |
66 ms |
14368 KB |
Output is correct |
51 |
Correct |
66 ms |
15160 KB |
Output is correct |
52 |
Correct |
61 ms |
15068 KB |
Output is correct |
53 |
Correct |
14 ms |
9048 KB |
Output is correct |
54 |
Correct |
65 ms |
14164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
10 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8628 KB |
Output is correct |
7 |
Correct |
2 ms |
8544 KB |
Output is correct |
8 |
Correct |
2 ms |
8544 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8644 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
3 ms |
8728 KB |
Output is correct |
13 |
Correct |
35 ms |
9052 KB |
Output is correct |
14 |
Correct |
45 ms |
9292 KB |
Output is correct |
15 |
Correct |
39 ms |
9304 KB |
Output is correct |
16 |
Correct |
66 ms |
14944 KB |
Output is correct |
17 |
Correct |
131 ms |
20564 KB |
Output is correct |
18 |
Correct |
298 ms |
39968 KB |
Output is correct |
19 |
Correct |
71 ms |
14936 KB |
Output is correct |
20 |
Correct |
78 ms |
15072 KB |
Output is correct |
21 |
Correct |
80 ms |
15080 KB |
Output is correct |
22 |
Correct |
169 ms |
21176 KB |
Output is correct |
23 |
Correct |
107 ms |
21576 KB |
Output is correct |
24 |
Correct |
147 ms |
21216 KB |
Output is correct |
25 |
Correct |
127 ms |
21376 KB |
Output is correct |
26 |
Correct |
125 ms |
21104 KB |
Output is correct |
27 |
Correct |
126 ms |
20104 KB |
Output is correct |
28 |
Correct |
127 ms |
20068 KB |
Output is correct |
29 |
Correct |
128 ms |
20172 KB |
Output is correct |
30 |
Correct |
94 ms |
17172 KB |
Output is correct |
31 |
Correct |
90 ms |
17196 KB |
Output is correct |
32 |
Correct |
93 ms |
17288 KB |
Output is correct |
33 |
Correct |
91 ms |
17356 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 |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
17 ms |
9048 KB |
Output is correct |
5 |
Correct |
36 ms |
9300 KB |
Output is correct |
6 |
Correct |
3 ms |
8536 KB |
Output is correct |
7 |
Correct |
3 ms |
8540 KB |
Output is correct |
8 |
Correct |
5 ms |
8792 KB |
Output is correct |
9 |
Correct |
121 ms |
12112 KB |
Output is correct |
10 |
Correct |
198 ms |
21356 KB |
Output is correct |
11 |
Correct |
11 ms |
8796 KB |
Output is correct |
12 |
Correct |
82 ms |
9796 KB |
Output is correct |
13 |
Correct |
195 ms |
50412 KB |
Output is correct |
14 |
Correct |
265 ms |
64208 KB |
Output is correct |
15 |
Runtime error |
3783 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |