# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
709483 |
2023-03-13T16:58:29 Z |
urosk |
Jail (JOI22_jail) |
C++14 |
|
426 ms |
292672 KB |
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 2000005
#define lg 22
ll n,m;
vector<ll> g[maxn];
vector<ll> v[maxn];
ll tsz = 0;
ll id[maxn][lg][2];
ll st[maxn][lg];
ll in[maxn],out[maxn],ti = 0;
bool intree(ll x,ll y){return in[y]<=in[x]&&out[x]<=out[y];}
ll lca(ll x,ll y){
if(intree(x,y)) return y;
if(intree(y,x)) return x;
for(ll j = lg-1;j>=0;j--){
if(!intree(x,st[y][j])) y = st[y][j];
}
return st[y][0];
}
void dfs(ll u,ll p){
in[u] = ++ti;
st[u][0] = p;
for(ll s : g[u]) if(s!=p) dfs(s,u);
out[u] = ti-1;
}
ll get(ll x,ll y){
for(ll j = lg-1;j>=0;j--){
if(!intree(y,st[x][j])) x = st[x][j];
}
return x;
}
void add(ll x,ll y,bool smer,ll nod){
for(ll j = lg-1;j>=0;j--){
if(!intree(y,st[x][j])){
if(smer) v[id[x][j][1]].pb(nod);
else v[nod].pb(id[x][j][0]);
x = st[x][j];
}
}
if(smer) v[id[x][0][1]].pb(nod);
else v[nod].pb(id[x][0][0]);
if(smer) v[id[y][0][1]].pb(nod);
else v[nod].pb(id[y][0][0]);
}
ll deg[maxn];
void tc(){
ti = 0;
cin >> n;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfs(1,1);
for(ll i = 1;i<=n;i++) id[i][0][0] = i,id[i][0][1] = i+n;
tsz = 2*n;
for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++){
st[i][j] = st[st[i][j-1]][j-1];
++tsz;
id[i][j][0] = tsz;
v[tsz].pb(id[i][j-1][0]);
v[tsz].pb(id[st[i][j-1]][j-1][0]);
++tsz;
id[i][j][1] = tsz;
v[id[i][j-1][1]].pb(tsz);
v[id[st[i][j-1]][j-1][1]].pb(tsz);
}
cin >> m;
for(ll i = 1;i<=m;i++){
ll x,y; cin >> x >> y;
++tsz;
v[x].pb(tsz);
v[tsz].pb(n+y);
ll z = lca(x,y);
if(z!=x){
add(x,get(x,z),1,tsz);
add(st[x][0],z,0,tsz);
}
if(z!=y){
add(y,get(y,z),0,tsz);
add(st[y][0],z,1,tsz);
}
}
queue<ll> q;
for(ll i = 1;i<=tsz;i++) deg[i] = 0;
for(ll i = 1;i<=tsz;i++){
for(ll j : v[i]) deg[j]++;
}
for(ll i = 1;i<=tsz;i++){
if(!deg[i]) q.push(i);
}
ll cnt = 0;
while(q.size()){
ll x = q.front();
q.pop();
cnt++;
for(ll y : v[x]){
deg[y]--;
if(!deg[y]) q.push(y);
}
}
if(cnt!=tsz) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
for(ll i = 1;i<=n;i++) g[i].clear();
for(ll i = 1;i<=tsz;i++) v[i].clear();
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
cin >> t;
while(t--){
tc();
}
return 0;
}
/**
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
2
3 4
4 8
2
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7
4
1 2
1 3
1 4
3
2 3
3 4
4 2
3
3
1 2
2 3
2
2 1
3 2
7
1 2
2 3
3 4
4 5
5 6
6 7
3
1 3
4 2
2 5
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94292 KB |
Output is correct |
2 |
Correct |
50 ms |
94256 KB |
Output is correct |
3 |
Correct |
50 ms |
94284 KB |
Output is correct |
4 |
Correct |
151 ms |
94824 KB |
Output is correct |
5 |
Correct |
242 ms |
95340 KB |
Output is correct |
6 |
Correct |
64 ms |
94832 KB |
Output is correct |
7 |
Correct |
66 ms |
94936 KB |
Output is correct |
8 |
Correct |
64 ms |
94952 KB |
Output is correct |
9 |
Correct |
405 ms |
111156 KB |
Output is correct |
10 |
Runtime error |
334 ms |
291720 KB |
Execution killed with signal 6 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94236 KB |
Output is correct |
2 |
Correct |
45 ms |
94196 KB |
Output is correct |
3 |
Correct |
54 ms |
94836 KB |
Output is correct |
4 |
Correct |
54 ms |
94964 KB |
Output is correct |
5 |
Correct |
59 ms |
94920 KB |
Output is correct |
6 |
Correct |
59 ms |
94872 KB |
Output is correct |
7 |
Correct |
56 ms |
94964 KB |
Output is correct |
8 |
Correct |
62 ms |
94964 KB |
Output is correct |
9 |
Correct |
57 ms |
94960 KB |
Output is correct |
10 |
Correct |
55 ms |
94924 KB |
Output is correct |
11 |
Correct |
56 ms |
94864 KB |
Output is correct |
12 |
Correct |
52 ms |
94824 KB |
Output is correct |
13 |
Correct |
53 ms |
94924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94236 KB |
Output is correct |
2 |
Correct |
45 ms |
94196 KB |
Output is correct |
3 |
Correct |
54 ms |
94836 KB |
Output is correct |
4 |
Correct |
54 ms |
94964 KB |
Output is correct |
5 |
Correct |
59 ms |
94920 KB |
Output is correct |
6 |
Correct |
59 ms |
94872 KB |
Output is correct |
7 |
Correct |
56 ms |
94964 KB |
Output is correct |
8 |
Correct |
62 ms |
94964 KB |
Output is correct |
9 |
Correct |
57 ms |
94960 KB |
Output is correct |
10 |
Correct |
55 ms |
94924 KB |
Output is correct |
11 |
Correct |
56 ms |
94864 KB |
Output is correct |
12 |
Correct |
52 ms |
94824 KB |
Output is correct |
13 |
Correct |
53 ms |
94924 KB |
Output is correct |
14 |
Correct |
44 ms |
94288 KB |
Output is correct |
15 |
Correct |
47 ms |
94276 KB |
Output is correct |
16 |
Correct |
56 ms |
94900 KB |
Output is correct |
17 |
Correct |
59 ms |
94932 KB |
Output is correct |
18 |
Correct |
65 ms |
94992 KB |
Output is correct |
19 |
Correct |
50 ms |
94252 KB |
Output is correct |
20 |
Correct |
57 ms |
94928 KB |
Output is correct |
21 |
Correct |
64 ms |
94896 KB |
Output is correct |
22 |
Correct |
57 ms |
94896 KB |
Output is correct |
23 |
Correct |
47 ms |
94248 KB |
Output is correct |
24 |
Correct |
49 ms |
94504 KB |
Output is correct |
25 |
Correct |
57 ms |
94992 KB |
Output is correct |
26 |
Correct |
56 ms |
94924 KB |
Output is correct |
27 |
Correct |
59 ms |
94896 KB |
Output is correct |
28 |
Correct |
57 ms |
94304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94236 KB |
Output is correct |
2 |
Correct |
45 ms |
94196 KB |
Output is correct |
3 |
Correct |
54 ms |
94836 KB |
Output is correct |
4 |
Correct |
54 ms |
94964 KB |
Output is correct |
5 |
Correct |
59 ms |
94920 KB |
Output is correct |
6 |
Correct |
59 ms |
94872 KB |
Output is correct |
7 |
Correct |
56 ms |
94964 KB |
Output is correct |
8 |
Correct |
62 ms |
94964 KB |
Output is correct |
9 |
Correct |
57 ms |
94960 KB |
Output is correct |
10 |
Correct |
55 ms |
94924 KB |
Output is correct |
11 |
Correct |
56 ms |
94864 KB |
Output is correct |
12 |
Correct |
52 ms |
94824 KB |
Output is correct |
13 |
Correct |
53 ms |
94924 KB |
Output is correct |
14 |
Correct |
44 ms |
94288 KB |
Output is correct |
15 |
Correct |
47 ms |
94276 KB |
Output is correct |
16 |
Correct |
56 ms |
94900 KB |
Output is correct |
17 |
Correct |
59 ms |
94932 KB |
Output is correct |
18 |
Correct |
65 ms |
94992 KB |
Output is correct |
19 |
Correct |
50 ms |
94252 KB |
Output is correct |
20 |
Correct |
57 ms |
94928 KB |
Output is correct |
21 |
Correct |
64 ms |
94896 KB |
Output is correct |
22 |
Correct |
57 ms |
94896 KB |
Output is correct |
23 |
Correct |
47 ms |
94248 KB |
Output is correct |
24 |
Correct |
49 ms |
94504 KB |
Output is correct |
25 |
Correct |
57 ms |
94992 KB |
Output is correct |
26 |
Correct |
56 ms |
94924 KB |
Output is correct |
27 |
Correct |
59 ms |
94896 KB |
Output is correct |
28 |
Correct |
57 ms |
94304 KB |
Output is correct |
29 |
Correct |
61 ms |
94968 KB |
Output is correct |
30 |
Correct |
59 ms |
95040 KB |
Output is correct |
31 |
Correct |
55 ms |
95060 KB |
Output is correct |
32 |
Correct |
57 ms |
95044 KB |
Output is correct |
33 |
Correct |
59 ms |
95024 KB |
Output is correct |
34 |
Correct |
65 ms |
94876 KB |
Output is correct |
35 |
Correct |
57 ms |
94924 KB |
Output is correct |
36 |
Correct |
53 ms |
94976 KB |
Output is correct |
37 |
Correct |
56 ms |
94896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94236 KB |
Output is correct |
2 |
Correct |
45 ms |
94196 KB |
Output is correct |
3 |
Correct |
54 ms |
94836 KB |
Output is correct |
4 |
Correct |
54 ms |
94964 KB |
Output is correct |
5 |
Correct |
59 ms |
94920 KB |
Output is correct |
6 |
Correct |
59 ms |
94872 KB |
Output is correct |
7 |
Correct |
56 ms |
94964 KB |
Output is correct |
8 |
Correct |
62 ms |
94964 KB |
Output is correct |
9 |
Correct |
57 ms |
94960 KB |
Output is correct |
10 |
Correct |
55 ms |
94924 KB |
Output is correct |
11 |
Correct |
56 ms |
94864 KB |
Output is correct |
12 |
Correct |
52 ms |
94824 KB |
Output is correct |
13 |
Correct |
53 ms |
94924 KB |
Output is correct |
14 |
Correct |
44 ms |
94288 KB |
Output is correct |
15 |
Correct |
47 ms |
94276 KB |
Output is correct |
16 |
Correct |
56 ms |
94900 KB |
Output is correct |
17 |
Correct |
59 ms |
94932 KB |
Output is correct |
18 |
Correct |
65 ms |
94992 KB |
Output is correct |
19 |
Correct |
50 ms |
94252 KB |
Output is correct |
20 |
Correct |
57 ms |
94928 KB |
Output is correct |
21 |
Correct |
64 ms |
94896 KB |
Output is correct |
22 |
Correct |
57 ms |
94896 KB |
Output is correct |
23 |
Correct |
47 ms |
94248 KB |
Output is correct |
24 |
Correct |
49 ms |
94504 KB |
Output is correct |
25 |
Correct |
57 ms |
94992 KB |
Output is correct |
26 |
Correct |
56 ms |
94924 KB |
Output is correct |
27 |
Correct |
59 ms |
94896 KB |
Output is correct |
28 |
Correct |
57 ms |
94304 KB |
Output is correct |
29 |
Correct |
61 ms |
94968 KB |
Output is correct |
30 |
Correct |
59 ms |
95040 KB |
Output is correct |
31 |
Correct |
55 ms |
95060 KB |
Output is correct |
32 |
Correct |
57 ms |
95044 KB |
Output is correct |
33 |
Correct |
59 ms |
95024 KB |
Output is correct |
34 |
Correct |
65 ms |
94876 KB |
Output is correct |
35 |
Correct |
57 ms |
94924 KB |
Output is correct |
36 |
Correct |
53 ms |
94976 KB |
Output is correct |
37 |
Correct |
56 ms |
94896 KB |
Output is correct |
38 |
Correct |
426 ms |
111192 KB |
Output is correct |
39 |
Runtime error |
339 ms |
291680 KB |
Execution killed with signal 6 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
94244 KB |
Output is correct |
2 |
Correct |
55 ms |
94300 KB |
Output is correct |
3 |
Correct |
48 ms |
94204 KB |
Output is correct |
4 |
Correct |
47 ms |
94244 KB |
Output is correct |
5 |
Correct |
94 ms |
94380 KB |
Output is correct |
6 |
Correct |
57 ms |
94908 KB |
Output is correct |
7 |
Correct |
58 ms |
94928 KB |
Output is correct |
8 |
Correct |
49 ms |
94284 KB |
Output is correct |
9 |
Correct |
47 ms |
94216 KB |
Output is correct |
10 |
Correct |
48 ms |
94520 KB |
Output is correct |
11 |
Correct |
56 ms |
94272 KB |
Output is correct |
12 |
Correct |
59 ms |
95012 KB |
Output is correct |
13 |
Correct |
180 ms |
95160 KB |
Output is correct |
14 |
Correct |
286 ms |
95500 KB |
Output is correct |
15 |
Correct |
251 ms |
95364 KB |
Output is correct |
16 |
Runtime error |
342 ms |
292672 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94292 KB |
Output is correct |
2 |
Correct |
50 ms |
94256 KB |
Output is correct |
3 |
Correct |
50 ms |
94284 KB |
Output is correct |
4 |
Correct |
151 ms |
94824 KB |
Output is correct |
5 |
Correct |
242 ms |
95340 KB |
Output is correct |
6 |
Correct |
64 ms |
94832 KB |
Output is correct |
7 |
Correct |
66 ms |
94936 KB |
Output is correct |
8 |
Correct |
64 ms |
94952 KB |
Output is correct |
9 |
Correct |
405 ms |
111156 KB |
Output is correct |
10 |
Runtime error |
334 ms |
291720 KB |
Execution killed with signal 6 |
11 |
Halted |
0 ms |
0 KB |
- |