Submission #1118877

# Submission time Handle Problem Language Result Execution time Memory
1118877 2024-11-26T10:34:27 Z yusufhocaoglu Swapping Cities (APIO20_swap) C++17
100 / 100
307 ms 74152 KB
#include "swap.h"

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define ll long long
#define pri pair<int,int>
#define prl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pair<int,int>>
#define vpl vector<pair<ll,ll>>
#define re return 0
#define sqrt sqrtl

vi dsu;
vi parent;
vi cnt;
vi sz;
vi weight;
vector<vi> adj;

void make_set(int x) {
    dsu[x] = x;
    parent[x] = x;
    sz[x] = 1;
    cnt[x] = 0;
    weight[x] = 0;
}

int get_root(int u) {
    if (dsu[u]==u) return u;
    return dsu[u] = get_root(dsu[u]);
}
int s;
vp edges;
void unite(int u,int v,int w) {
    make_set(s);
    edges[s] = {u,v};
    u = get_root(u);
    v = get_root(v);
    //cout<<"uniting "<<u<<" and "<<v<<" to "<<s<<endl;

    dsu[u] = dsu[v] = s;
    parent[u] = parent[v] = s;
    adj[s].push_back(u);
    weight[s] = w;
    if (u==v) sz[s]+=sz[u], cnt[s]+=cnt[u];
    else {
        sz[s]+=sz[u]+sz[v];
        cnt[s]+=cnt[u]+cnt[v];
        adj[s].push_back(v);
    }

    s++;
}

vector<vi> adj_mst;

vi stf;
vp chain;

void dfs(int u) {
    if (chain[u].first==-1) stf[u] = u;
    else if (parent[u]!=u) stf[u] = stf[parent[u]];
    else stf[u] = -1;
    for (auto m : adj[u]) {
        dfs(m);
    }
}


pri cc(pri a,pri b,pri c) {
    //chain,chain,edge
    if (a.first == c.first && b.first == c.second) {
        return {a.second,b.second};
    } 
    if (a.second == c.first && b.first == c.second) {
        return {a.first,b.second};
    } 
    if (a.first == c.first && b.second == c.second) {
        return {a.second,b.first};
    } 
    if (a.second == c.first && b.second == c.second) {
        return {a.first,b.first};
    } 
    return {-1,-1};
}

vp ln2;
vp lnf;
vp seg2;
void dfs2(int u,int h) { // on the reachability tree
    lnf[u].first = ln2.size();
    ln2.push_back({h,u});
    for (auto v : adj[u]) {
        dfs2(v,h+1);
        ln2.push_back({h,u});
    }
    if (adj[u].size()==0) chain[u] = {u,u};
    else if (adj[u].size()==1) chain[u] = {-1,-1};
    else if (chain[adj[u][0]].first==-1 || chain[adj[u][1]].first==-1) chain[u]={-1,-1};
    else {
        //each child forms a chain
        //edge is edges[u].first - edges[u].second
        auto uc = chain[adj[u][0]];
        auto vc = chain[adj[u][1]];

        chain[u] = cc(uc,vc,edges[u]);

    }
    lnf[u].second = ln2.size()-1;
}

int lca2(int u,int v) {
    int l = min(lnf[u].first,lnf[v].first);
    int r = max(lnf[u].second,lnf[v].second);
    l+=ln2.size();
    r+=ln2.size();
    r++;
    pri ans = {1e9,1e9};
    for (;l<r;l>>=1,r>>=1) {
        if (l&1) {
            ans = min(ans,seg2[l++]);
        } if (r&1) {
            ans = min(ans,seg2[--r]);
        }
    }
    return ans.second;

}

int getMinimumFuelCapacity(int u,int v) {
    //pri lca1_ = lca(u,v);
    int lca2_ = lca2(u,v);
    //int path_len = node_l[fl[u].first].first+node_l[fl[v].first].first - lca1_.second*2; 
    if (chain[lca2_].first==-1) {
        return weight[lca2_];
    } else {
        //find earliest parent with no chain
        if (stf[lca2_]==-1) return -1;
        else return weight[stf[lca2_]];
    }
}

void init(int n,int m,vector<int> u,vector<int> v,vector<int> w) {
    dsu = vi(m+n+10);
    parent = vi(m+n+10);
    sz = vi(m+n+10);
    cnt = vi(m+n+10);
    adj = vector<vi>(m+n+10);
    weight = vi(m+n+10);

    s = n;

    struct edge {
        int u,v,w;
        bool operator<(const edge& other) const {
            return w<other.w;
        }
    };
    for (int i = 0;i<n;i++){
        make_set(i);
        cnt[i] = 1;
    }
    //cout<<"nigga"<<endl;
    vector<edge> ss;
    vector<edge> edging;
    for (int i = 0;i<m;i++) ss.push_back(edge{u[i],v[i],w[i]});
    sort(ss.begin(),ss.end());
    adj_mst = vector<vi>(n);
    edges = vp(m+n+10);
    for (auto edge : ss) {
        if (get_root(edge.u)!=get_root(edge.v)) {
            adj_mst[edge.u].push_back(edge.v);
            adj_mst[edge.v].push_back(edge.u);
        }
        unite(edge.u,edge.v,edge.w);
    }
    
    //cout<<cnt[s-1]<<endl;
    
    lnf = vp(m+n+10);
    chain = vp(m+n+10);
    dfs2(s-1,0);
    seg2 = vp(2*ln2.size()+2);
    for (int i = 0;i<ln2.size();i++) {
        seg2[i+ln2.size()] = ln2[i];
    }
    for (int i = ln2.size()-1;i>0;i--) {
        seg2[i] = min(seg2[2*i],seg2[2*i+1]);
    }
    stf = vi(m+n+10);
    dfs(s-1);
}

/*int32_t main() {
    vi u = {0,1,2,3,4,5};
    vi v = {1,2,3,4,5,6};
    vi w = {1,1,1,1,1,1};
    init(7, 6, u, v, w);
    cout<<getMinimumFuelCapacity(0,5)<<endl;
    return 0;
}*/

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:189:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  189 |     for (int i = 0;i<ln2.size();i++) {
      |                    ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 2 ms 756 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 1104 KB Output is correct
9 Correct 94 ms 30320 KB Output is correct
10 Correct 106 ms 36800 KB Output is correct
11 Correct 115 ms 36276 KB Output is correct
12 Correct 157 ms 38324 KB Output is correct
13 Correct 97 ms 43840 KB Output is correct
14 Correct 109 ms 30756 KB Output is correct
15 Correct 199 ms 40988 KB Output is correct
16 Correct 195 ms 39828 KB Output is correct
17 Correct 240 ms 42148 KB Output is correct
18 Correct 175 ms 47568 KB Output is correct
19 Correct 88 ms 11208 KB Output is correct
20 Correct 219 ms 41100 KB Output is correct
21 Correct 222 ms 39972 KB Output is correct
22 Correct 233 ms 42516 KB Output is correct
23 Correct 198 ms 47872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 151 ms 50476 KB Output is correct
4 Correct 157 ms 52916 KB Output is correct
5 Correct 175 ms 51420 KB Output is correct
6 Correct 161 ms 52660 KB Output is correct
7 Correct 174 ms 52236 KB Output is correct
8 Correct 178 ms 50192 KB Output is correct
9 Correct 167 ms 51896 KB Output is correct
10 Correct 158 ms 49708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 2 ms 756 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 1104 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 2 ms 592 KB Output is correct
14 Correct 2 ms 592 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 704 KB Output is correct
17 Correct 2 ms 848 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 848 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 2 ms 1104 KB Output is correct
25 Correct 2 ms 1104 KB Output is correct
26 Correct 2 ms 1272 KB Output is correct
27 Correct 1 ms 848 KB Output is correct
28 Correct 3 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 756 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 1104 KB Output is correct
10 Correct 94 ms 30320 KB Output is correct
11 Correct 106 ms 36800 KB Output is correct
12 Correct 115 ms 36276 KB Output is correct
13 Correct 157 ms 38324 KB Output is correct
14 Correct 97 ms 43840 KB Output is correct
15 Correct 2 ms 848 KB Output is correct
16 Correct 2 ms 592 KB Output is correct
17 Correct 2 ms 592 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 704 KB Output is correct
22 Correct 2 ms 848 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 2 ms 896 KB Output is correct
28 Correct 1 ms 592 KB Output is correct
29 Correct 2 ms 1104 KB Output is correct
30 Correct 2 ms 1104 KB Output is correct
31 Correct 2 ms 1272 KB Output is correct
32 Correct 1 ms 848 KB Output is correct
33 Correct 3 ms 1104 KB Output is correct
34 Correct 12 ms 5516 KB Output is correct
35 Correct 132 ms 37960 KB Output is correct
36 Correct 117 ms 37920 KB Output is correct
37 Correct 136 ms 37992 KB Output is correct
38 Correct 125 ms 37600 KB Output is correct
39 Correct 134 ms 37268 KB Output is correct
40 Correct 140 ms 34472 KB Output is correct
41 Correct 141 ms 38324 KB Output is correct
42 Correct 143 ms 38068 KB Output is correct
43 Correct 96 ms 45240 KB Output is correct
44 Correct 138 ms 38324 KB Output is correct
45 Correct 176 ms 55468 KB Output is correct
46 Correct 149 ms 38012 KB Output is correct
47 Correct 134 ms 38344 KB Output is correct
48 Correct 137 ms 44068 KB Output is correct
49 Correct 120 ms 50972 KB Output is correct
50 Correct 81 ms 39292 KB Output is correct
51 Correct 140 ms 47248 KB Output is correct
52 Correct 181 ms 58480 KB Output is correct
53 Correct 211 ms 61608 KB Output is correct
54 Correct 216 ms 70444 KB Output is correct
55 Correct 109 ms 44496 KB Output is correct
56 Correct 187 ms 64940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 2 ms 756 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 1104 KB Output is correct
9 Correct 94 ms 30320 KB Output is correct
10 Correct 106 ms 36800 KB Output is correct
11 Correct 115 ms 36276 KB Output is correct
12 Correct 157 ms 38324 KB Output is correct
13 Correct 97 ms 43840 KB Output is correct
14 Correct 109 ms 30756 KB Output is correct
15 Correct 199 ms 40988 KB Output is correct
16 Correct 195 ms 39828 KB Output is correct
17 Correct 240 ms 42148 KB Output is correct
18 Correct 175 ms 47568 KB Output is correct
19 Correct 151 ms 50476 KB Output is correct
20 Correct 157 ms 52916 KB Output is correct
21 Correct 175 ms 51420 KB Output is correct
22 Correct 161 ms 52660 KB Output is correct
23 Correct 174 ms 52236 KB Output is correct
24 Correct 178 ms 50192 KB Output is correct
25 Correct 167 ms 51896 KB Output is correct
26 Correct 158 ms 49708 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 2 ms 592 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 2 ms 592 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 704 KB Output is correct
34 Correct 2 ms 848 KB Output is correct
35 Correct 1 ms 592 KB Output is correct
36 Correct 12 ms 5516 KB Output is correct
37 Correct 132 ms 37960 KB Output is correct
38 Correct 117 ms 37920 KB Output is correct
39 Correct 136 ms 37992 KB Output is correct
40 Correct 125 ms 37600 KB Output is correct
41 Correct 134 ms 37268 KB Output is correct
42 Correct 140 ms 34472 KB Output is correct
43 Correct 141 ms 38324 KB Output is correct
44 Correct 143 ms 38068 KB Output is correct
45 Correct 96 ms 45240 KB Output is correct
46 Correct 138 ms 38324 KB Output is correct
47 Correct 26 ms 5356 KB Output is correct
48 Correct 234 ms 41888 KB Output is correct
49 Correct 221 ms 41764 KB Output is correct
50 Correct 222 ms 41908 KB Output is correct
51 Correct 217 ms 41652 KB Output is correct
52 Correct 205 ms 39600 KB Output is correct
53 Correct 165 ms 30364 KB Output is correct
54 Correct 245 ms 42480 KB Output is correct
55 Correct 257 ms 41908 KB Output is correct
56 Correct 202 ms 47648 KB Output is correct
57 Correct 228 ms 42628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 756 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 1104 KB Output is correct
10 Correct 94 ms 30320 KB Output is correct
11 Correct 106 ms 36800 KB Output is correct
12 Correct 115 ms 36276 KB Output is correct
13 Correct 157 ms 38324 KB Output is correct
14 Correct 97 ms 43840 KB Output is correct
15 Correct 109 ms 30756 KB Output is correct
16 Correct 199 ms 40988 KB Output is correct
17 Correct 195 ms 39828 KB Output is correct
18 Correct 240 ms 42148 KB Output is correct
19 Correct 175 ms 47568 KB Output is correct
20 Correct 151 ms 50476 KB Output is correct
21 Correct 157 ms 52916 KB Output is correct
22 Correct 175 ms 51420 KB Output is correct
23 Correct 161 ms 52660 KB Output is correct
24 Correct 174 ms 52236 KB Output is correct
25 Correct 178 ms 50192 KB Output is correct
26 Correct 167 ms 51896 KB Output is correct
27 Correct 158 ms 49708 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 2 ms 592 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 2 ms 592 KB Output is correct
32 Correct 2 ms 592 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 1 ms 704 KB Output is correct
35 Correct 2 ms 848 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 12 ms 5516 KB Output is correct
38 Correct 132 ms 37960 KB Output is correct
39 Correct 117 ms 37920 KB Output is correct
40 Correct 136 ms 37992 KB Output is correct
41 Correct 125 ms 37600 KB Output is correct
42 Correct 134 ms 37268 KB Output is correct
43 Correct 140 ms 34472 KB Output is correct
44 Correct 141 ms 38324 KB Output is correct
45 Correct 143 ms 38068 KB Output is correct
46 Correct 96 ms 45240 KB Output is correct
47 Correct 138 ms 38324 KB Output is correct
48 Correct 26 ms 5356 KB Output is correct
49 Correct 234 ms 41888 KB Output is correct
50 Correct 221 ms 41764 KB Output is correct
51 Correct 222 ms 41908 KB Output is correct
52 Correct 217 ms 41652 KB Output is correct
53 Correct 205 ms 39600 KB Output is correct
54 Correct 165 ms 30364 KB Output is correct
55 Correct 245 ms 42480 KB Output is correct
56 Correct 257 ms 41908 KB Output is correct
57 Correct 202 ms 47648 KB Output is correct
58 Correct 228 ms 42628 KB Output is correct
59 Correct 88 ms 11208 KB Output is correct
60 Correct 219 ms 41100 KB Output is correct
61 Correct 222 ms 39972 KB Output is correct
62 Correct 233 ms 42516 KB Output is correct
63 Correct 198 ms 47872 KB Output is correct
64 Correct 2 ms 592 KB Output is correct
65 Correct 2 ms 592 KB Output is correct
66 Correct 2 ms 848 KB Output is correct
67 Correct 2 ms 896 KB Output is correct
68 Correct 1 ms 592 KB Output is correct
69 Correct 2 ms 1104 KB Output is correct
70 Correct 2 ms 1104 KB Output is correct
71 Correct 2 ms 1272 KB Output is correct
72 Correct 1 ms 848 KB Output is correct
73 Correct 3 ms 1104 KB Output is correct
74 Correct 176 ms 55468 KB Output is correct
75 Correct 149 ms 38012 KB Output is correct
76 Correct 134 ms 38344 KB Output is correct
77 Correct 137 ms 44068 KB Output is correct
78 Correct 120 ms 50972 KB Output is correct
79 Correct 81 ms 39292 KB Output is correct
80 Correct 140 ms 47248 KB Output is correct
81 Correct 181 ms 58480 KB Output is correct
82 Correct 211 ms 61608 KB Output is correct
83 Correct 216 ms 70444 KB Output is correct
84 Correct 109 ms 44496 KB Output is correct
85 Correct 187 ms 64940 KB Output is correct
86 Correct 79 ms 20256 KB Output is correct
87 Correct 202 ms 41912 KB Output is correct
88 Correct 206 ms 41944 KB Output is correct
89 Correct 200 ms 46184 KB Output is correct
90 Correct 165 ms 57144 KB Output is correct
91 Correct 167 ms 52916 KB Output is correct
92 Correct 196 ms 49080 KB Output is correct
93 Correct 251 ms 61364 KB Output is correct
94 Correct 300 ms 65528 KB Output is correct
95 Correct 307 ms 74152 KB Output is correct
96 Correct 187 ms 47888 KB Output is correct
97 Correct 235 ms 57268 KB Output is correct