Submission #567185

# Submission time Handle Problem Language Result Execution time Memory
567185 2022-05-23T08:54:32 Z zaneyu Jail (JOI22_jail) C++14
100 / 100
2105 ms 413380 KB
/*input
1
7
1 2
2 6
2 7
1 3
1 4
4 5
2
5 6
6 7
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=2e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f<<' '<<P.s;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
    return out;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mypow(ll a,ll b){
    a%=MOD;
    if(a==0) return 0;
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
bool wk=1;
vector<int> v[maxn];
int in[maxn],out[maxn];
vector<int> vv[maxn*40];
int vis[maxn*40];
int par[maxn][20];
int dep[maxn];
int cur;
int idx[maxn][20],idx2[maxn][20];

void dfs(int u,int p){
    par[u][0]=p;
    idx[u][0]=(cur++);
    idx2[u][0]=(cur++);
    if(in[u]) vv[in[u]].pb(idx[u][0]);
    if(out[u]) vv[idx2[u][0]].pb(out[u]);
    REP1(j,19){
        if(par[u][j-1]!=-1){
            par[u][j]=par[par[u][j-1]][j-1];
            idx[u][j]=(cur++);
            idx2[u][j]=(cur++);
            vv[idx2[u][j]].pb(idx2[u][j-1]);
            if(idx2[par[u][j-1]][j-1]) vv[idx2[u][j]].pb(idx2[par[u][j-1]][j-1]);
            vv[idx[u][j-1]].pb(idx[u][j]);
            if(idx[par[u][j-1]][j-1]) vv[idx[par[u][j-1]][j-1]].pb(idx[u][j]); 
        }
        else par[u][j]=-1;
    }
    for(int x:v[u]){
        if(x==p) continue;
        dep[x]=dep[u]+1;
        dfs(x,u);
    }
}

void dfs2(int u){
    vis[u]=1;
    for(int x:vv[u]){
        if(!vis[x]) dfs2(x);
        else if(vis[x]==1){
            wk=0;
            break;
        }
    }
    vis[u]=2;
}
int get(int a,int d){
    while(d){
        int z=__lg(lowb(d));
        a=par[a][z];
        d-=lowb(d);
    }
    return a;
}
void solve(){
    int n,m;
    cin>>n;
    REP(i,n-1){
        int a,b;
        cin>>a>>b;
        --a,--b;
        v[a].pb(b),v[b].pb(a);
    }
    cin>>m;
    vector<pii> asd;
    REP1(i,m){
        int a,b;
        cin>>a>>b;
        --a,--b;
        in[a]=i,out[b]=i;
        asd.pb({a,b});
    }
    cur=m+1;
    dfs(0,-1);
    REP1(i,m){
        int a=asd[i-1].f,b=asd[i-1].s;
        int cur=i;
        if(in[b]) vv[in[b]].pb(i);
        if(out[a]) vv[i].pb(out[a]);
        if(dep[a]<dep[b]) swap(a,b);
        int d=dep[a]-dep[b];
        if(get(a,d)==b){
            --d;
            a=par[a][0];
        }
        else{
            a=par[a][0],b=par[b][0];
            if(get(a,d)==b){
                if(in[b]) vv[in[b]].pb(i);
                if(out[b]) vv[i].pb(out[b]);
            }
        }
        while(d){
            int z=__lg(lowb(d));
            vv[idx[a][z]].pb(cur);
            vv[cur].pb(idx2[a][z]);
            a=par[a][z];
            d-=lowb(d);
        }
        if(a==b) continue;
        for(int j=19;j>=0;j--){
            if(par[a][j]!=par[b][j]){
                vv[cur].pb(idx2[a][j]);
                vv[cur].pb(idx2[b][j]);
                vv[idx[a][j]].pb(cur);
                vv[idx[b][j]].pb(cur);
                a=par[a][j],b=par[b][j];
            }
        }
        int j=0;
        vv[cur].pb(idx2[a][j]);
        vv[cur].pb(idx2[b][j]);
        vv[idx[a][j]].pb(cur);
        vv[idx[b][j]].pb(cur);
        a=par[a][j],b=par[b][j];
        if(in[a]) vv[in[a]].pb(i);
        if(out[a]) vv[i].pb(out[a]);
    }
    //REP1(i,cur-1) cout<<i<<' '<<vv[i]<<'\n';
    wk=1;
    REP1(i,cur-1){
        if(!vis[i]) dfs2(i);
    }
    if(wk){
        cout<<"Yes\n";
    }
    else cout<<"No\n";
    REP(i,n) v[i].clear(),in[i]=out[i]=0;
    REP1(i,cur) vv[i].clear(),vis[i]=0;
    REP(i,n) REP(j,20) idx[i][j]=idx2[i][j]=0;
}
int main(){
    int t;
    cin>>t;
    while(t--) solve();
} 
# Verdict Execution time Memory Grader output
1 Correct 93 ms 192860 KB Output is correct
2 Correct 110 ms 192800 KB Output is correct
3 Correct 92 ms 192848 KB Output is correct
4 Correct 132 ms 192944 KB Output is correct
5 Correct 173 ms 192992 KB Output is correct
6 Correct 107 ms 193020 KB Output is correct
7 Correct 100 ms 193056 KB Output is correct
8 Correct 92 ms 193036 KB Output is correct
9 Correct 450 ms 200316 KB Output is correct
10 Correct 1248 ms 372840 KB Output is correct
11 Correct 105 ms 192876 KB Output is correct
12 Correct 208 ms 193032 KB Output is correct
13 Correct 1265 ms 377612 KB Output is correct
14 Correct 1145 ms 377848 KB Output is correct
15 Correct 1665 ms 388180 KB Output is correct
16 Correct 2105 ms 413380 KB Output is correct
17 Correct 1411 ms 381940 KB Output is correct
18 Correct 1264 ms 381632 KB Output is correct
19 Correct 1334 ms 381908 KB Output is correct
20 Correct 1319 ms 381916 KB Output is correct
21 Correct 1431 ms 383164 KB Output is correct
22 Correct 1020 ms 377192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 192820 KB Output is correct
2 Correct 94 ms 192788 KB Output is correct
3 Correct 102 ms 193112 KB Output is correct
4 Correct 97 ms 193020 KB Output is correct
5 Correct 97 ms 193064 KB Output is correct
6 Correct 97 ms 192988 KB Output is correct
7 Correct 101 ms 193020 KB Output is correct
8 Correct 98 ms 192988 KB Output is correct
9 Correct 95 ms 193048 KB Output is correct
10 Correct 96 ms 193060 KB Output is correct
11 Correct 97 ms 193016 KB Output is correct
12 Correct 108 ms 192972 KB Output is correct
13 Correct 93 ms 192976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 192820 KB Output is correct
2 Correct 94 ms 192788 KB Output is correct
3 Correct 102 ms 193112 KB Output is correct
4 Correct 97 ms 193020 KB Output is correct
5 Correct 97 ms 193064 KB Output is correct
6 Correct 97 ms 192988 KB Output is correct
7 Correct 101 ms 193020 KB Output is correct
8 Correct 98 ms 192988 KB Output is correct
9 Correct 95 ms 193048 KB Output is correct
10 Correct 96 ms 193060 KB Output is correct
11 Correct 97 ms 193016 KB Output is correct
12 Correct 108 ms 192972 KB Output is correct
13 Correct 93 ms 192976 KB Output is correct
14 Correct 92 ms 192824 KB Output is correct
15 Correct 99 ms 192892 KB Output is correct
16 Correct 94 ms 193084 KB Output is correct
17 Correct 90 ms 193036 KB Output is correct
18 Correct 93 ms 193108 KB Output is correct
19 Correct 86 ms 192844 KB Output is correct
20 Correct 95 ms 193092 KB Output is correct
21 Correct 104 ms 193048 KB Output is correct
22 Correct 93 ms 193064 KB Output is correct
23 Correct 90 ms 192948 KB Output is correct
24 Correct 92 ms 192868 KB Output is correct
25 Correct 97 ms 193076 KB Output is correct
26 Correct 92 ms 193012 KB Output is correct
27 Correct 92 ms 193016 KB Output is correct
28 Correct 92 ms 192788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 192820 KB Output is correct
2 Correct 94 ms 192788 KB Output is correct
3 Correct 102 ms 193112 KB Output is correct
4 Correct 97 ms 193020 KB Output is correct
5 Correct 97 ms 193064 KB Output is correct
6 Correct 97 ms 192988 KB Output is correct
7 Correct 101 ms 193020 KB Output is correct
8 Correct 98 ms 192988 KB Output is correct
9 Correct 95 ms 193048 KB Output is correct
10 Correct 96 ms 193060 KB Output is correct
11 Correct 97 ms 193016 KB Output is correct
12 Correct 108 ms 192972 KB Output is correct
13 Correct 93 ms 192976 KB Output is correct
14 Correct 92 ms 192824 KB Output is correct
15 Correct 99 ms 192892 KB Output is correct
16 Correct 94 ms 193084 KB Output is correct
17 Correct 90 ms 193036 KB Output is correct
18 Correct 93 ms 193108 KB Output is correct
19 Correct 86 ms 192844 KB Output is correct
20 Correct 95 ms 193092 KB Output is correct
21 Correct 104 ms 193048 KB Output is correct
22 Correct 93 ms 193064 KB Output is correct
23 Correct 90 ms 192948 KB Output is correct
24 Correct 92 ms 192868 KB Output is correct
25 Correct 97 ms 193076 KB Output is correct
26 Correct 92 ms 193012 KB Output is correct
27 Correct 92 ms 193016 KB Output is correct
28 Correct 92 ms 192788 KB Output is correct
29 Correct 95 ms 193112 KB Output is correct
30 Correct 100 ms 193088 KB Output is correct
31 Correct 94 ms 193092 KB Output is correct
32 Correct 93 ms 193048 KB Output is correct
33 Correct 94 ms 193080 KB Output is correct
34 Correct 94 ms 192972 KB Output is correct
35 Correct 100 ms 193068 KB Output is correct
36 Correct 100 ms 193052 KB Output is correct
37 Correct 90 ms 192972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 192820 KB Output is correct
2 Correct 94 ms 192788 KB Output is correct
3 Correct 102 ms 193112 KB Output is correct
4 Correct 97 ms 193020 KB Output is correct
5 Correct 97 ms 193064 KB Output is correct
6 Correct 97 ms 192988 KB Output is correct
7 Correct 101 ms 193020 KB Output is correct
8 Correct 98 ms 192988 KB Output is correct
9 Correct 95 ms 193048 KB Output is correct
10 Correct 96 ms 193060 KB Output is correct
11 Correct 97 ms 193016 KB Output is correct
12 Correct 108 ms 192972 KB Output is correct
13 Correct 93 ms 192976 KB Output is correct
14 Correct 92 ms 192824 KB Output is correct
15 Correct 99 ms 192892 KB Output is correct
16 Correct 94 ms 193084 KB Output is correct
17 Correct 90 ms 193036 KB Output is correct
18 Correct 93 ms 193108 KB Output is correct
19 Correct 86 ms 192844 KB Output is correct
20 Correct 95 ms 193092 KB Output is correct
21 Correct 104 ms 193048 KB Output is correct
22 Correct 93 ms 193064 KB Output is correct
23 Correct 90 ms 192948 KB Output is correct
24 Correct 92 ms 192868 KB Output is correct
25 Correct 97 ms 193076 KB Output is correct
26 Correct 92 ms 193012 KB Output is correct
27 Correct 92 ms 193016 KB Output is correct
28 Correct 92 ms 192788 KB Output is correct
29 Correct 95 ms 193112 KB Output is correct
30 Correct 100 ms 193088 KB Output is correct
31 Correct 94 ms 193092 KB Output is correct
32 Correct 93 ms 193048 KB Output is correct
33 Correct 94 ms 193080 KB Output is correct
34 Correct 94 ms 192972 KB Output is correct
35 Correct 100 ms 193068 KB Output is correct
36 Correct 100 ms 193052 KB Output is correct
37 Correct 90 ms 192972 KB Output is correct
38 Correct 468 ms 200244 KB Output is correct
39 Correct 1170 ms 372840 KB Output is correct
40 Correct 316 ms 200840 KB Output is correct
41 Correct 204 ms 197956 KB Output is correct
42 Correct 218 ms 198840 KB Output is correct
43 Correct 322 ms 200760 KB Output is correct
44 Correct 121 ms 193864 KB Output is correct
45 Correct 444 ms 282624 KB Output is correct
46 Correct 463 ms 282880 KB Output is correct
47 Correct 1004 ms 359032 KB Output is correct
48 Correct 1043 ms 359152 KB Output is correct
49 Correct 595 ms 320228 KB Output is correct
50 Correct 575 ms 320460 KB Output is correct
51 Correct 749 ms 346364 KB Output is correct
52 Correct 771 ms 346376 KB Output is correct
53 Correct 134 ms 198588 KB Output is correct
54 Correct 440 ms 273688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 192892 KB Output is correct
2 Correct 96 ms 192872 KB Output is correct
3 Correct 94 ms 192788 KB Output is correct
4 Correct 89 ms 192856 KB Output is correct
5 Correct 106 ms 192844 KB Output is correct
6 Correct 101 ms 192884 KB Output is correct
7 Correct 97 ms 192992 KB Output is correct
8 Correct 91 ms 192904 KB Output is correct
9 Correct 99 ms 192872 KB Output is correct
10 Correct 92 ms 192884 KB Output is correct
11 Correct 96 ms 192900 KB Output is correct
12 Correct 97 ms 193040 KB Output is correct
13 Correct 165 ms 193060 KB Output is correct
14 Correct 185 ms 193004 KB Output is correct
15 Correct 162 ms 192972 KB Output is correct
16 Correct 364 ms 262224 KB Output is correct
17 Correct 592 ms 273436 KB Output is correct
18 Correct 813 ms 288060 KB Output is correct
19 Correct 437 ms 264860 KB Output is correct
20 Correct 415 ms 263436 KB Output is correct
21 Correct 432 ms 263700 KB Output is correct
22 Correct 537 ms 271444 KB Output is correct
23 Correct 454 ms 271112 KB Output is correct
24 Correct 455 ms 270304 KB Output is correct
25 Correct 447 ms 271264 KB Output is correct
26 Correct 495 ms 271348 KB Output is correct
27 Correct 473 ms 255332 KB Output is correct
28 Correct 422 ms 257504 KB Output is correct
29 Correct 434 ms 255976 KB Output is correct
30 Correct 404 ms 252328 KB Output is correct
31 Correct 410 ms 252868 KB Output is correct
32 Correct 426 ms 251312 KB Output is correct
33 Correct 433 ms 252992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 192860 KB Output is correct
2 Correct 110 ms 192800 KB Output is correct
3 Correct 92 ms 192848 KB Output is correct
4 Correct 132 ms 192944 KB Output is correct
5 Correct 173 ms 192992 KB Output is correct
6 Correct 107 ms 193020 KB Output is correct
7 Correct 100 ms 193056 KB Output is correct
8 Correct 92 ms 193036 KB Output is correct
9 Correct 450 ms 200316 KB Output is correct
10 Correct 1248 ms 372840 KB Output is correct
11 Correct 105 ms 192876 KB Output is correct
12 Correct 208 ms 193032 KB Output is correct
13 Correct 1265 ms 377612 KB Output is correct
14 Correct 1145 ms 377848 KB Output is correct
15 Correct 1665 ms 388180 KB Output is correct
16 Correct 2105 ms 413380 KB Output is correct
17 Correct 1411 ms 381940 KB Output is correct
18 Correct 1264 ms 381632 KB Output is correct
19 Correct 1334 ms 381908 KB Output is correct
20 Correct 1319 ms 381916 KB Output is correct
21 Correct 1431 ms 383164 KB Output is correct
22 Correct 1020 ms 377192 KB Output is correct
23 Correct 106 ms 192820 KB Output is correct
24 Correct 94 ms 192788 KB Output is correct
25 Correct 102 ms 193112 KB Output is correct
26 Correct 97 ms 193020 KB Output is correct
27 Correct 97 ms 193064 KB Output is correct
28 Correct 97 ms 192988 KB Output is correct
29 Correct 101 ms 193020 KB Output is correct
30 Correct 98 ms 192988 KB Output is correct
31 Correct 95 ms 193048 KB Output is correct
32 Correct 96 ms 193060 KB Output is correct
33 Correct 97 ms 193016 KB Output is correct
34 Correct 108 ms 192972 KB Output is correct
35 Correct 93 ms 192976 KB Output is correct
36 Correct 92 ms 192824 KB Output is correct
37 Correct 99 ms 192892 KB Output is correct
38 Correct 94 ms 193084 KB Output is correct
39 Correct 90 ms 193036 KB Output is correct
40 Correct 93 ms 193108 KB Output is correct
41 Correct 86 ms 192844 KB Output is correct
42 Correct 95 ms 193092 KB Output is correct
43 Correct 104 ms 193048 KB Output is correct
44 Correct 93 ms 193064 KB Output is correct
45 Correct 90 ms 192948 KB Output is correct
46 Correct 92 ms 192868 KB Output is correct
47 Correct 97 ms 193076 KB Output is correct
48 Correct 92 ms 193012 KB Output is correct
49 Correct 92 ms 193016 KB Output is correct
50 Correct 92 ms 192788 KB Output is correct
51 Correct 95 ms 193112 KB Output is correct
52 Correct 100 ms 193088 KB Output is correct
53 Correct 94 ms 193092 KB Output is correct
54 Correct 93 ms 193048 KB Output is correct
55 Correct 94 ms 193080 KB Output is correct
56 Correct 94 ms 192972 KB Output is correct
57 Correct 100 ms 193068 KB Output is correct
58 Correct 100 ms 193052 KB Output is correct
59 Correct 90 ms 192972 KB Output is correct
60 Correct 468 ms 200244 KB Output is correct
61 Correct 1170 ms 372840 KB Output is correct
62 Correct 316 ms 200840 KB Output is correct
63 Correct 204 ms 197956 KB Output is correct
64 Correct 218 ms 198840 KB Output is correct
65 Correct 322 ms 200760 KB Output is correct
66 Correct 121 ms 193864 KB Output is correct
67 Correct 444 ms 282624 KB Output is correct
68 Correct 463 ms 282880 KB Output is correct
69 Correct 1004 ms 359032 KB Output is correct
70 Correct 1043 ms 359152 KB Output is correct
71 Correct 595 ms 320228 KB Output is correct
72 Correct 575 ms 320460 KB Output is correct
73 Correct 749 ms 346364 KB Output is correct
74 Correct 771 ms 346376 KB Output is correct
75 Correct 134 ms 198588 KB Output is correct
76 Correct 440 ms 273688 KB Output is correct
77 Correct 89 ms 192892 KB Output is correct
78 Correct 96 ms 192872 KB Output is correct
79 Correct 94 ms 192788 KB Output is correct
80 Correct 89 ms 192856 KB Output is correct
81 Correct 106 ms 192844 KB Output is correct
82 Correct 101 ms 192884 KB Output is correct
83 Correct 97 ms 192992 KB Output is correct
84 Correct 91 ms 192904 KB Output is correct
85 Correct 99 ms 192872 KB Output is correct
86 Correct 92 ms 192884 KB Output is correct
87 Correct 96 ms 192900 KB Output is correct
88 Correct 97 ms 193040 KB Output is correct
89 Correct 165 ms 193060 KB Output is correct
90 Correct 185 ms 193004 KB Output is correct
91 Correct 162 ms 192972 KB Output is correct
92 Correct 364 ms 262224 KB Output is correct
93 Correct 592 ms 273436 KB Output is correct
94 Correct 813 ms 288060 KB Output is correct
95 Correct 437 ms 264860 KB Output is correct
96 Correct 415 ms 263436 KB Output is correct
97 Correct 432 ms 263700 KB Output is correct
98 Correct 537 ms 271444 KB Output is correct
99 Correct 454 ms 271112 KB Output is correct
100 Correct 455 ms 270304 KB Output is correct
101 Correct 447 ms 271264 KB Output is correct
102 Correct 495 ms 271348 KB Output is correct
103 Correct 473 ms 255332 KB Output is correct
104 Correct 422 ms 257504 KB Output is correct
105 Correct 434 ms 255976 KB Output is correct
106 Correct 404 ms 252328 KB Output is correct
107 Correct 410 ms 252868 KB Output is correct
108 Correct 426 ms 251312 KB Output is correct
109 Correct 433 ms 252992 KB Output is correct
110 Correct 179 ms 194208 KB Output is correct
111 Correct 166 ms 193624 KB Output is correct
112 Correct 1571 ms 382556 KB Output is correct
113 Correct 1195 ms 365256 KB Output is correct
114 Correct 1055 ms 313336 KB Output is correct
115 Correct 186 ms 237892 KB Output is correct
116 Correct 476 ms 277688 KB Output is correct
117 Correct 1018 ms 303968 KB Output is correct
118 Correct 420 ms 272908 KB Output is correct
119 Correct 416 ms 273140 KB Output is correct
120 Correct 126 ms 200492 KB Output is correct
121 Correct 651 ms 291340 KB Output is correct
122 Correct 638 ms 287908 KB Output is correct
123 Correct 1362 ms 370064 KB Output is correct
124 Correct 1306 ms 370160 KB Output is correct
125 Correct 1390 ms 370624 KB Output is correct
126 Correct 2015 ms 407008 KB Output is correct
127 Correct 1652 ms 386784 KB Output is correct
128 Correct 1394 ms 378328 KB Output is correct
129 Correct 1460 ms 376256 KB Output is correct
130 Correct 1471 ms 380220 KB Output is correct