Submission #973056

# Submission time Handle Problem Language Result Execution time Memory
973056 2024-05-01T12:59:10 Z Younis_Dwai Swapping Cities (APIO20_swap) C++14
100 / 100
309 ms 67412 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define in insert
#define F first
#define S second
vector<int> adj[300009];
int par1[300009],par[300009][21],depth[300009],cnt[300009],nxt=0,good[300009],val[3000009];
int get_par(int node){
    if(par1[node]==node) return node;
    return par1[node]=get_par(par1[node]);
}
void merg(int u,  int v ,int w){
     int uu=get_par(u);
     int vv=get_par(v);
     nxt++;
     val[nxt]=w;
     if(uu==vv){
        adj[nxt].pb(uu);
        cnt[u]++;
        cnt[v]++;
        good[nxt]=1;
        par1[uu]=nxt;
        return ;
     }
     adj[nxt].pb(uu);
     adj[nxt].pb(vv);
     cnt[u]++;
     cnt[v]++;
     par1[uu]=nxt;
     par1[vv]=nxt;
     if(cnt[u]>=3 || cnt[v]>=3) good[nxt]=1;
     return ;
}
void dfs(int node , int p){
     par[node][0]=p;
     for(int i=1;i<=20;i++) par[node][i]=par[par[node][i-1]][i-1];
     for(auto u : adj[node]){
         depth[u]=1+depth[node];
         dfs(u,node);
         good[node]|=good[u];
     }
     //cout<<" # "<<node<<' '<<good[node]<<' '<<val[node]<<endl;
     return ;
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
     nxt=N-1;
     for(int i=0;i<N+M;i++) par1[i]=i;
     vector<pair<int,pair<int,int>>> v;
     for(int i=0;i<M;i++){
         v.pb({W[i],{U[i],V[i]}});
     }
     sort(v.begin(),v.end());
     for(auto u : v){
         merg(u.S.F,u.S.S,u.F);
     }
     dfs(nxt,nxt);
     return ;
}
int lca(int u , int v){
    if(depth[v]>depth[u]) swap(u,v);
    for(int i=20;i>=0;i--) if(depth[u]-(1<<i)>=depth[v]) u=par[u][i];
    if(u==v) return u;
    for(int i=20;i>=0;i--) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
    return par[u][0];
}
int getMinimumFuelCapacity(int X, int Y) {
    int lc=lca(X,Y);
    //cout<<" @ "<<X<<' '<<Y<<' '<<lc<<endl;
    if(good[lc]==1) return val[lc];
    for(int i=20;i>=0;i--) if(good[par[lc][i]]==0) lc=par[lc][i];
    lc=par[lc][0];
    if(good[lc]) return val[lc];
    return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13400 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 4 ms 13404 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13400 KB Output is correct
9 Correct 91 ms 32808 KB Output is correct
10 Correct 102 ms 38328 KB Output is correct
11 Correct 108 ms 36100 KB Output is correct
12 Correct 116 ms 38440 KB Output is correct
13 Correct 95 ms 42436 KB Output is correct
14 Correct 123 ms 32984 KB Output is correct
15 Correct 231 ms 39880 KB Output is correct
16 Correct 214 ms 37552 KB Output is correct
17 Correct 232 ms 40188 KB Output is correct
18 Correct 284 ms 44012 KB Output is correct
19 Correct 84 ms 21868 KB Output is correct
20 Correct 192 ms 40132 KB Output is correct
21 Correct 226 ms 39736 KB Output is correct
22 Correct 305 ms 40592 KB Output is correct
23 Correct 252 ms 44436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 239 ms 47112 KB Output is correct
4 Correct 183 ms 49556 KB Output is correct
5 Correct 193 ms 49700 KB Output is correct
6 Correct 184 ms 49568 KB Output is correct
7 Correct 191 ms 49600 KB Output is correct
8 Correct 188 ms 46880 KB Output is correct
9 Correct 233 ms 49448 KB Output is correct
10 Correct 202 ms 46904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13400 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 4 ms 13404 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13400 KB Output is correct
9 Correct 4 ms 15448 KB Output is correct
10 Correct 3 ms 15448 KB Output is correct
11 Correct 4 ms 15448 KB Output is correct
12 Correct 4 ms 15452 KB Output is correct
13 Correct 3 ms 15452 KB Output is correct
14 Correct 3 ms 15452 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15448 KB Output is correct
18 Correct 4 ms 15452 KB Output is correct
19 Correct 5 ms 15448 KB Output is correct
20 Correct 3 ms 15452 KB Output is correct
21 Correct 4 ms 15452 KB Output is correct
22 Correct 4 ms 15708 KB Output is correct
23 Correct 3 ms 15452 KB Output is correct
24 Correct 4 ms 15768 KB Output is correct
25 Correct 5 ms 15708 KB Output is correct
26 Correct 4 ms 15708 KB Output is correct
27 Correct 3 ms 15452 KB Output is correct
28 Correct 4 ms 15904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15448 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13400 KB Output is correct
5 Correct 3 ms 13404 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 4 ms 13404 KB Output is correct
9 Correct 4 ms 13400 KB Output is correct
10 Correct 91 ms 32808 KB Output is correct
11 Correct 102 ms 38328 KB Output is correct
12 Correct 108 ms 36100 KB Output is correct
13 Correct 116 ms 38440 KB Output is correct
14 Correct 95 ms 42436 KB Output is correct
15 Correct 3 ms 15448 KB Output is correct
16 Correct 4 ms 15448 KB Output is correct
17 Correct 4 ms 15452 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15452 KB Output is correct
21 Correct 3 ms 15452 KB Output is correct
22 Correct 3 ms 15448 KB Output is correct
23 Correct 4 ms 15452 KB Output is correct
24 Correct 5 ms 15448 KB Output is correct
25 Correct 3 ms 15452 KB Output is correct
26 Correct 4 ms 15452 KB Output is correct
27 Correct 4 ms 15708 KB Output is correct
28 Correct 3 ms 15452 KB Output is correct
29 Correct 4 ms 15768 KB Output is correct
30 Correct 5 ms 15708 KB Output is correct
31 Correct 4 ms 15708 KB Output is correct
32 Correct 3 ms 15452 KB Output is correct
33 Correct 4 ms 15904 KB Output is correct
34 Correct 11 ms 18456 KB Output is correct
35 Correct 117 ms 40208 KB Output is correct
36 Correct 103 ms 40132 KB Output is correct
37 Correct 96 ms 40220 KB Output is correct
38 Correct 111 ms 40136 KB Output is correct
39 Correct 104 ms 40004 KB Output is correct
40 Correct 94 ms 37316 KB Output is correct
41 Correct 97 ms 38596 KB Output is correct
42 Correct 101 ms 38368 KB Output is correct
43 Correct 86 ms 45328 KB Output is correct
44 Correct 121 ms 40196 KB Output is correct
45 Correct 129 ms 52672 KB Output is correct
46 Correct 99 ms 41928 KB Output is correct
47 Correct 103 ms 42180 KB Output is correct
48 Correct 107 ms 46204 KB Output is correct
49 Correct 90 ms 55440 KB Output is correct
50 Correct 72 ms 46688 KB Output is correct
51 Correct 131 ms 51688 KB Output is correct
52 Correct 140 ms 55060 KB Output is correct
53 Correct 173 ms 60100 KB Output is correct
54 Correct 173 ms 63548 KB Output is correct
55 Correct 113 ms 46648 KB Output is correct
56 Correct 159 ms 62916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13400 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 4 ms 13404 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13400 KB Output is correct
9 Correct 91 ms 32808 KB Output is correct
10 Correct 102 ms 38328 KB Output is correct
11 Correct 108 ms 36100 KB Output is correct
12 Correct 116 ms 38440 KB Output is correct
13 Correct 95 ms 42436 KB Output is correct
14 Correct 123 ms 32984 KB Output is correct
15 Correct 231 ms 39880 KB Output is correct
16 Correct 214 ms 37552 KB Output is correct
17 Correct 232 ms 40188 KB Output is correct
18 Correct 284 ms 44012 KB Output is correct
19 Correct 239 ms 47112 KB Output is correct
20 Correct 183 ms 49556 KB Output is correct
21 Correct 193 ms 49700 KB Output is correct
22 Correct 184 ms 49568 KB Output is correct
23 Correct 191 ms 49600 KB Output is correct
24 Correct 188 ms 46880 KB Output is correct
25 Correct 233 ms 49448 KB Output is correct
26 Correct 202 ms 46904 KB Output is correct
27 Correct 3 ms 15448 KB Output is correct
28 Correct 4 ms 15448 KB Output is correct
29 Correct 4 ms 15452 KB Output is correct
30 Correct 3 ms 15452 KB Output is correct
31 Correct 3 ms 15452 KB Output is correct
32 Correct 3 ms 15452 KB Output is correct
33 Correct 3 ms 15452 KB Output is correct
34 Correct 3 ms 15448 KB Output is correct
35 Correct 4 ms 15452 KB Output is correct
36 Correct 11 ms 18456 KB Output is correct
37 Correct 117 ms 40208 KB Output is correct
38 Correct 103 ms 40132 KB Output is correct
39 Correct 96 ms 40220 KB Output is correct
40 Correct 111 ms 40136 KB Output is correct
41 Correct 104 ms 40004 KB Output is correct
42 Correct 94 ms 37316 KB Output is correct
43 Correct 97 ms 38596 KB Output is correct
44 Correct 101 ms 38368 KB Output is correct
45 Correct 86 ms 45328 KB Output is correct
46 Correct 121 ms 40196 KB Output is correct
47 Correct 19 ms 18564 KB Output is correct
48 Correct 211 ms 41712 KB Output is correct
49 Correct 193 ms 41668 KB Output is correct
50 Correct 182 ms 41712 KB Output is correct
51 Correct 180 ms 41692 KB Output is correct
52 Correct 182 ms 39340 KB Output is correct
53 Correct 142 ms 34272 KB Output is correct
54 Correct 196 ms 40616 KB Output is correct
55 Correct 235 ms 40136 KB Output is correct
56 Correct 248 ms 45768 KB Output is correct
57 Correct 192 ms 42408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15448 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13400 KB Output is correct
5 Correct 3 ms 13404 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 4 ms 13404 KB Output is correct
9 Correct 4 ms 13400 KB Output is correct
10 Correct 91 ms 32808 KB Output is correct
11 Correct 102 ms 38328 KB Output is correct
12 Correct 108 ms 36100 KB Output is correct
13 Correct 116 ms 38440 KB Output is correct
14 Correct 95 ms 42436 KB Output is correct
15 Correct 123 ms 32984 KB Output is correct
16 Correct 231 ms 39880 KB Output is correct
17 Correct 214 ms 37552 KB Output is correct
18 Correct 232 ms 40188 KB Output is correct
19 Correct 284 ms 44012 KB Output is correct
20 Correct 239 ms 47112 KB Output is correct
21 Correct 183 ms 49556 KB Output is correct
22 Correct 193 ms 49700 KB Output is correct
23 Correct 184 ms 49568 KB Output is correct
24 Correct 191 ms 49600 KB Output is correct
25 Correct 188 ms 46880 KB Output is correct
26 Correct 233 ms 49448 KB Output is correct
27 Correct 202 ms 46904 KB Output is correct
28 Correct 3 ms 15448 KB Output is correct
29 Correct 4 ms 15448 KB Output is correct
30 Correct 4 ms 15452 KB Output is correct
31 Correct 3 ms 15452 KB Output is correct
32 Correct 3 ms 15452 KB Output is correct
33 Correct 3 ms 15452 KB Output is correct
34 Correct 3 ms 15452 KB Output is correct
35 Correct 3 ms 15448 KB Output is correct
36 Correct 4 ms 15452 KB Output is correct
37 Correct 11 ms 18456 KB Output is correct
38 Correct 117 ms 40208 KB Output is correct
39 Correct 103 ms 40132 KB Output is correct
40 Correct 96 ms 40220 KB Output is correct
41 Correct 111 ms 40136 KB Output is correct
42 Correct 104 ms 40004 KB Output is correct
43 Correct 94 ms 37316 KB Output is correct
44 Correct 97 ms 38596 KB Output is correct
45 Correct 101 ms 38368 KB Output is correct
46 Correct 86 ms 45328 KB Output is correct
47 Correct 121 ms 40196 KB Output is correct
48 Correct 19 ms 18564 KB Output is correct
49 Correct 211 ms 41712 KB Output is correct
50 Correct 193 ms 41668 KB Output is correct
51 Correct 182 ms 41712 KB Output is correct
52 Correct 180 ms 41692 KB Output is correct
53 Correct 182 ms 39340 KB Output is correct
54 Correct 142 ms 34272 KB Output is correct
55 Correct 196 ms 40616 KB Output is correct
56 Correct 235 ms 40136 KB Output is correct
57 Correct 248 ms 45768 KB Output is correct
58 Correct 192 ms 42408 KB Output is correct
59 Correct 84 ms 21868 KB Output is correct
60 Correct 192 ms 40132 KB Output is correct
61 Correct 226 ms 39736 KB Output is correct
62 Correct 305 ms 40592 KB Output is correct
63 Correct 252 ms 44436 KB Output is correct
64 Correct 5 ms 15448 KB Output is correct
65 Correct 3 ms 15452 KB Output is correct
66 Correct 4 ms 15452 KB Output is correct
67 Correct 4 ms 15708 KB Output is correct
68 Correct 3 ms 15452 KB Output is correct
69 Correct 4 ms 15768 KB Output is correct
70 Correct 5 ms 15708 KB Output is correct
71 Correct 4 ms 15708 KB Output is correct
72 Correct 3 ms 15452 KB Output is correct
73 Correct 4 ms 15904 KB Output is correct
74 Correct 129 ms 52672 KB Output is correct
75 Correct 99 ms 41928 KB Output is correct
76 Correct 103 ms 42180 KB Output is correct
77 Correct 107 ms 46204 KB Output is correct
78 Correct 90 ms 55440 KB Output is correct
79 Correct 72 ms 46688 KB Output is correct
80 Correct 131 ms 51688 KB Output is correct
81 Correct 140 ms 55060 KB Output is correct
82 Correct 173 ms 60100 KB Output is correct
83 Correct 173 ms 63548 KB Output is correct
84 Correct 113 ms 46648 KB Output is correct
85 Correct 159 ms 62916 KB Output is correct
86 Correct 63 ms 31180 KB Output is correct
87 Correct 179 ms 45896 KB Output is correct
88 Correct 180 ms 45768 KB Output is correct
89 Correct 224 ms 49608 KB Output is correct
90 Correct 172 ms 62020 KB Output is correct
91 Correct 169 ms 57284 KB Output is correct
92 Correct 205 ms 52164 KB Output is correct
93 Correct 234 ms 59072 KB Output is correct
94 Correct 285 ms 64220 KB Output is correct
95 Correct 287 ms 67412 KB Output is correct
96 Correct 218 ms 50240 KB Output is correct
97 Correct 309 ms 56256 KB Output is correct