답안 #951099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951099 2024-03-21T06:45:30 Z hotboy2703 Jail (JOI22_jail) C++14
100 / 100
2283 ms 563348 KB
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
ll n,m;
const ll MAXN = 1.2e5;
const ll MAXK = 17;
ll cur_node;
namespace CYC{
    vector <ll> g[MAXN*MAXK*2 + MAXN * 5 + 100];
    void add_edge(ll u,ll v){g[u].push_back(v);}
    bool in[MAXN*MAXK*2 + MAXN * 5 + 100];
    bool in_st[MAXN*MAXK*2 + MAXN * 5 + 100];
    bool detect_cycle;
    void init(){
        for (ll i = 1;i <= cur_node;i++){g[i].clear();in[i] = 0;in_st[i] = 0;}
        detect_cycle = 0;
    }
    ll depth=0;
    ll cnt = 0;
    void dfs(ll u){
//        cout<<u<<' '<<depth<<' '<<cnt<<endl;
//        depth++;
//        cnt++;
        in_st[u] = 1;
        in[u] = 1;
        for (auto v:g[u]){
            if (in_st[v])detect_cycle = 1;
            if (in[v])continue;
            dfs(v);
        }
        in_st[u] = 0;
//        depth--;
    }
    void solve(){
        for (ll i = 1;i <= cur_node;i++)if (!in[i])dfs(i);
    }
}
vector <ll> g[MAXN+100];
ll sp[MAXK][MAXN+100];
ll node[MAXK][MAXN+100];
ll path[MAXN+100][2];
ll depth[MAXN+100];
ll lca(ll u,ll v){
    if (depth[u] > depth[v])swap(u,v);
    for (ll j = MAXK-1;j>=0;j--){
        if (depth[sp[j][v]]>=depth[u])v=sp[j][v];
    }
    if (v==u)return u;
    for (ll j = MAXK-1;j>=0;j--){
        if (sp[j][v] != sp[j][u]){
            v=sp[j][v];
            u=sp[j][u];
        }
    }
    return sp[0][u];
}
void dfs(ll u,ll p){
    sp[0][u] = p;
    depth[u] = depth[p]+1;
    for (auto v:g[u]){
        if (v==p)continue;
        dfs(v,u);
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
    ll t;
    cin>>t;
    while (t--){
        cin>>n;
        for (ll i = 1;i <= n; i++){
            g[i].clear();

        }
        for (ll i = 1;i < n;i ++){
            ll u,v;
            cin>>u>>v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        cin>>m;
        for (ll i = 1;i <= m;i ++){
            ll s,t;
            cin>>s>>t;
            path[i][0] = s,path[i][1] = t;
        }
        dfs(1,1);
        for (ll j = 1;j < MAXK;j ++){
            for (ll i = 1;i <= n;i ++){
                sp[j][i] = sp[j-1][sp[j-1][i]];
//                cout<<sp[j][i]<<' ';
            }
//            cout<<'\n';
        }
//        exit(0);
        vector <pll> edge[2];
        cur_node = m + 1;
        for (ll k = 0;k < 2;k ++){
            for (ll i = 1;i <= n;i ++){
                node[0][i] = cur_node++;
            }
            for (ll j = 1;j < MAXK;j ++){
                for (ll i = 1;i <= n;i ++){
                    node[j][i] = cur_node++;
                    edge[k].push_back(MP(node[j][i],node[j-1][i]));
                    edge[k].push_back(MP(node[j][i],node[j-1][sp[j-1][i]]));
                }
            }
            for (ll i = 1;i <= m;i ++){
                edge[k].push_back(MP(node[0][path[i][k]],i));
            }
            for (ll i = 1;i <= m;i ++){
                ll u = path[i][0],v = path[i][1];
                ll x = lca(u,v);
//                cout<<u<<' '<<v<<' '<<x<<'\n';
                bool no_lca = 0;
                if (k==0){
                    if (u != x)u = sp[0][u];
                    else no_lca = 1;
                }
                if (k==1){
                    if (v != x)v = sp[0][v];
                    else no_lca = 1;
                }
                for (ll j = MAXK-1;j>=0;j--){
                    if (depth[u]-MASK(j) >= depth[x]){
//                        cout<<"U "<<i<<' '<<j<<'\n';
                        edge[k].push_back(MP(i,node[j][u]));
                        u = sp[j][u];
                    }
                    if (depth[v]-MASK(j) >= depth[x]){
//                        cout<<"V "<<i<<' '<<j<<'\n';
                        edge[k].push_back(MP(i,node[j][v]));
                        v = sp[j][v];
                    }
                }
                if (!no_lca)edge[k].push_back(MP(i,node[0][x]));
            }
        }
        CYC::init();
        for (ll j = 0;j < 2;j ++){
            for (auto x:edge[j]){
                if (j)swap(x.fi,x.se);
                CYC::add_edge(x.fi,x.se);
            }
        }
//        exit(0);
//        cout<<cur_node<<endl;
//        exit(0);
        CYC::solve();
        cout<<(CYC::detect_cycle?"No":"Yes")<<'\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 132440 KB Output is correct
2 Correct 29 ms 132444 KB Output is correct
3 Correct 30 ms 134488 KB Output is correct
4 Correct 91 ms 132700 KB Output is correct
5 Correct 164 ms 134748 KB Output is correct
6 Correct 36 ms 133212 KB Output is correct
7 Correct 36 ms 135252 KB Output is correct
8 Correct 36 ms 135612 KB Output is correct
9 Correct 315 ms 156824 KB Output is correct
10 Correct 675 ms 408100 KB Output is correct
11 Correct 60 ms 132692 KB Output is correct
12 Correct 213 ms 137332 KB Output is correct
13 Correct 713 ms 431920 KB Output is correct
14 Correct 680 ms 444700 KB Output is correct
15 Correct 1514 ms 473176 KB Output is correct
16 Correct 2265 ms 563232 KB Output is correct
17 Correct 840 ms 444812 KB Output is correct
18 Correct 610 ms 432420 KB Output is correct
19 Correct 821 ms 439324 KB Output is correct
20 Correct 829 ms 439144 KB Output is correct
21 Correct 1193 ms 485132 KB Output is correct
22 Correct 679 ms 418792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 132440 KB Output is correct
2 Correct 30 ms 134492 KB Output is correct
3 Correct 34 ms 135256 KB Output is correct
4 Correct 36 ms 133212 KB Output is correct
5 Correct 40 ms 133276 KB Output is correct
6 Correct 36 ms 133280 KB Output is correct
7 Correct 37 ms 133244 KB Output is correct
8 Correct 37 ms 135312 KB Output is correct
9 Correct 38 ms 133208 KB Output is correct
10 Correct 36 ms 135268 KB Output is correct
11 Correct 37 ms 135264 KB Output is correct
12 Correct 32 ms 133168 KB Output is correct
13 Correct 32 ms 135132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 132440 KB Output is correct
2 Correct 30 ms 134492 KB Output is correct
3 Correct 34 ms 135256 KB Output is correct
4 Correct 36 ms 133212 KB Output is correct
5 Correct 40 ms 133276 KB Output is correct
6 Correct 36 ms 133280 KB Output is correct
7 Correct 37 ms 133244 KB Output is correct
8 Correct 37 ms 135312 KB Output is correct
9 Correct 38 ms 133208 KB Output is correct
10 Correct 36 ms 135268 KB Output is correct
11 Correct 37 ms 135264 KB Output is correct
12 Correct 32 ms 133168 KB Output is correct
13 Correct 32 ms 135132 KB Output is correct
14 Correct 29 ms 132696 KB Output is correct
15 Correct 28 ms 132444 KB Output is correct
16 Correct 36 ms 135216 KB Output is correct
17 Correct 40 ms 135268 KB Output is correct
18 Correct 36 ms 135260 KB Output is correct
19 Correct 29 ms 134492 KB Output is correct
20 Correct 37 ms 135248 KB Output is correct
21 Correct 36 ms 135512 KB Output is correct
22 Correct 38 ms 133276 KB Output is correct
23 Correct 29 ms 134492 KB Output is correct
24 Correct 29 ms 132540 KB Output is correct
25 Correct 36 ms 133248 KB Output is correct
26 Correct 31 ms 132944 KB Output is correct
27 Correct 37 ms 135460 KB Output is correct
28 Correct 29 ms 132444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 132440 KB Output is correct
2 Correct 30 ms 134492 KB Output is correct
3 Correct 34 ms 135256 KB Output is correct
4 Correct 36 ms 133212 KB Output is correct
5 Correct 40 ms 133276 KB Output is correct
6 Correct 36 ms 133280 KB Output is correct
7 Correct 37 ms 133244 KB Output is correct
8 Correct 37 ms 135312 KB Output is correct
9 Correct 38 ms 133208 KB Output is correct
10 Correct 36 ms 135268 KB Output is correct
11 Correct 37 ms 135264 KB Output is correct
12 Correct 32 ms 133168 KB Output is correct
13 Correct 32 ms 135132 KB Output is correct
14 Correct 29 ms 132696 KB Output is correct
15 Correct 28 ms 132444 KB Output is correct
16 Correct 36 ms 135216 KB Output is correct
17 Correct 40 ms 135268 KB Output is correct
18 Correct 36 ms 135260 KB Output is correct
19 Correct 29 ms 134492 KB Output is correct
20 Correct 37 ms 135248 KB Output is correct
21 Correct 36 ms 135512 KB Output is correct
22 Correct 38 ms 133276 KB Output is correct
23 Correct 29 ms 134492 KB Output is correct
24 Correct 29 ms 132540 KB Output is correct
25 Correct 36 ms 133248 KB Output is correct
26 Correct 31 ms 132944 KB Output is correct
27 Correct 37 ms 135460 KB Output is correct
28 Correct 29 ms 132444 KB Output is correct
29 Correct 37 ms 135868 KB Output is correct
30 Correct 38 ms 135896 KB Output is correct
31 Correct 39 ms 133696 KB Output is correct
32 Correct 38 ms 135524 KB Output is correct
33 Correct 38 ms 133224 KB Output is correct
34 Correct 40 ms 133136 KB Output is correct
35 Correct 37 ms 135224 KB Output is correct
36 Correct 35 ms 133312 KB Output is correct
37 Correct 33 ms 135224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 132440 KB Output is correct
2 Correct 30 ms 134492 KB Output is correct
3 Correct 34 ms 135256 KB Output is correct
4 Correct 36 ms 133212 KB Output is correct
5 Correct 40 ms 133276 KB Output is correct
6 Correct 36 ms 133280 KB Output is correct
7 Correct 37 ms 133244 KB Output is correct
8 Correct 37 ms 135312 KB Output is correct
9 Correct 38 ms 133208 KB Output is correct
10 Correct 36 ms 135268 KB Output is correct
11 Correct 37 ms 135264 KB Output is correct
12 Correct 32 ms 133168 KB Output is correct
13 Correct 32 ms 135132 KB Output is correct
14 Correct 29 ms 132696 KB Output is correct
15 Correct 28 ms 132444 KB Output is correct
16 Correct 36 ms 135216 KB Output is correct
17 Correct 40 ms 135268 KB Output is correct
18 Correct 36 ms 135260 KB Output is correct
19 Correct 29 ms 134492 KB Output is correct
20 Correct 37 ms 135248 KB Output is correct
21 Correct 36 ms 135512 KB Output is correct
22 Correct 38 ms 133276 KB Output is correct
23 Correct 29 ms 134492 KB Output is correct
24 Correct 29 ms 132540 KB Output is correct
25 Correct 36 ms 133248 KB Output is correct
26 Correct 31 ms 132944 KB Output is correct
27 Correct 37 ms 135460 KB Output is correct
28 Correct 29 ms 132444 KB Output is correct
29 Correct 37 ms 135868 KB Output is correct
30 Correct 38 ms 135896 KB Output is correct
31 Correct 39 ms 133696 KB Output is correct
32 Correct 38 ms 135524 KB Output is correct
33 Correct 38 ms 133224 KB Output is correct
34 Correct 40 ms 133136 KB Output is correct
35 Correct 37 ms 135224 KB Output is correct
36 Correct 35 ms 133312 KB Output is correct
37 Correct 33 ms 135224 KB Output is correct
38 Correct 323 ms 158436 KB Output is correct
39 Correct 657 ms 408208 KB Output is correct
40 Correct 345 ms 158356 KB Output is correct
41 Correct 311 ms 154936 KB Output is correct
42 Correct 278 ms 155660 KB Output is correct
43 Correct 280 ms 158372 KB Output is correct
44 Correct 74 ms 136432 KB Output is correct
45 Correct 652 ms 417832 KB Output is correct
46 Correct 642 ms 418028 KB Output is correct
47 Correct 668 ms 416912 KB Output is correct
48 Correct 667 ms 416856 KB Output is correct
49 Correct 613 ms 419624 KB Output is correct
50 Correct 626 ms 419368 KB Output is correct
51 Correct 605 ms 419480 KB Output is correct
52 Correct 628 ms 419680 KB Output is correct
53 Correct 115 ms 158548 KB Output is correct
54 Correct 666 ms 419488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 134488 KB Output is correct
2 Correct 28 ms 132440 KB Output is correct
3 Correct 31 ms 132432 KB Output is correct
4 Correct 28 ms 134228 KB Output is correct
5 Correct 54 ms 134620 KB Output is correct
6 Correct 31 ms 135128 KB Output is correct
7 Correct 31 ms 135124 KB Output is correct
8 Correct 30 ms 134480 KB Output is correct
9 Correct 30 ms 134532 KB Output is correct
10 Correct 31 ms 132692 KB Output is correct
11 Correct 29 ms 132444 KB Output is correct
12 Correct 42 ms 133300 KB Output is correct
13 Correct 125 ms 135060 KB Output is correct
14 Correct 187 ms 134920 KB Output is correct
15 Correct 166 ms 133944 KB Output is correct
16 Correct 864 ms 420120 KB Output is correct
17 Correct 1380 ms 443444 KB Output is correct
18 Correct 1434 ms 479996 KB Output is correct
19 Correct 791 ms 424900 KB Output is correct
20 Correct 786 ms 424744 KB Output is correct
21 Correct 789 ms 425144 KB Output is correct
22 Correct 1067 ms 440168 KB Output is correct
23 Correct 974 ms 438924 KB Output is correct
24 Correct 1082 ms 438420 KB Output is correct
25 Correct 1128 ms 438400 KB Output is correct
26 Correct 1167 ms 438336 KB Output is correct
27 Correct 1630 ms 450976 KB Output is correct
28 Correct 1519 ms 453772 KB Output is correct
29 Correct 1660 ms 449456 KB Output is correct
30 Correct 909 ms 437228 KB Output is correct
31 Correct 863 ms 438764 KB Output is correct
32 Correct 1084 ms 437984 KB Output is correct
33 Correct 1173 ms 439820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 132440 KB Output is correct
2 Correct 29 ms 132444 KB Output is correct
3 Correct 30 ms 134488 KB Output is correct
4 Correct 91 ms 132700 KB Output is correct
5 Correct 164 ms 134748 KB Output is correct
6 Correct 36 ms 133212 KB Output is correct
7 Correct 36 ms 135252 KB Output is correct
8 Correct 36 ms 135612 KB Output is correct
9 Correct 315 ms 156824 KB Output is correct
10 Correct 675 ms 408100 KB Output is correct
11 Correct 60 ms 132692 KB Output is correct
12 Correct 213 ms 137332 KB Output is correct
13 Correct 713 ms 431920 KB Output is correct
14 Correct 680 ms 444700 KB Output is correct
15 Correct 1514 ms 473176 KB Output is correct
16 Correct 2265 ms 563232 KB Output is correct
17 Correct 840 ms 444812 KB Output is correct
18 Correct 610 ms 432420 KB Output is correct
19 Correct 821 ms 439324 KB Output is correct
20 Correct 829 ms 439144 KB Output is correct
21 Correct 1193 ms 485132 KB Output is correct
22 Correct 679 ms 418792 KB Output is correct
23 Correct 28 ms 132440 KB Output is correct
24 Correct 30 ms 134492 KB Output is correct
25 Correct 34 ms 135256 KB Output is correct
26 Correct 36 ms 133212 KB Output is correct
27 Correct 40 ms 133276 KB Output is correct
28 Correct 36 ms 133280 KB Output is correct
29 Correct 37 ms 133244 KB Output is correct
30 Correct 37 ms 135312 KB Output is correct
31 Correct 38 ms 133208 KB Output is correct
32 Correct 36 ms 135268 KB Output is correct
33 Correct 37 ms 135264 KB Output is correct
34 Correct 32 ms 133168 KB Output is correct
35 Correct 32 ms 135132 KB Output is correct
36 Correct 29 ms 132696 KB Output is correct
37 Correct 28 ms 132444 KB Output is correct
38 Correct 36 ms 135216 KB Output is correct
39 Correct 40 ms 135268 KB Output is correct
40 Correct 36 ms 135260 KB Output is correct
41 Correct 29 ms 134492 KB Output is correct
42 Correct 37 ms 135248 KB Output is correct
43 Correct 36 ms 135512 KB Output is correct
44 Correct 38 ms 133276 KB Output is correct
45 Correct 29 ms 134492 KB Output is correct
46 Correct 29 ms 132540 KB Output is correct
47 Correct 36 ms 133248 KB Output is correct
48 Correct 31 ms 132944 KB Output is correct
49 Correct 37 ms 135460 KB Output is correct
50 Correct 29 ms 132444 KB Output is correct
51 Correct 37 ms 135868 KB Output is correct
52 Correct 38 ms 135896 KB Output is correct
53 Correct 39 ms 133696 KB Output is correct
54 Correct 38 ms 135524 KB Output is correct
55 Correct 38 ms 133224 KB Output is correct
56 Correct 40 ms 133136 KB Output is correct
57 Correct 37 ms 135224 KB Output is correct
58 Correct 35 ms 133312 KB Output is correct
59 Correct 33 ms 135224 KB Output is correct
60 Correct 323 ms 158436 KB Output is correct
61 Correct 657 ms 408208 KB Output is correct
62 Correct 345 ms 158356 KB Output is correct
63 Correct 311 ms 154936 KB Output is correct
64 Correct 278 ms 155660 KB Output is correct
65 Correct 280 ms 158372 KB Output is correct
66 Correct 74 ms 136432 KB Output is correct
67 Correct 652 ms 417832 KB Output is correct
68 Correct 642 ms 418028 KB Output is correct
69 Correct 668 ms 416912 KB Output is correct
70 Correct 667 ms 416856 KB Output is correct
71 Correct 613 ms 419624 KB Output is correct
72 Correct 626 ms 419368 KB Output is correct
73 Correct 605 ms 419480 KB Output is correct
74 Correct 628 ms 419680 KB Output is correct
75 Correct 115 ms 158548 KB Output is correct
76 Correct 666 ms 419488 KB Output is correct
77 Correct 29 ms 134488 KB Output is correct
78 Correct 28 ms 132440 KB Output is correct
79 Correct 31 ms 132432 KB Output is correct
80 Correct 28 ms 134228 KB Output is correct
81 Correct 54 ms 134620 KB Output is correct
82 Correct 31 ms 135128 KB Output is correct
83 Correct 31 ms 135124 KB Output is correct
84 Correct 30 ms 134480 KB Output is correct
85 Correct 30 ms 134532 KB Output is correct
86 Correct 31 ms 132692 KB Output is correct
87 Correct 29 ms 132444 KB Output is correct
88 Correct 42 ms 133300 KB Output is correct
89 Correct 125 ms 135060 KB Output is correct
90 Correct 187 ms 134920 KB Output is correct
91 Correct 166 ms 133944 KB Output is correct
92 Correct 864 ms 420120 KB Output is correct
93 Correct 1380 ms 443444 KB Output is correct
94 Correct 1434 ms 479996 KB Output is correct
95 Correct 791 ms 424900 KB Output is correct
96 Correct 786 ms 424744 KB Output is correct
97 Correct 789 ms 425144 KB Output is correct
98 Correct 1067 ms 440168 KB Output is correct
99 Correct 974 ms 438924 KB Output is correct
100 Correct 1082 ms 438420 KB Output is correct
101 Correct 1128 ms 438400 KB Output is correct
102 Correct 1167 ms 438336 KB Output is correct
103 Correct 1630 ms 450976 KB Output is correct
104 Correct 1519 ms 453772 KB Output is correct
105 Correct 1660 ms 449456 KB Output is correct
106 Correct 909 ms 437228 KB Output is correct
107 Correct 863 ms 438764 KB Output is correct
108 Correct 1084 ms 437984 KB Output is correct
109 Correct 1173 ms 439820 KB Output is correct
110 Correct 243 ms 155004 KB Output is correct
111 Correct 150 ms 154100 KB Output is correct
112 Correct 1687 ms 487924 KB Output is correct
113 Correct 1112 ms 430188 KB Output is correct
114 Correct 1174 ms 467852 KB Output is correct
115 Correct 490 ms 418568 KB Output is correct
116 Correct 1283 ms 431472 KB Output is correct
117 Correct 1794 ms 496544 KB Output is correct
118 Correct 666 ms 420248 KB Output is correct
119 Correct 611 ms 420200 KB Output is correct
120 Correct 90 ms 177992 KB Output is correct
121 Correct 1294 ms 436692 KB Output is correct
122 Correct 1191 ms 436996 KB Output is correct
123 Correct 1174 ms 432420 KB Output is correct
124 Correct 1170 ms 432524 KB Output is correct
125 Correct 1238 ms 434144 KB Output is correct
126 Correct 2283 ms 563348 KB Output is correct
127 Correct 1312 ms 506536 KB Output is correct
128 Correct 1047 ms 499964 KB Output is correct
129 Correct 1020 ms 472748 KB Output is correct
130 Correct 1054 ms 500884 KB Output is correct