Submission #545032

# Submission time Handle Problem Language Result Execution time Memory
545032 2022-04-03T10:53:49 Z leaked Jail (JOI22_jail) C++14
100 / 100
2212 ms 463540 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int M=1e9+7;
const int N=120'000+1;
const int lg=17;
void add(int &a,int b){
    a+=b;
    if(a>=M) a-=M;
    else if(a<0) a+=M;
}
int mult(int a,int b){
    return 1ll*a*b%M;
}
vec<int> gr[4*N*lg],g[N];
int used[4*N*lg],ok;
int up[N][lg];

int fin[N][lg],sta[N][lg];
void dfs(int v){
    used[v]=1;
    for(auto &z : gr[v]){
        if(used[z]){
            ok&=(used[z]==2);
            continue;
        }
        dfs(z);
    }
    used[v]=2;
}
int par[N],in[N],out[N],tt,ut[N],dt[N];
int id=0;
void dfs1(int v,int p){
    par[v]=p;
    in[v]=tt++;
    ut[v]=(ut[v]==-1?id++:ut[v]);
    dt[v]=(dt[v]==-1?id++:dt[v]);

    up[v][0]=p;

    sta[v][0]=id++;fin[v][0]=id++;

//    cout<<"ADD "<<fin[v][0]<<' '<<dt[p]<<endl;
    gr[fin[v][0]].pb(dt[p]);

    gr[ut[p]].pb(sta[v][0]);
//    cout<<"ADD "<<ut[p]<<' ' <<sta[v][0]<<endl;
//    cout<<"DFS "<<v<<endl;
    for(int j=1;j<lg;j++){
        up[v][j]=up[up[v][j-1]][j-1];
        sta[v][j]=id++;fin[v][j]=id++;
        assert(id<4*N*lg);

//        cout<<"ADDT1 "<<fin[v][j]<<' '<<fin[v][j-1]<<' '<<fin[up[v][j-1]][j-1]<<endl;
        gr[fin[v][j]].pb(fin[v][j-1]);gr[fin[v][j]].pb(fin[up[v][j-1]][j-1]);
//        cout<<"ADDT2 "<<sta[v][j-1]<<' '<<sta[up[v][j-1]][j-1]<<' '<<sta[v][j]<<endl;
        gr[sta[v][j-1]].pb(sta[v][j]);gr[sta[up[v][j-1]][j-1]].pb(sta[v][j]);
    }
//    assert(id<4*N*lg);
    for(auto &z : g[v]){
        if(z==p) continue;
        dfs1(z,v);
    }
    out[v]=tt-1;
}
bool is(int a,int b){
    return in[a]<=in[b]&&out[a]>=out[b];
}
int lca(int a,int b){
    if(is(a,b)) return a;
    for(int i=lg-1;i>=0;i--){
        if(!is(up[a][i],b))
            a=up[a][i];
    }
    return up[a][0];
}
void addway(int a,int b,int j){
    if(is(a,b)) return;
    for(int i=lg-1;i>=0;i--){
        if(!is(up[a][i],b)){
//            cout<<"ADD "<<sta[a][i]<<' '<<j<<endl;
//            cout<<"ADD "<<j<<' '<<fin[a][i]<<endl;
            gr[sta[a][i]].pb(j);
            gr[j].pb(fin[a][i]);
            a=up[a][i];
        }
    }
//    int i=0;
//    gr[j].pb(gu[a][i]);
//    gr[gd[a][i]].pb(j);
}
void solve(){
    int n,m;
    cin>>n;
    for(int i=0;i<n;i++) ut[i]=dt[i]=-1,g[i].clear();
    ok=1;
    for(int i=1;i<n;i++){
        int v,u;
        cin>>v>>u;--v;--u;
        g[v].pb(u);g[u].pb(v);
    }
    cin>>m;
    vec<int> st(m),f(m);
    for(int i=0;i<m;i++) used[i]=0,gr[i].clear();
//    vec<int>ids(m);
    id=m;
    for(int i=0;i<m;i++){
        cin>>st[i]>>f[i];--st[i];--f[i];
        ut[st[i]]=i;dt[f[i]]=i;
    }
    dfs1(0,0);
    ///ut - start , dt - finish
    for(int i=0;i<m;i++){
//        gr[ut[f[i]]].pb(ids[i]);
//        gr[ids[i]].pb(dt[st[i]]);

        int v=st[i],u=f[i];
        int lc=lca(v,u);
        addway(v,lc,i);addway(u,lc,i);

        gr[i].pb(dt[st[i]]);
        gr[ut[f[i]]].pb(i);
//
        if(lc!=v && lc!=u){
            gr[i].pb(dt[lc]);
            gr[ut[lc]].pb(i);
        }
    }
    for(int i=0;i<id;i++){
        if(!used[i])
            dfs(i);
    }
    for(int i=0;i<id;i++) gr[i].clear(),used[i]=0;
    id=0;
    cout<<(ok?"Yes":"No")<<'\n';
}
signed main(){
    fast_resp;
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
4
1 2
2 3
3 4
4 5
3 6
6 7
1
4 1
5 7
*/
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194752 KB Output is correct
2 Correct 88 ms 194696 KB Output is correct
3 Correct 86 ms 194804 KB Output is correct
4 Correct 158 ms 195012 KB Output is correct
5 Correct 210 ms 195120 KB Output is correct
6 Correct 95 ms 195136 KB Output is correct
7 Correct 107 ms 195236 KB Output is correct
8 Correct 96 ms 195200 KB Output is correct
9 Correct 544 ms 204844 KB Output is correct
10 Correct 1486 ms 382444 KB Output is correct
11 Correct 114 ms 194892 KB Output is correct
12 Correct 267 ms 196056 KB Output is correct
13 Correct 1353 ms 385092 KB Output is correct
14 Correct 1242 ms 385096 KB Output is correct
15 Correct 1799 ms 410784 KB Output is correct
16 Correct 2212 ms 463540 KB Output is correct
17 Correct 1653 ms 388216 KB Output is correct
18 Correct 1369 ms 384452 KB Output is correct
19 Correct 1581 ms 388116 KB Output is correct
20 Correct 1645 ms 388316 KB Output is correct
21 Correct 1526 ms 411088 KB Output is correct
22 Correct 1169 ms 383540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194772 KB Output is correct
2 Correct 91 ms 194804 KB Output is correct
3 Correct 97 ms 195228 KB Output is correct
4 Correct 95 ms 195200 KB Output is correct
5 Correct 96 ms 195184 KB Output is correct
6 Correct 101 ms 195212 KB Output is correct
7 Correct 100 ms 195228 KB Output is correct
8 Correct 96 ms 195148 KB Output is correct
9 Correct 95 ms 195212 KB Output is correct
10 Correct 94 ms 195244 KB Output is correct
11 Correct 95 ms 195176 KB Output is correct
12 Correct 109 ms 195232 KB Output is correct
13 Correct 95 ms 195136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194772 KB Output is correct
2 Correct 91 ms 194804 KB Output is correct
3 Correct 97 ms 195228 KB Output is correct
4 Correct 95 ms 195200 KB Output is correct
5 Correct 96 ms 195184 KB Output is correct
6 Correct 101 ms 195212 KB Output is correct
7 Correct 100 ms 195228 KB Output is correct
8 Correct 96 ms 195148 KB Output is correct
9 Correct 95 ms 195212 KB Output is correct
10 Correct 94 ms 195244 KB Output is correct
11 Correct 95 ms 195176 KB Output is correct
12 Correct 109 ms 195232 KB Output is correct
13 Correct 95 ms 195136 KB Output is correct
14 Correct 87 ms 194700 KB Output is correct
15 Correct 89 ms 194732 KB Output is correct
16 Correct 111 ms 195236 KB Output is correct
17 Correct 96 ms 195256 KB Output is correct
18 Correct 95 ms 195260 KB Output is correct
19 Correct 96 ms 194748 KB Output is correct
20 Correct 98 ms 195176 KB Output is correct
21 Correct 100 ms 195164 KB Output is correct
22 Correct 93 ms 195256 KB Output is correct
23 Correct 91 ms 194824 KB Output is correct
24 Correct 109 ms 195008 KB Output is correct
25 Correct 99 ms 195208 KB Output is correct
26 Correct 107 ms 195144 KB Output is correct
27 Correct 94 ms 195264 KB Output is correct
28 Correct 91 ms 194764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194772 KB Output is correct
2 Correct 91 ms 194804 KB Output is correct
3 Correct 97 ms 195228 KB Output is correct
4 Correct 95 ms 195200 KB Output is correct
5 Correct 96 ms 195184 KB Output is correct
6 Correct 101 ms 195212 KB Output is correct
7 Correct 100 ms 195228 KB Output is correct
8 Correct 96 ms 195148 KB Output is correct
9 Correct 95 ms 195212 KB Output is correct
10 Correct 94 ms 195244 KB Output is correct
11 Correct 95 ms 195176 KB Output is correct
12 Correct 109 ms 195232 KB Output is correct
13 Correct 95 ms 195136 KB Output is correct
14 Correct 87 ms 194700 KB Output is correct
15 Correct 89 ms 194732 KB Output is correct
16 Correct 111 ms 195236 KB Output is correct
17 Correct 96 ms 195256 KB Output is correct
18 Correct 95 ms 195260 KB Output is correct
19 Correct 96 ms 194748 KB Output is correct
20 Correct 98 ms 195176 KB Output is correct
21 Correct 100 ms 195164 KB Output is correct
22 Correct 93 ms 195256 KB Output is correct
23 Correct 91 ms 194824 KB Output is correct
24 Correct 109 ms 195008 KB Output is correct
25 Correct 99 ms 195208 KB Output is correct
26 Correct 107 ms 195144 KB Output is correct
27 Correct 94 ms 195264 KB Output is correct
28 Correct 91 ms 194764 KB Output is correct
29 Correct 109 ms 195252 KB Output is correct
30 Correct 108 ms 195332 KB Output is correct
31 Correct 98 ms 195372 KB Output is correct
32 Correct 99 ms 195152 KB Output is correct
33 Correct 100 ms 195196 KB Output is correct
34 Correct 93 ms 195148 KB Output is correct
35 Correct 102 ms 195156 KB Output is correct
36 Correct 97 ms 195184 KB Output is correct
37 Correct 95 ms 195144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194772 KB Output is correct
2 Correct 91 ms 194804 KB Output is correct
3 Correct 97 ms 195228 KB Output is correct
4 Correct 95 ms 195200 KB Output is correct
5 Correct 96 ms 195184 KB Output is correct
6 Correct 101 ms 195212 KB Output is correct
7 Correct 100 ms 195228 KB Output is correct
8 Correct 96 ms 195148 KB Output is correct
9 Correct 95 ms 195212 KB Output is correct
10 Correct 94 ms 195244 KB Output is correct
11 Correct 95 ms 195176 KB Output is correct
12 Correct 109 ms 195232 KB Output is correct
13 Correct 95 ms 195136 KB Output is correct
14 Correct 87 ms 194700 KB Output is correct
15 Correct 89 ms 194732 KB Output is correct
16 Correct 111 ms 195236 KB Output is correct
17 Correct 96 ms 195256 KB Output is correct
18 Correct 95 ms 195260 KB Output is correct
19 Correct 96 ms 194748 KB Output is correct
20 Correct 98 ms 195176 KB Output is correct
21 Correct 100 ms 195164 KB Output is correct
22 Correct 93 ms 195256 KB Output is correct
23 Correct 91 ms 194824 KB Output is correct
24 Correct 109 ms 195008 KB Output is correct
25 Correct 99 ms 195208 KB Output is correct
26 Correct 107 ms 195144 KB Output is correct
27 Correct 94 ms 195264 KB Output is correct
28 Correct 91 ms 194764 KB Output is correct
29 Correct 109 ms 195252 KB Output is correct
30 Correct 108 ms 195332 KB Output is correct
31 Correct 98 ms 195372 KB Output is correct
32 Correct 99 ms 195152 KB Output is correct
33 Correct 100 ms 195196 KB Output is correct
34 Correct 93 ms 195148 KB Output is correct
35 Correct 102 ms 195156 KB Output is correct
36 Correct 97 ms 195184 KB Output is correct
37 Correct 95 ms 195144 KB Output is correct
38 Correct 567 ms 204848 KB Output is correct
39 Correct 1488 ms 382548 KB Output is correct
40 Correct 480 ms 208184 KB Output is correct
41 Correct 363 ms 207328 KB Output is correct
42 Correct 357 ms 207892 KB Output is correct
43 Correct 459 ms 207168 KB Output is correct
44 Correct 124 ms 196816 KB Output is correct
45 Correct 708 ms 376616 KB Output is correct
46 Correct 769 ms 376588 KB Output is correct
47 Correct 1202 ms 376256 KB Output is correct
48 Correct 1245 ms 376160 KB Output is correct
49 Correct 770 ms 377344 KB Output is correct
50 Correct 737 ms 377408 KB Output is correct
51 Correct 869 ms 377412 KB Output is correct
52 Correct 879 ms 377376 KB Output is correct
53 Correct 154 ms 208204 KB Output is correct
54 Correct 667 ms 376592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 194884 KB Output is correct
2 Correct 89 ms 194820 KB Output is correct
3 Correct 91 ms 194764 KB Output is correct
4 Correct 87 ms 194772 KB Output is correct
5 Correct 109 ms 194768 KB Output is correct
6 Correct 90 ms 195148 KB Output is correct
7 Correct 95 ms 195128 KB Output is correct
8 Correct 92 ms 194800 KB Output is correct
9 Correct 89 ms 194804 KB Output is correct
10 Correct 92 ms 195020 KB Output is correct
11 Correct 91 ms 194840 KB Output is correct
12 Correct 97 ms 195104 KB Output is correct
13 Correct 163 ms 195132 KB Output is correct
14 Correct 214 ms 195180 KB Output is correct
15 Correct 183 ms 195028 KB Output is correct
16 Correct 679 ms 375796 KB Output is correct
17 Correct 867 ms 385400 KB Output is correct
18 Correct 1090 ms 401260 KB Output is correct
19 Correct 737 ms 378400 KB Output is correct
20 Correct 732 ms 378404 KB Output is correct
21 Correct 731 ms 378488 KB Output is correct
22 Correct 859 ms 383276 KB Output is correct
23 Correct 775 ms 381672 KB Output is correct
24 Correct 785 ms 381508 KB Output is correct
25 Correct 792 ms 381692 KB Output is correct
26 Correct 810 ms 381752 KB Output is correct
27 Correct 661 ms 383420 KB Output is correct
28 Correct 667 ms 387672 KB Output is correct
29 Correct 653 ms 384636 KB Output is correct
30 Correct 762 ms 384668 KB Output is correct
31 Correct 770 ms 385584 KB Output is correct
32 Correct 738 ms 382780 KB Output is correct
33 Correct 768 ms 385600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 194752 KB Output is correct
2 Correct 88 ms 194696 KB Output is correct
3 Correct 86 ms 194804 KB Output is correct
4 Correct 158 ms 195012 KB Output is correct
5 Correct 210 ms 195120 KB Output is correct
6 Correct 95 ms 195136 KB Output is correct
7 Correct 107 ms 195236 KB Output is correct
8 Correct 96 ms 195200 KB Output is correct
9 Correct 544 ms 204844 KB Output is correct
10 Correct 1486 ms 382444 KB Output is correct
11 Correct 114 ms 194892 KB Output is correct
12 Correct 267 ms 196056 KB Output is correct
13 Correct 1353 ms 385092 KB Output is correct
14 Correct 1242 ms 385096 KB Output is correct
15 Correct 1799 ms 410784 KB Output is correct
16 Correct 2212 ms 463540 KB Output is correct
17 Correct 1653 ms 388216 KB Output is correct
18 Correct 1369 ms 384452 KB Output is correct
19 Correct 1581 ms 388116 KB Output is correct
20 Correct 1645 ms 388316 KB Output is correct
21 Correct 1526 ms 411088 KB Output is correct
22 Correct 1169 ms 383540 KB Output is correct
23 Correct 91 ms 194772 KB Output is correct
24 Correct 91 ms 194804 KB Output is correct
25 Correct 97 ms 195228 KB Output is correct
26 Correct 95 ms 195200 KB Output is correct
27 Correct 96 ms 195184 KB Output is correct
28 Correct 101 ms 195212 KB Output is correct
29 Correct 100 ms 195228 KB Output is correct
30 Correct 96 ms 195148 KB Output is correct
31 Correct 95 ms 195212 KB Output is correct
32 Correct 94 ms 195244 KB Output is correct
33 Correct 95 ms 195176 KB Output is correct
34 Correct 109 ms 195232 KB Output is correct
35 Correct 95 ms 195136 KB Output is correct
36 Correct 87 ms 194700 KB Output is correct
37 Correct 89 ms 194732 KB Output is correct
38 Correct 111 ms 195236 KB Output is correct
39 Correct 96 ms 195256 KB Output is correct
40 Correct 95 ms 195260 KB Output is correct
41 Correct 96 ms 194748 KB Output is correct
42 Correct 98 ms 195176 KB Output is correct
43 Correct 100 ms 195164 KB Output is correct
44 Correct 93 ms 195256 KB Output is correct
45 Correct 91 ms 194824 KB Output is correct
46 Correct 109 ms 195008 KB Output is correct
47 Correct 99 ms 195208 KB Output is correct
48 Correct 107 ms 195144 KB Output is correct
49 Correct 94 ms 195264 KB Output is correct
50 Correct 91 ms 194764 KB Output is correct
51 Correct 109 ms 195252 KB Output is correct
52 Correct 108 ms 195332 KB Output is correct
53 Correct 98 ms 195372 KB Output is correct
54 Correct 99 ms 195152 KB Output is correct
55 Correct 100 ms 195196 KB Output is correct
56 Correct 93 ms 195148 KB Output is correct
57 Correct 102 ms 195156 KB Output is correct
58 Correct 97 ms 195184 KB Output is correct
59 Correct 95 ms 195144 KB Output is correct
60 Correct 567 ms 204848 KB Output is correct
61 Correct 1488 ms 382548 KB Output is correct
62 Correct 480 ms 208184 KB Output is correct
63 Correct 363 ms 207328 KB Output is correct
64 Correct 357 ms 207892 KB Output is correct
65 Correct 459 ms 207168 KB Output is correct
66 Correct 124 ms 196816 KB Output is correct
67 Correct 708 ms 376616 KB Output is correct
68 Correct 769 ms 376588 KB Output is correct
69 Correct 1202 ms 376256 KB Output is correct
70 Correct 1245 ms 376160 KB Output is correct
71 Correct 770 ms 377344 KB Output is correct
72 Correct 737 ms 377408 KB Output is correct
73 Correct 869 ms 377412 KB Output is correct
74 Correct 879 ms 377376 KB Output is correct
75 Correct 154 ms 208204 KB Output is correct
76 Correct 667 ms 376592 KB Output is correct
77 Correct 90 ms 194884 KB Output is correct
78 Correct 89 ms 194820 KB Output is correct
79 Correct 91 ms 194764 KB Output is correct
80 Correct 87 ms 194772 KB Output is correct
81 Correct 109 ms 194768 KB Output is correct
82 Correct 90 ms 195148 KB Output is correct
83 Correct 95 ms 195128 KB Output is correct
84 Correct 92 ms 194800 KB Output is correct
85 Correct 89 ms 194804 KB Output is correct
86 Correct 92 ms 195020 KB Output is correct
87 Correct 91 ms 194840 KB Output is correct
88 Correct 97 ms 195104 KB Output is correct
89 Correct 163 ms 195132 KB Output is correct
90 Correct 214 ms 195180 KB Output is correct
91 Correct 183 ms 195028 KB Output is correct
92 Correct 679 ms 375796 KB Output is correct
93 Correct 867 ms 385400 KB Output is correct
94 Correct 1090 ms 401260 KB Output is correct
95 Correct 737 ms 378400 KB Output is correct
96 Correct 732 ms 378404 KB Output is correct
97 Correct 731 ms 378488 KB Output is correct
98 Correct 859 ms 383276 KB Output is correct
99 Correct 775 ms 381672 KB Output is correct
100 Correct 785 ms 381508 KB Output is correct
101 Correct 792 ms 381692 KB Output is correct
102 Correct 810 ms 381752 KB Output is correct
103 Correct 661 ms 383420 KB Output is correct
104 Correct 667 ms 387672 KB Output is correct
105 Correct 653 ms 384636 KB Output is correct
106 Correct 762 ms 384668 KB Output is correct
107 Correct 770 ms 385584 KB Output is correct
108 Correct 738 ms 382780 KB Output is correct
109 Correct 768 ms 385600 KB Output is correct
110 Correct 227 ms 196696 KB Output is correct
111 Correct 194 ms 195872 KB Output is correct
112 Correct 1644 ms 417188 KB Output is correct
113 Correct 1341 ms 379396 KB Output is correct
114 Correct 1145 ms 409760 KB Output is correct
115 Correct 535 ms 375864 KB Output is correct
116 Correct 729 ms 380672 KB Output is correct
117 Correct 1158 ms 411344 KB Output is correct
118 Correct 679 ms 376832 KB Output is correct
119 Correct 674 ms 376724 KB Output is correct
120 Correct 148 ms 210372 KB Output is correct
121 Correct 922 ms 380588 KB Output is correct
122 Correct 896 ms 380600 KB Output is correct
123 Correct 1484 ms 380532 KB Output is correct
124 Correct 1463 ms 380508 KB Output is correct
125 Correct 1512 ms 380912 KB Output is correct
126 Correct 2031 ms 461340 KB Output is correct
127 Correct 1564 ms 425664 KB Output is correct
128 Correct 1418 ms 426944 KB Output is correct
129 Correct 1418 ms 397596 KB Output is correct
130 Correct 1475 ms 417380 KB Output is correct