# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
884011 |
2023-12-06T14:17:01 Z |
LucaIlie |
Jail (JOI22_jail) |
C++17 |
|
1190 ms |
306672 KB |
#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 + 2 * MAX_N * (MAX_LOG_N + 1) + 1;
int parent[MAX_LOG_N + 1][MAX_N + 1], depth[MAX_N + 1], vertP[MAX_M], vertB[MAX_LOG_N + 1][MAX_N + 1], vertW[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++ )
vertP[i] = V++;
for ( int l = 0; l <= log2( n ); l++ ) {
for ( int v = 1; v <= n; v++ ) {
vertB[l][v] = V++;
vertW[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[vertB[l - 1][v]].push_back( vertB[l][v] );
g[vertB[l - 1][parent[l - 1][v]]].push_back( vertB[l][v] );
g[vertW[l][v]].push_back( vertW[l - 1][v] );
g[vertW[l][v]].push_back( vertW[l - 1][parent[l - 1][v]] );
}
}
for ( int i = 1; i <= m; i++ ) {
g[vertP[i]].push_back( vertB[0][prisoners[i].b] );
g[vertW[0][prisoners[i].w]].push_back( vertP[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[vertB[l][u]].push_back( vertP[i] );
u = parent[l][u];
}
}
for ( int l = log2( n ); l >= 0; l-- ) {
if ( parent[l][u] != parent[l][v] ) {
g[vertB[l][u]].push_back( vertP[i] );
g[vertB[l][v]].push_back( vertP[i] );
u = parent[l][u];
v = parent[l][v];
}
}
g[vertB[0][u]].push_back( vertP[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[vertP[i]].push_back( vertW[l][u] );
u = parent[l][u];
}
}
for ( int l = MAX_LOG_N; l >= 0; l-- ) {
if ( parent[l][u] != parent[l][v] ) {
g[vertP[i]].push_back( vertW[l][u] );
g[vertP[i]].push_back( vertW[l][v] );
u = parent[l][u];
v = parent[l][v];
}
}
g[vertP[i]].push_back( vertW[0][u] );
}
g[0].clear();
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");
for ( int v = 0; v < V; v++ )
g[v].clear(), in[v] = 0;
for ( int i = 1; i <= m; i++ )
vertP[i] = 0;
for ( int l = 0; l <= log2( n ); l++ ) {
for ( int v = 1; v <= n; v++ )
parent[l][v] = vertB[l][v] = vertW[l][v] = 0;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
113240 KB |
Output is correct |
2 |
Correct |
24 ms |
113240 KB |
Output is correct |
3 |
Correct |
24 ms |
111196 KB |
Output is correct |
4 |
Correct |
51 ms |
119388 KB |
Output is correct |
5 |
Correct |
81 ms |
119388 KB |
Output is correct |
6 |
Correct |
28 ms |
119384 KB |
Output is correct |
7 |
Correct |
28 ms |
119384 KB |
Output is correct |
8 |
Correct |
28 ms |
119384 KB |
Output is correct |
9 |
Correct |
135 ms |
132820 KB |
Output is correct |
10 |
Correct |
459 ms |
274384 KB |
Output is correct |
11 |
Correct |
36 ms |
113244 KB |
Output is correct |
12 |
Correct |
102 ms |
119388 KB |
Output is correct |
13 |
Correct |
613 ms |
279240 KB |
Output is correct |
14 |
Correct |
501 ms |
279600 KB |
Output is correct |
15 |
Correct |
763 ms |
281208 KB |
Output is correct |
16 |
Correct |
1190 ms |
306672 KB |
Output is correct |
17 |
Correct |
556 ms |
283092 KB |
Output is correct |
18 |
Correct |
576 ms |
281824 KB |
Output is correct |
19 |
Correct |
586 ms |
282828 KB |
Output is correct |
20 |
Correct |
515 ms |
283088 KB |
Output is correct |
21 |
Correct |
554 ms |
282316 KB |
Output is correct |
22 |
Correct |
479 ms |
278224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
113244 KB |
Output is correct |
2 |
Correct |
26 ms |
111196 KB |
Output is correct |
3 |
Correct |
28 ms |
119388 KB |
Output is correct |
4 |
Incorrect |
27 ms |
119388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
113244 KB |
Output is correct |
2 |
Correct |
26 ms |
111196 KB |
Output is correct |
3 |
Correct |
28 ms |
119388 KB |
Output is correct |
4 |
Incorrect |
27 ms |
119388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
113244 KB |
Output is correct |
2 |
Correct |
26 ms |
111196 KB |
Output is correct |
3 |
Correct |
28 ms |
119388 KB |
Output is correct |
4 |
Incorrect |
27 ms |
119388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
113244 KB |
Output is correct |
2 |
Correct |
26 ms |
111196 KB |
Output is correct |
3 |
Correct |
28 ms |
119388 KB |
Output is correct |
4 |
Incorrect |
27 ms |
119388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
113244 KB |
Output is correct |
2 |
Correct |
24 ms |
113252 KB |
Output is correct |
3 |
Correct |
23 ms |
113164 KB |
Output is correct |
4 |
Correct |
23 ms |
111196 KB |
Output is correct |
5 |
Correct |
36 ms |
113356 KB |
Output is correct |
6 |
Correct |
25 ms |
117452 KB |
Output is correct |
7 |
Correct |
27 ms |
117336 KB |
Output is correct |
8 |
Incorrect |
27 ms |
113244 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
113240 KB |
Output is correct |
2 |
Correct |
24 ms |
113240 KB |
Output is correct |
3 |
Correct |
24 ms |
111196 KB |
Output is correct |
4 |
Correct |
51 ms |
119388 KB |
Output is correct |
5 |
Correct |
81 ms |
119388 KB |
Output is correct |
6 |
Correct |
28 ms |
119384 KB |
Output is correct |
7 |
Correct |
28 ms |
119384 KB |
Output is correct |
8 |
Correct |
28 ms |
119384 KB |
Output is correct |
9 |
Correct |
135 ms |
132820 KB |
Output is correct |
10 |
Correct |
459 ms |
274384 KB |
Output is correct |
11 |
Correct |
36 ms |
113244 KB |
Output is correct |
12 |
Correct |
102 ms |
119388 KB |
Output is correct |
13 |
Correct |
613 ms |
279240 KB |
Output is correct |
14 |
Correct |
501 ms |
279600 KB |
Output is correct |
15 |
Correct |
763 ms |
281208 KB |
Output is correct |
16 |
Correct |
1190 ms |
306672 KB |
Output is correct |
17 |
Correct |
556 ms |
283092 KB |
Output is correct |
18 |
Correct |
576 ms |
281824 KB |
Output is correct |
19 |
Correct |
586 ms |
282828 KB |
Output is correct |
20 |
Correct |
515 ms |
283088 KB |
Output is correct |
21 |
Correct |
554 ms |
282316 KB |
Output is correct |
22 |
Correct |
479 ms |
278224 KB |
Output is correct |
23 |
Correct |
23 ms |
113244 KB |
Output is correct |
24 |
Correct |
26 ms |
111196 KB |
Output is correct |
25 |
Correct |
28 ms |
119388 KB |
Output is correct |
26 |
Incorrect |
27 ms |
119388 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |