답안 #951125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951125 2024-03-21T07:39:03 Z hotboy2703 Jail (JOI22_jail) C++17
100 / 100
2379 ms 624432 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].emplace_back(v);}
    ll in[MAXN*MAXK*2 + MAXN * 5 + 100];
    ll in_st[MAXN*MAXK*2 + MAXN * 5 + 100];
    ll 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;
    }
    void dfs(ll u){
        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;
    }
    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]];
            }
        }
        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);
                ll 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]){
                        edge[k].push_back(MP(i,node[j][u]));
                        u = sp[j][u];
                    }
                    if (depth[v]-MASK(j) >= depth[x]){
                        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();
        ll cnt_edge = 0;
        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);
                cnt_edge++;
            }
        }
        CYC::solve();
        cout<<(CYC::detect_cycle?"No":"Yes")<<'\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 153292 KB Output is correct
2 Correct 31 ms 153356 KB Output is correct
3 Correct 34 ms 153328 KB Output is correct
4 Correct 97 ms 153720 KB Output is correct
5 Correct 178 ms 153684 KB Output is correct
6 Correct 38 ms 154032 KB Output is correct
7 Correct 42 ms 154140 KB Output is correct
8 Correct 43 ms 154300 KB Output is correct
9 Correct 333 ms 178988 KB Output is correct
10 Correct 705 ms 466880 KB Output is correct
11 Correct 57 ms 153444 KB Output is correct
12 Correct 226 ms 154996 KB Output is correct
13 Correct 679 ms 489496 KB Output is correct
14 Correct 687 ms 502616 KB Output is correct
15 Correct 1559 ms 530596 KB Output is correct
16 Correct 2379 ms 624432 KB Output is correct
17 Correct 784 ms 507060 KB Output is correct
18 Correct 705 ms 488640 KB Output is correct
19 Correct 818 ms 496924 KB Output is correct
20 Correct 765 ms 500392 KB Output is correct
21 Correct 1235 ms 542728 KB Output is correct
22 Correct 667 ms 476252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 153352 KB Output is correct
2 Correct 30 ms 153180 KB Output is correct
3 Correct 38 ms 154064 KB Output is correct
4 Correct 45 ms 154104 KB Output is correct
5 Correct 39 ms 154020 KB Output is correct
6 Correct 43 ms 154076 KB Output is correct
7 Correct 41 ms 154052 KB Output is correct
8 Correct 41 ms 154100 KB Output is correct
9 Correct 41 ms 154136 KB Output is correct
10 Correct 48 ms 154052 KB Output is correct
11 Correct 38 ms 154080 KB Output is correct
12 Correct 35 ms 153972 KB Output is correct
13 Correct 34 ms 153812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 153352 KB Output is correct
2 Correct 30 ms 153180 KB Output is correct
3 Correct 38 ms 154064 KB Output is correct
4 Correct 45 ms 154104 KB Output is correct
5 Correct 39 ms 154020 KB Output is correct
6 Correct 43 ms 154076 KB Output is correct
7 Correct 41 ms 154052 KB Output is correct
8 Correct 41 ms 154100 KB Output is correct
9 Correct 41 ms 154136 KB Output is correct
10 Correct 48 ms 154052 KB Output is correct
11 Correct 38 ms 154080 KB Output is correct
12 Correct 35 ms 153972 KB Output is correct
13 Correct 34 ms 153812 KB Output is correct
14 Correct 32 ms 153180 KB Output is correct
15 Correct 33 ms 153360 KB Output is correct
16 Correct 39 ms 154060 KB Output is correct
17 Correct 39 ms 154128 KB Output is correct
18 Correct 44 ms 154280 KB Output is correct
19 Correct 33 ms 153180 KB Output is correct
20 Correct 40 ms 154064 KB Output is correct
21 Correct 38 ms 154076 KB Output is correct
22 Correct 38 ms 154052 KB Output is correct
23 Correct 31 ms 153388 KB Output is correct
24 Correct 32 ms 153428 KB Output is correct
25 Correct 39 ms 154108 KB Output is correct
26 Correct 33 ms 153948 KB Output is correct
27 Correct 37 ms 154096 KB Output is correct
28 Correct 31 ms 153292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 153352 KB Output is correct
2 Correct 30 ms 153180 KB Output is correct
3 Correct 38 ms 154064 KB Output is correct
4 Correct 45 ms 154104 KB Output is correct
5 Correct 39 ms 154020 KB Output is correct
6 Correct 43 ms 154076 KB Output is correct
7 Correct 41 ms 154052 KB Output is correct
8 Correct 41 ms 154100 KB Output is correct
9 Correct 41 ms 154136 KB Output is correct
10 Correct 48 ms 154052 KB Output is correct
11 Correct 38 ms 154080 KB Output is correct
12 Correct 35 ms 153972 KB Output is correct
13 Correct 34 ms 153812 KB Output is correct
14 Correct 32 ms 153180 KB Output is correct
15 Correct 33 ms 153360 KB Output is correct
16 Correct 39 ms 154060 KB Output is correct
17 Correct 39 ms 154128 KB Output is correct
18 Correct 44 ms 154280 KB Output is correct
19 Correct 33 ms 153180 KB Output is correct
20 Correct 40 ms 154064 KB Output is correct
21 Correct 38 ms 154076 KB Output is correct
22 Correct 38 ms 154052 KB Output is correct
23 Correct 31 ms 153388 KB Output is correct
24 Correct 32 ms 153428 KB Output is correct
25 Correct 39 ms 154108 KB Output is correct
26 Correct 33 ms 153948 KB Output is correct
27 Correct 37 ms 154096 KB Output is correct
28 Correct 31 ms 153292 KB Output is correct
29 Correct 39 ms 154300 KB Output is correct
30 Correct 42 ms 154620 KB Output is correct
31 Correct 39 ms 154728 KB Output is correct
32 Correct 41 ms 154316 KB Output is correct
33 Correct 39 ms 154076 KB Output is correct
34 Correct 37 ms 154264 KB Output is correct
35 Correct 39 ms 154028 KB Output is correct
36 Correct 37 ms 154164 KB Output is correct
37 Correct 39 ms 154020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 153352 KB Output is correct
2 Correct 30 ms 153180 KB Output is correct
3 Correct 38 ms 154064 KB Output is correct
4 Correct 45 ms 154104 KB Output is correct
5 Correct 39 ms 154020 KB Output is correct
6 Correct 43 ms 154076 KB Output is correct
7 Correct 41 ms 154052 KB Output is correct
8 Correct 41 ms 154100 KB Output is correct
9 Correct 41 ms 154136 KB Output is correct
10 Correct 48 ms 154052 KB Output is correct
11 Correct 38 ms 154080 KB Output is correct
12 Correct 35 ms 153972 KB Output is correct
13 Correct 34 ms 153812 KB Output is correct
14 Correct 32 ms 153180 KB Output is correct
15 Correct 33 ms 153360 KB Output is correct
16 Correct 39 ms 154060 KB Output is correct
17 Correct 39 ms 154128 KB Output is correct
18 Correct 44 ms 154280 KB Output is correct
19 Correct 33 ms 153180 KB Output is correct
20 Correct 40 ms 154064 KB Output is correct
21 Correct 38 ms 154076 KB Output is correct
22 Correct 38 ms 154052 KB Output is correct
23 Correct 31 ms 153388 KB Output is correct
24 Correct 32 ms 153428 KB Output is correct
25 Correct 39 ms 154108 KB Output is correct
26 Correct 33 ms 153948 KB Output is correct
27 Correct 37 ms 154096 KB Output is correct
28 Correct 31 ms 153292 KB Output is correct
29 Correct 39 ms 154300 KB Output is correct
30 Correct 42 ms 154620 KB Output is correct
31 Correct 39 ms 154728 KB Output is correct
32 Correct 41 ms 154316 KB Output is correct
33 Correct 39 ms 154076 KB Output is correct
34 Correct 37 ms 154264 KB Output is correct
35 Correct 39 ms 154028 KB Output is correct
36 Correct 37 ms 154164 KB Output is correct
37 Correct 39 ms 154020 KB Output is correct
38 Correct 395 ms 177656 KB Output is correct
39 Correct 737 ms 464712 KB Output is correct
40 Correct 382 ms 179432 KB Output is correct
41 Correct 322 ms 173656 KB Output is correct
42 Correct 276 ms 176836 KB Output is correct
43 Correct 338 ms 181128 KB Output is correct
44 Correct 77 ms 156900 KB Output is correct
45 Correct 738 ms 474552 KB Output is correct
46 Correct 664 ms 474508 KB Output is correct
47 Correct 676 ms 474612 KB Output is correct
48 Correct 662 ms 473948 KB Output is correct
49 Correct 586 ms 480448 KB Output is correct
50 Correct 556 ms 477980 KB Output is correct
51 Correct 575 ms 476924 KB Output is correct
52 Correct 624 ms 480360 KB Output is correct
53 Correct 155 ms 179272 KB Output is correct
54 Correct 623 ms 476972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 153168 KB Output is correct
2 Correct 31 ms 153168 KB Output is correct
3 Correct 32 ms 153180 KB Output is correct
4 Correct 31 ms 153176 KB Output is correct
5 Correct 58 ms 153468 KB Output is correct
6 Correct 34 ms 153812 KB Output is correct
7 Correct 34 ms 153812 KB Output is correct
8 Correct 31 ms 153212 KB Output is correct
9 Correct 33 ms 153156 KB Output is correct
10 Correct 31 ms 153576 KB Output is correct
11 Correct 32 ms 153176 KB Output is correct
12 Correct 37 ms 154168 KB Output is correct
13 Correct 127 ms 153900 KB Output is correct
14 Correct 209 ms 154032 KB Output is correct
15 Correct 166 ms 153880 KB Output is correct
16 Correct 896 ms 478136 KB Output is correct
17 Correct 1564 ms 500068 KB Output is correct
18 Correct 1687 ms 542408 KB Output is correct
19 Correct 753 ms 481820 KB Output is correct
20 Correct 829 ms 483284 KB Output is correct
21 Correct 802 ms 484596 KB Output is correct
22 Correct 1016 ms 498996 KB Output is correct
23 Correct 1024 ms 495436 KB Output is correct
24 Correct 1217 ms 495064 KB Output is correct
25 Correct 1320 ms 497136 KB Output is correct
26 Correct 1145 ms 494884 KB Output is correct
27 Correct 2017 ms 510024 KB Output is correct
28 Correct 1500 ms 514308 KB Output is correct
29 Correct 1735 ms 511532 KB Output is correct
30 Correct 1057 ms 493452 KB Output is correct
31 Correct 1036 ms 495960 KB Output is correct
32 Correct 1292 ms 495776 KB Output is correct
33 Correct 1276 ms 498344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 153292 KB Output is correct
2 Correct 31 ms 153356 KB Output is correct
3 Correct 34 ms 153328 KB Output is correct
4 Correct 97 ms 153720 KB Output is correct
5 Correct 178 ms 153684 KB Output is correct
6 Correct 38 ms 154032 KB Output is correct
7 Correct 42 ms 154140 KB Output is correct
8 Correct 43 ms 154300 KB Output is correct
9 Correct 333 ms 178988 KB Output is correct
10 Correct 705 ms 466880 KB Output is correct
11 Correct 57 ms 153444 KB Output is correct
12 Correct 226 ms 154996 KB Output is correct
13 Correct 679 ms 489496 KB Output is correct
14 Correct 687 ms 502616 KB Output is correct
15 Correct 1559 ms 530596 KB Output is correct
16 Correct 2379 ms 624432 KB Output is correct
17 Correct 784 ms 507060 KB Output is correct
18 Correct 705 ms 488640 KB Output is correct
19 Correct 818 ms 496924 KB Output is correct
20 Correct 765 ms 500392 KB Output is correct
21 Correct 1235 ms 542728 KB Output is correct
22 Correct 667 ms 476252 KB Output is correct
23 Correct 32 ms 153352 KB Output is correct
24 Correct 30 ms 153180 KB Output is correct
25 Correct 38 ms 154064 KB Output is correct
26 Correct 45 ms 154104 KB Output is correct
27 Correct 39 ms 154020 KB Output is correct
28 Correct 43 ms 154076 KB Output is correct
29 Correct 41 ms 154052 KB Output is correct
30 Correct 41 ms 154100 KB Output is correct
31 Correct 41 ms 154136 KB Output is correct
32 Correct 48 ms 154052 KB Output is correct
33 Correct 38 ms 154080 KB Output is correct
34 Correct 35 ms 153972 KB Output is correct
35 Correct 34 ms 153812 KB Output is correct
36 Correct 32 ms 153180 KB Output is correct
37 Correct 33 ms 153360 KB Output is correct
38 Correct 39 ms 154060 KB Output is correct
39 Correct 39 ms 154128 KB Output is correct
40 Correct 44 ms 154280 KB Output is correct
41 Correct 33 ms 153180 KB Output is correct
42 Correct 40 ms 154064 KB Output is correct
43 Correct 38 ms 154076 KB Output is correct
44 Correct 38 ms 154052 KB Output is correct
45 Correct 31 ms 153388 KB Output is correct
46 Correct 32 ms 153428 KB Output is correct
47 Correct 39 ms 154108 KB Output is correct
48 Correct 33 ms 153948 KB Output is correct
49 Correct 37 ms 154096 KB Output is correct
50 Correct 31 ms 153292 KB Output is correct
51 Correct 39 ms 154300 KB Output is correct
52 Correct 42 ms 154620 KB Output is correct
53 Correct 39 ms 154728 KB Output is correct
54 Correct 41 ms 154316 KB Output is correct
55 Correct 39 ms 154076 KB Output is correct
56 Correct 37 ms 154264 KB Output is correct
57 Correct 39 ms 154028 KB Output is correct
58 Correct 37 ms 154164 KB Output is correct
59 Correct 39 ms 154020 KB Output is correct
60 Correct 395 ms 177656 KB Output is correct
61 Correct 737 ms 464712 KB Output is correct
62 Correct 382 ms 179432 KB Output is correct
63 Correct 322 ms 173656 KB Output is correct
64 Correct 276 ms 176836 KB Output is correct
65 Correct 338 ms 181128 KB Output is correct
66 Correct 77 ms 156900 KB Output is correct
67 Correct 738 ms 474552 KB Output is correct
68 Correct 664 ms 474508 KB Output is correct
69 Correct 676 ms 474612 KB Output is correct
70 Correct 662 ms 473948 KB Output is correct
71 Correct 586 ms 480448 KB Output is correct
72 Correct 556 ms 477980 KB Output is correct
73 Correct 575 ms 476924 KB Output is correct
74 Correct 624 ms 480360 KB Output is correct
75 Correct 155 ms 179272 KB Output is correct
76 Correct 623 ms 476972 KB Output is correct
77 Correct 34 ms 153168 KB Output is correct
78 Correct 31 ms 153168 KB Output is correct
79 Correct 32 ms 153180 KB Output is correct
80 Correct 31 ms 153176 KB Output is correct
81 Correct 58 ms 153468 KB Output is correct
82 Correct 34 ms 153812 KB Output is correct
83 Correct 34 ms 153812 KB Output is correct
84 Correct 31 ms 153212 KB Output is correct
85 Correct 33 ms 153156 KB Output is correct
86 Correct 31 ms 153576 KB Output is correct
87 Correct 32 ms 153176 KB Output is correct
88 Correct 37 ms 154168 KB Output is correct
89 Correct 127 ms 153900 KB Output is correct
90 Correct 209 ms 154032 KB Output is correct
91 Correct 166 ms 153880 KB Output is correct
92 Correct 896 ms 478136 KB Output is correct
93 Correct 1564 ms 500068 KB Output is correct
94 Correct 1687 ms 542408 KB Output is correct
95 Correct 753 ms 481820 KB Output is correct
96 Correct 829 ms 483284 KB Output is correct
97 Correct 802 ms 484596 KB Output is correct
98 Correct 1016 ms 498996 KB Output is correct
99 Correct 1024 ms 495436 KB Output is correct
100 Correct 1217 ms 495064 KB Output is correct
101 Correct 1320 ms 497136 KB Output is correct
102 Correct 1145 ms 494884 KB Output is correct
103 Correct 2017 ms 510024 KB Output is correct
104 Correct 1500 ms 514308 KB Output is correct
105 Correct 1735 ms 511532 KB Output is correct
106 Correct 1057 ms 493452 KB Output is correct
107 Correct 1036 ms 495960 KB Output is correct
108 Correct 1292 ms 495776 KB Output is correct
109 Correct 1276 ms 498344 KB Output is correct
110 Correct 221 ms 154708 KB Output is correct
111 Correct 157 ms 154116 KB Output is correct
112 Correct 1804 ms 543824 KB Output is correct
113 Correct 1092 ms 490232 KB Output is correct
114 Correct 1152 ms 526324 KB Output is correct
115 Correct 451 ms 477252 KB Output is correct
116 Correct 1255 ms 492536 KB Output is correct
117 Correct 1715 ms 553988 KB Output is correct
118 Correct 625 ms 481216 KB Output is correct
119 Correct 608 ms 481024 KB Output is correct
120 Correct 92 ms 183600 KB Output is correct
121 Correct 1323 ms 495192 KB Output is correct
122 Correct 1294 ms 496780 KB Output is correct
123 Correct 1221 ms 489200 KB Output is correct
124 Correct 1191 ms 492332 KB Output is correct
125 Correct 1274 ms 492540 KB Output is correct
126 Correct 2361 ms 622460 KB Output is correct
127 Correct 1251 ms 565184 KB Output is correct
128 Correct 1029 ms 560284 KB Output is correct
129 Correct 971 ms 532712 KB Output is correct
130 Correct 1046 ms 559928 KB Output is correct