# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
632629 |
2022-08-20T13:17:45 Z |
Mahdi |
Jail (JOI22_jail) |
C++17 |
|
279 ms |
121876 KB |
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef pair<int, int> pii;
typedef long long ll;
const int N=3e5+5;
int n, m, nm, sq, s[N], t[N], dis[N], pr[N][18];
vector<int>g[N], h[3*N], nd;
bool mk[3*N];
pii st[N];
void dfs(const int &v, const int &p=-1){
for(int u: g[v]){
if(u!=p){
pr[u][0]=v;
for(int i=1;i<17;++i)
pr[u][i]=pr[pr[u][i-1]][i-1];
dis[u]=dis[v]+1;
dfs(u, v);
}
}
}
inline int par(int v, int p){
for(int i=0;p>0;++i){
if(p&1)
v=pr[v][i];
p>>=1;
}
return v;
}
inline int lca(int u, int v){
u=par(u, dis[u]-dis[v]);
v=par(v, dis[v]-dis[u]);
if(u==v)
return v;
for(int i=16;i>=0;--i){
if(pr[u][i]!=pr[v][i]){
v=pr[v][i];
u=pr[u][i];
}
}
return pr[v][0];
}
void pre(){
nm=2*n+m;
for(int i=0;i<nm;++i)
h[i].clear();
for(int v=0;v<n;++v){
int u=v, w=par(v, sq);
while(u!=w){
if(s[u]!=-1)
h[s[u]+2*n].push_back(v);
if(t[u]!=-1)
h[v+n].push_back(t[u]+2*n);
u=pr[u][0];
}
}
}
inline void opr(int i, int v, int u){
int w=lca(u, v);
int x=pr[v][0];
int y=par(x, sq);
while(dis[y]>=dis[w]){
h[x].push_back(i);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>=dis[w];x=pr[x][0]){
if(s[x]!=-1)
h[s[x]+2*n].push_back(i);
if(x==w)
break;
}
x=v;
y=par(v, sq);
while(dis[y]>=dis[w]){
h[i].push_back(x+n);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>=dis[w];x=pr[x][0]){
if(t[x]!=-1)
h[i].push_back(t[x]+2*n);
if(x==w)
break;
}
x=pr[u][0];
y=par(x, sq);
while(dis[y]>=dis[w]){
h[i].push_back(x+n);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>dis[w];x=pr[x][0]){
if(t[x]!=-1)
h[i].push_back(t[x]+2*n);
}
x=u;
y=par(x, sq);
while(dis[y]>=dis[w]){
h[x].push_back(i);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>dis[w];x=pr[x][0]){
if(s[x]!=-1)
h[s[x]+2*n].push_back(i);
}
}
void sfd(const int &v){
mk[v]=1;
for(int u: h[v]){
if(!mk[u])
sfd(u);
}
nd.push_back(v);
}
void sol(){
cin>>n;
sq=sqrt(n);
for(int i=0;i<n;++i)
g[i].clear();
for(int i=1;i<n;++i){
int u, v;
cin>>u>>v;
g[--v].push_back(--u);
g[u].push_back(v);
}
cin>>m;
memset(s, -1, 4*n);
memset(t, -1, 4*n);
for(int i=0;i<m;++i){
int u, v;
cin>>u>>v;
s[--u]=i;
t[--v]=i;
st[i]={u, v};
}
dfs(0);
pre();
for(int i=0;i<m;++i)
opr(2*n+i, st[i].F, st[i].S);
nd.clear();
memset(mk, 0, 4*nm);
for(int i=0;i<nm;++i){
if(!mk[i])
sfd(i);
}
memset(mk, 0, 4*nm);
for(int i=0;i<nm;++i){
mk[nd[i]]=1;
for(int u: h[nd[i]]){
if(!mk[u]){
cout<<"No\n";
return;
}
}
}
cout<<"Yes\n";
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int T;
cin>>T;
while(T--)
sol();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28524 KB |
Output is correct |
2 |
Correct |
15 ms |
28520 KB |
Output is correct |
3 |
Correct |
14 ms |
28528 KB |
Output is correct |
4 |
Correct |
26 ms |
28876 KB |
Output is correct |
5 |
Correct |
41 ms |
29344 KB |
Output is correct |
6 |
Correct |
15 ms |
28628 KB |
Output is correct |
7 |
Correct |
17 ms |
28644 KB |
Output is correct |
8 |
Correct |
16 ms |
28628 KB |
Output is correct |
9 |
Correct |
102 ms |
32124 KB |
Output is correct |
10 |
Runtime error |
279 ms |
121876 KB |
Execution killed with signal 6 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
16 ms |
28520 KB |
Output is correct |
6 |
Correct |
16 ms |
28592 KB |
Output is correct |
7 |
Correct |
17 ms |
28588 KB |
Output is correct |
8 |
Correct |
15 ms |
28484 KB |
Output is correct |
9 |
Correct |
19 ms |
28468 KB |
Output is correct |
10 |
Correct |
15 ms |
28484 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
15 ms |
28548 KB |
Output is correct |
13 |
Correct |
15 ms |
28524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
16 ms |
28520 KB |
Output is correct |
6 |
Correct |
16 ms |
28592 KB |
Output is correct |
7 |
Correct |
17 ms |
28588 KB |
Output is correct |
8 |
Correct |
15 ms |
28484 KB |
Output is correct |
9 |
Correct |
19 ms |
28468 KB |
Output is correct |
10 |
Correct |
15 ms |
28484 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
15 ms |
28548 KB |
Output is correct |
13 |
Correct |
15 ms |
28524 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28576 KB |
Output is correct |
16 |
Correct |
16 ms |
28500 KB |
Output is correct |
17 |
Correct |
15 ms |
28596 KB |
Output is correct |
18 |
Correct |
16 ms |
28500 KB |
Output is correct |
19 |
Correct |
16 ms |
28496 KB |
Output is correct |
20 |
Correct |
16 ms |
28500 KB |
Output is correct |
21 |
Correct |
17 ms |
28604 KB |
Output is correct |
22 |
Correct |
16 ms |
28488 KB |
Output is correct |
23 |
Correct |
15 ms |
28472 KB |
Output is correct |
24 |
Correct |
14 ms |
28492 KB |
Output is correct |
25 |
Correct |
15 ms |
28500 KB |
Output is correct |
26 |
Correct |
14 ms |
28480 KB |
Output is correct |
27 |
Correct |
15 ms |
28500 KB |
Output is correct |
28 |
Correct |
13 ms |
28544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
16 ms |
28520 KB |
Output is correct |
6 |
Correct |
16 ms |
28592 KB |
Output is correct |
7 |
Correct |
17 ms |
28588 KB |
Output is correct |
8 |
Correct |
15 ms |
28484 KB |
Output is correct |
9 |
Correct |
19 ms |
28468 KB |
Output is correct |
10 |
Correct |
15 ms |
28484 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
15 ms |
28548 KB |
Output is correct |
13 |
Correct |
15 ms |
28524 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28576 KB |
Output is correct |
16 |
Correct |
16 ms |
28500 KB |
Output is correct |
17 |
Correct |
15 ms |
28596 KB |
Output is correct |
18 |
Correct |
16 ms |
28500 KB |
Output is correct |
19 |
Correct |
16 ms |
28496 KB |
Output is correct |
20 |
Correct |
16 ms |
28500 KB |
Output is correct |
21 |
Correct |
17 ms |
28604 KB |
Output is correct |
22 |
Correct |
16 ms |
28488 KB |
Output is correct |
23 |
Correct |
15 ms |
28472 KB |
Output is correct |
24 |
Correct |
14 ms |
28492 KB |
Output is correct |
25 |
Correct |
15 ms |
28500 KB |
Output is correct |
26 |
Correct |
14 ms |
28480 KB |
Output is correct |
27 |
Correct |
15 ms |
28500 KB |
Output is correct |
28 |
Correct |
13 ms |
28544 KB |
Output is correct |
29 |
Correct |
17 ms |
28628 KB |
Output is correct |
30 |
Correct |
16 ms |
28628 KB |
Output is correct |
31 |
Correct |
16 ms |
28628 KB |
Output is correct |
32 |
Correct |
17 ms |
28500 KB |
Output is correct |
33 |
Correct |
17 ms |
28604 KB |
Output is correct |
34 |
Correct |
15 ms |
28528 KB |
Output is correct |
35 |
Correct |
17 ms |
28516 KB |
Output is correct |
36 |
Correct |
19 ms |
28628 KB |
Output is correct |
37 |
Correct |
15 ms |
28584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
16 ms |
28520 KB |
Output is correct |
6 |
Correct |
16 ms |
28592 KB |
Output is correct |
7 |
Correct |
17 ms |
28588 KB |
Output is correct |
8 |
Correct |
15 ms |
28484 KB |
Output is correct |
9 |
Correct |
19 ms |
28468 KB |
Output is correct |
10 |
Correct |
15 ms |
28484 KB |
Output is correct |
11 |
Correct |
14 ms |
28592 KB |
Output is correct |
12 |
Correct |
15 ms |
28548 KB |
Output is correct |
13 |
Correct |
15 ms |
28524 KB |
Output is correct |
14 |
Correct |
14 ms |
28500 KB |
Output is correct |
15 |
Correct |
14 ms |
28576 KB |
Output is correct |
16 |
Correct |
16 ms |
28500 KB |
Output is correct |
17 |
Correct |
15 ms |
28596 KB |
Output is correct |
18 |
Correct |
16 ms |
28500 KB |
Output is correct |
19 |
Correct |
16 ms |
28496 KB |
Output is correct |
20 |
Correct |
16 ms |
28500 KB |
Output is correct |
21 |
Correct |
17 ms |
28604 KB |
Output is correct |
22 |
Correct |
16 ms |
28488 KB |
Output is correct |
23 |
Correct |
15 ms |
28472 KB |
Output is correct |
24 |
Correct |
14 ms |
28492 KB |
Output is correct |
25 |
Correct |
15 ms |
28500 KB |
Output is correct |
26 |
Correct |
14 ms |
28480 KB |
Output is correct |
27 |
Correct |
15 ms |
28500 KB |
Output is correct |
28 |
Correct |
13 ms |
28544 KB |
Output is correct |
29 |
Correct |
17 ms |
28628 KB |
Output is correct |
30 |
Correct |
16 ms |
28628 KB |
Output is correct |
31 |
Correct |
16 ms |
28628 KB |
Output is correct |
32 |
Correct |
17 ms |
28500 KB |
Output is correct |
33 |
Correct |
17 ms |
28604 KB |
Output is correct |
34 |
Correct |
15 ms |
28528 KB |
Output is correct |
35 |
Correct |
17 ms |
28516 KB |
Output is correct |
36 |
Correct |
19 ms |
28628 KB |
Output is correct |
37 |
Correct |
15 ms |
28584 KB |
Output is correct |
38 |
Correct |
99 ms |
31336 KB |
Output is correct |
39 |
Runtime error |
259 ms |
121128 KB |
Execution killed with signal 6 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
16 ms |
28468 KB |
Output is correct |
4 |
Correct |
14 ms |
28440 KB |
Output is correct |
5 |
Correct |
20 ms |
28552 KB |
Output is correct |
6 |
Correct |
14 ms |
28588 KB |
Output is correct |
7 |
Correct |
14 ms |
28588 KB |
Output is correct |
8 |
Correct |
15 ms |
28500 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
15 ms |
28500 KB |
Output is correct |
11 |
Correct |
14 ms |
28500 KB |
Output is correct |
12 |
Correct |
16 ms |
28628 KB |
Output is correct |
13 |
Correct |
37 ms |
28552 KB |
Output is correct |
14 |
Correct |
46 ms |
28576 KB |
Output is correct |
15 |
Correct |
41 ms |
28584 KB |
Output is correct |
16 |
Runtime error |
119 ms |
91868 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28524 KB |
Output is correct |
2 |
Correct |
15 ms |
28520 KB |
Output is correct |
3 |
Correct |
14 ms |
28528 KB |
Output is correct |
4 |
Correct |
26 ms |
28876 KB |
Output is correct |
5 |
Correct |
41 ms |
29344 KB |
Output is correct |
6 |
Correct |
15 ms |
28628 KB |
Output is correct |
7 |
Correct |
17 ms |
28644 KB |
Output is correct |
8 |
Correct |
16 ms |
28628 KB |
Output is correct |
9 |
Correct |
102 ms |
32124 KB |
Output is correct |
10 |
Runtime error |
279 ms |
121876 KB |
Execution killed with signal 6 |
11 |
Halted |
0 ms |
0 KB |
- |