Submission #977265

# Submission time Handle Problem Language Result Execution time Memory
977265 2024-05-07T15:22:28 Z imarn Swapping Cities (APIO20_swap) C++14
100 / 100
276 ms 65476 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define sz(x) (ll)x.size()
using namespace std;
const int mxn=3e5+5;
vector<int>g[mxn];
int pr[mxn]{0},cur,d[mxn]{0},dp[mxn]{0},dep[mxn]{0},up[mxn][20]{0};
bool cyc[mxn]{0},ok=0;
int get(int r){
    return pr[r]==r?r:pr[r]=get(pr[r]);
}
void dfs(int u,int p,int l){
    up[u][0]=p;dep[u]=l;
    for(int i=1;i<20;i++)up[u][i]=up[up[u][i-1]][i-1];
    for(auto v:g[u]){
        if(v==p)continue;
        if(!cyc[v])dp[v]=dp[u];
        dfs(v,u,l+1);
    }
}
int qr(int a,int b){
    if(dep[a]>dep[b])swap(a,b);
    for(int i=19;i>=0;i--)if(dep[b]-(1<<i)>=dep[a])b=up[b][i];
    if(a==b)return dp[b];
    for(int i=19;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
    return dp[up[a][0]];
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W){
    cur=N;iota(pr,pr+mxn,0);vector<pair<int,pii>>ed;
    for(int i=0;i<M;i++){
        ed.pb({W[i],{U[i],V[i]}});
    }sort(ed.begin(),ed.end());
    for(auto it : ed){
        d[it.s.f]++;d[it.s.s]++;
        if(get(it.s.f)==get(it.s.s)){
            g[cur].pb(get(it.s.f));pr[get(it.s.f)]=cur;
            dp[cur]=it.f;cyc[cur++]=1;
        }else{
            int u=get(it.s.f),v=get(it.s.s);
            g[cur].pb(u);g[cur].pb(v);
            pr[u]=pr[v]=cur;dp[cur]=it.f;
            if(cyc[u]||cyc[v]||d[it.s.f]>=3||d[it.s.s]>=3)cyc[cur]=1;
            cur++;
        }
    }cur--;dfs(cur,cur,0);if(cyc[cur])ok=1;
}
int getMinimumFuelCapacity(int X, int Y){
    if(!ok)return -1;
    else return qr(X,Y);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15272 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15192 KB Output is correct
7 Correct 4 ms 15196 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 72 ms 32676 KB Output is correct
10 Correct 113 ms 35784 KB Output is correct
11 Correct 89 ms 35688 KB Output is correct
12 Correct 97 ms 36072 KB Output is correct
13 Correct 79 ms 39904 KB Output is correct
14 Correct 71 ms 32720 KB Output is correct
15 Correct 132 ms 37508 KB Output is correct
16 Correct 128 ms 37224 KB Output is correct
17 Correct 146 ms 37732 KB Output is correct
18 Correct 120 ms 41400 KB Output is correct
19 Correct 68 ms 21592 KB Output is correct
20 Correct 202 ms 37576 KB Output is correct
21 Correct 161 ms 37684 KB Output is correct
22 Correct 187 ms 37944 KB Output is correct
23 Correct 197 ms 41872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15272 KB Output is correct
3 Correct 177 ms 45068 KB Output is correct
4 Correct 177 ms 45716 KB Output is correct
5 Correct 175 ms 49448 KB Output is correct
6 Correct 164 ms 49348 KB Output is correct
7 Correct 177 ms 49600 KB Output is correct
8 Correct 186 ms 48984 KB Output is correct
9 Correct 188 ms 49388 KB Output is correct
10 Correct 182 ms 48864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15272 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15192 KB Output is correct
7 Correct 4 ms 15196 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 4 ms 15196 KB Output is correct
11 Correct 4 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 4 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 4 ms 15196 KB Output is correct
16 Correct 3 ms 15192 KB Output is correct
17 Correct 4 ms 15196 KB Output is correct
18 Correct 4 ms 15296 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15196 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 5 ms 15524 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 4 ms 15260 KB Output is correct
25 Correct 4 ms 15452 KB Output is correct
26 Correct 4 ms 15452 KB Output is correct
27 Correct 4 ms 15196 KB Output is correct
28 Correct 4 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15272 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15192 KB Output is correct
8 Correct 4 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 72 ms 32676 KB Output is correct
11 Correct 113 ms 35784 KB Output is correct
12 Correct 89 ms 35688 KB Output is correct
13 Correct 97 ms 36072 KB Output is correct
14 Correct 79 ms 39904 KB Output is correct
15 Correct 4 ms 15196 KB Output is correct
16 Correct 4 ms 15196 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 4 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 4 ms 15196 KB Output is correct
21 Correct 3 ms 15192 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 4 ms 15296 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 5 ms 15524 KB Output is correct
28 Correct 3 ms 15196 KB Output is correct
29 Correct 4 ms 15260 KB Output is correct
30 Correct 4 ms 15452 KB Output is correct
31 Correct 4 ms 15452 KB Output is correct
32 Correct 4 ms 15196 KB Output is correct
33 Correct 4 ms 15452 KB Output is correct
34 Correct 14 ms 18456 KB Output is correct
35 Correct 103 ms 38012 KB Output is correct
36 Correct 97 ms 37836 KB Output is correct
37 Correct 95 ms 38096 KB Output is correct
38 Correct 92 ms 37828 KB Output is correct
39 Correct 101 ms 38724 KB Output is correct
40 Correct 106 ms 35272 KB Output is correct
41 Correct 98 ms 38088 KB Output is correct
42 Correct 92 ms 37876 KB Output is correct
43 Correct 79 ms 43204 KB Output is correct
44 Correct 102 ms 38560 KB Output is correct
45 Correct 117 ms 53032 KB Output is correct
46 Correct 92 ms 37828 KB Output is correct
47 Correct 98 ms 38164 KB Output is correct
48 Correct 123 ms 41988 KB Output is correct
49 Correct 87 ms 54440 KB Output is correct
50 Correct 70 ms 42952 KB Output is correct
51 Correct 102 ms 47428 KB Output is correct
52 Correct 140 ms 54484 KB Output is correct
53 Correct 161 ms 56512 KB Output is correct
54 Correct 163 ms 60604 KB Output is correct
55 Correct 82 ms 42780 KB Output is correct
56 Correct 140 ms 59328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15272 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15192 KB Output is correct
7 Correct 4 ms 15196 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 72 ms 32676 KB Output is correct
10 Correct 113 ms 35784 KB Output is correct
11 Correct 89 ms 35688 KB Output is correct
12 Correct 97 ms 36072 KB Output is correct
13 Correct 79 ms 39904 KB Output is correct
14 Correct 71 ms 32720 KB Output is correct
15 Correct 132 ms 37508 KB Output is correct
16 Correct 128 ms 37224 KB Output is correct
17 Correct 146 ms 37732 KB Output is correct
18 Correct 120 ms 41400 KB Output is correct
19 Correct 177 ms 45068 KB Output is correct
20 Correct 177 ms 45716 KB Output is correct
21 Correct 175 ms 49448 KB Output is correct
22 Correct 164 ms 49348 KB Output is correct
23 Correct 177 ms 49600 KB Output is correct
24 Correct 186 ms 48984 KB Output is correct
25 Correct 188 ms 49388 KB Output is correct
26 Correct 182 ms 48864 KB Output is correct
27 Correct 4 ms 15196 KB Output is correct
28 Correct 4 ms 15196 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 4 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 4 ms 15196 KB Output is correct
33 Correct 3 ms 15192 KB Output is correct
34 Correct 4 ms 15196 KB Output is correct
35 Correct 4 ms 15296 KB Output is correct
36 Correct 14 ms 18456 KB Output is correct
37 Correct 103 ms 38012 KB Output is correct
38 Correct 97 ms 37836 KB Output is correct
39 Correct 95 ms 38096 KB Output is correct
40 Correct 92 ms 37828 KB Output is correct
41 Correct 101 ms 38724 KB Output is correct
42 Correct 106 ms 35272 KB Output is correct
43 Correct 98 ms 38088 KB Output is correct
44 Correct 92 ms 37876 KB Output is correct
45 Correct 79 ms 43204 KB Output is correct
46 Correct 102 ms 38560 KB Output is correct
47 Correct 18 ms 16772 KB Output is correct
48 Correct 176 ms 41668 KB Output is correct
49 Correct 183 ms 41876 KB Output is correct
50 Correct 169 ms 41900 KB Output is correct
51 Correct 184 ms 41924 KB Output is correct
52 Correct 162 ms 41408 KB Output is correct
53 Correct 147 ms 36496 KB Output is correct
54 Correct 210 ms 42572 KB Output is correct
55 Correct 205 ms 41672 KB Output is correct
56 Correct 199 ms 46020 KB Output is correct
57 Correct 184 ms 42668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15272 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15192 KB Output is correct
8 Correct 4 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 72 ms 32676 KB Output is correct
11 Correct 113 ms 35784 KB Output is correct
12 Correct 89 ms 35688 KB Output is correct
13 Correct 97 ms 36072 KB Output is correct
14 Correct 79 ms 39904 KB Output is correct
15 Correct 71 ms 32720 KB Output is correct
16 Correct 132 ms 37508 KB Output is correct
17 Correct 128 ms 37224 KB Output is correct
18 Correct 146 ms 37732 KB Output is correct
19 Correct 120 ms 41400 KB Output is correct
20 Correct 177 ms 45068 KB Output is correct
21 Correct 177 ms 45716 KB Output is correct
22 Correct 175 ms 49448 KB Output is correct
23 Correct 164 ms 49348 KB Output is correct
24 Correct 177 ms 49600 KB Output is correct
25 Correct 186 ms 48984 KB Output is correct
26 Correct 188 ms 49388 KB Output is correct
27 Correct 182 ms 48864 KB Output is correct
28 Correct 4 ms 15196 KB Output is correct
29 Correct 4 ms 15196 KB Output is correct
30 Correct 3 ms 15196 KB Output is correct
31 Correct 4 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 4 ms 15196 KB Output is correct
34 Correct 3 ms 15192 KB Output is correct
35 Correct 4 ms 15196 KB Output is correct
36 Correct 4 ms 15296 KB Output is correct
37 Correct 14 ms 18456 KB Output is correct
38 Correct 103 ms 38012 KB Output is correct
39 Correct 97 ms 37836 KB Output is correct
40 Correct 95 ms 38096 KB Output is correct
41 Correct 92 ms 37828 KB Output is correct
42 Correct 101 ms 38724 KB Output is correct
43 Correct 106 ms 35272 KB Output is correct
44 Correct 98 ms 38088 KB Output is correct
45 Correct 92 ms 37876 KB Output is correct
46 Correct 79 ms 43204 KB Output is correct
47 Correct 102 ms 38560 KB Output is correct
48 Correct 18 ms 16772 KB Output is correct
49 Correct 176 ms 41668 KB Output is correct
50 Correct 183 ms 41876 KB Output is correct
51 Correct 169 ms 41900 KB Output is correct
52 Correct 184 ms 41924 KB Output is correct
53 Correct 162 ms 41408 KB Output is correct
54 Correct 147 ms 36496 KB Output is correct
55 Correct 210 ms 42572 KB Output is correct
56 Correct 205 ms 41672 KB Output is correct
57 Correct 199 ms 46020 KB Output is correct
58 Correct 184 ms 42668 KB Output is correct
59 Correct 68 ms 21592 KB Output is correct
60 Correct 202 ms 37576 KB Output is correct
61 Correct 161 ms 37684 KB Output is correct
62 Correct 187 ms 37944 KB Output is correct
63 Correct 197 ms 41872 KB Output is correct
64 Correct 3 ms 15196 KB Output is correct
65 Correct 3 ms 15196 KB Output is correct
66 Correct 3 ms 15196 KB Output is correct
67 Correct 5 ms 15524 KB Output is correct
68 Correct 3 ms 15196 KB Output is correct
69 Correct 4 ms 15260 KB Output is correct
70 Correct 4 ms 15452 KB Output is correct
71 Correct 4 ms 15452 KB Output is correct
72 Correct 4 ms 15196 KB Output is correct
73 Correct 4 ms 15452 KB Output is correct
74 Correct 117 ms 53032 KB Output is correct
75 Correct 92 ms 37828 KB Output is correct
76 Correct 98 ms 38164 KB Output is correct
77 Correct 123 ms 41988 KB Output is correct
78 Correct 87 ms 54440 KB Output is correct
79 Correct 70 ms 42952 KB Output is correct
80 Correct 102 ms 47428 KB Output is correct
81 Correct 140 ms 54484 KB Output is correct
82 Correct 161 ms 56512 KB Output is correct
83 Correct 163 ms 60604 KB Output is correct
84 Correct 82 ms 42780 KB Output is correct
85 Correct 140 ms 59328 KB Output is correct
86 Correct 59 ms 28812 KB Output is correct
87 Correct 177 ms 41924 KB Output is correct
88 Correct 198 ms 41924 KB Output is correct
89 Correct 200 ms 45648 KB Output is correct
90 Correct 166 ms 58564 KB Output is correct
91 Correct 178 ms 55504 KB Output is correct
92 Correct 190 ms 50376 KB Output is correct
93 Correct 220 ms 57148 KB Output is correct
94 Correct 276 ms 60080 KB Output is correct
95 Correct 265 ms 65476 KB Output is correct
96 Correct 190 ms 46480 KB Output is correct
97 Correct 254 ms 54204 KB Output is correct