Submission #392062

# Submission time Handle Problem Language Result Execution time Memory
392062 2021-04-20T11:23:29 Z Jarif_Rahman Swapping Cities (APIO20_swap) C++17
6 / 100
253 ms 34220 KB
#include "swap.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1.5e9;

struct segtree{
    
    int k;
    vector<pair<int, int>> mn;
    segtree(int n){
        k = 1;
        while(k <= n) k*=2; k*=2;
        mn.assign(k, {inf,0});
        for(int i = 0; i < k/2; i++) mn[i+k/2].sc = i;
        for(int i = k/2; i > 0; i--) mn[i].sc = mn[2*i].sc;
    }
    void upd(int in, pair<int, int> x){
        in+=k/2;
        mn[in] = x, in/=2;
        while(in > 0){
            mn[in] = min(mn[2*in], mn[2*in+1]);
            in/=2;
        }
    }
    pair<int, int> smn(int l, int r, int nd, int a, int b){
        if(b < l || a > r) return {inf, inf};
        if(a >= l && b <= r) return mn[nd];
        int c = (a+b)/2;
        return min(smn(l, r, 2*nd, a, c), smn(l, r, 2*nd+1, c+1, b));
    }
    pair<int, int> smn(int l, int r){
        return smn(l, r, 1, 0, k/2-1);
    }
}seg(10);

struct reachability_tree{
    int n;
    vector<int> p;
    vector<int> adj;
    vector<bool> ok;
    vector<vector<int>> v;
    vector<int> w;
    reachability_tree(int n){
        reachability_tree::n = n;
        v.resize(n);
        p.resize(n);
        ok.resize(n, 0);
        adj.resize(n, 0);
        w.resize(n);
        for(int i = 0; i < n; i++) p[i] = i;
    }
    int get(int x){
        if(p[x] != x) return p[x] = get(p[x]);
        return p[x];
    }
    void unite(int a, int b, int vl){
        int aa = a, bb = b;
        a = get(a), b = get(b);
        adj[aa]++;
        adj[bb]++;
        p.pb(n);
        ok.pb(0);
        v.pb({});
        p[a] = n;
        p[b] = n;
        v[n].pb(a);
        if(a != b) v[n].pb(b);
        ok[n] = ok[a]|ok[b];
        if(max(adj[aa], adj[bb]) >= 3 || a == b) ok[n] = 1;
        n++;
        w.pb(vl);
    }
}rt(0);


int n, m;
vector<int> depth, ett, pos, anc;

void dfs(int nd, int d, int ls){
    depth[nd] = d;
    ett.pb(nd);
    if(rt.ok[nd]) ls = nd;
    anc[nd] = ls;
    for(int x: rt.v[nd]){
        dfs(x, d+1, ls);
        ett.pb(nd);
    }
}

void init(int n, int m, vector<int> u, vector<int>v, vector<int>w){
    ::n = n, ::m = m;
    rt = reachability_tree(n);
    vector<tuple<int, int, int>> edges;
    for(int i = 0; i < m; i++){
        edges.pb({w[i], u[i], v[i]});
    }
    sort(edges.begin(), edges.end());
    for(auto [wi, ui, vi]: edges) rt.unite(ui, vi, wi);
    depth.resize(rt.n);
    pos.resize(rt.n);
    anc.resize(rt.n);
    dfs(rt.get(0), 1, -1);
    seg = segtree(ett.size());
    for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
    for(int i = 0; i < rt.n; i++) pos[ett[i]] = i;
}

int getMinimumFuelCapacity(int x, int y){
    x = pos[x], y = pos[y];
    if(x > y) swap(x, y);
    int lca = seg.smn(x, y).sc;
    int ans = anc[lca];
    if(ans != -1) ans  = rt.w[ans];
    return ans;
}

Compilation message

swap.cpp: In constructor 'segtree::segtree(int)':
swap.cpp:17:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   17 |         while(k <= n) k*=2; k*=2;
      |         ^~~~~
swap.cpp:17:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   17 |         while(k <= n) k*=2; k*=2;
      |                             ^
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
      |                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 99 ms 24104 KB Output is correct
10 Correct 122 ms 27004 KB Output is correct
11 Correct 121 ms 26860 KB Output is correct
12 Correct 126 ms 27780 KB Output is correct
13 Correct 116 ms 30980 KB Output is correct
14 Correct 108 ms 24096 KB Output is correct
15 Correct 248 ms 28732 KB Output is correct
16 Correct 245 ms 28344 KB Output is correct
17 Correct 253 ms 29392 KB Output is correct
18 Correct 245 ms 32620 KB Output is correct
19 Correct 108 ms 7512 KB Output is correct
20 Correct 245 ms 28876 KB Output is correct
21 Correct 245 ms 28136 KB Output is correct
22 Correct 253 ms 29492 KB Output is correct
23 Correct 241 ms 32512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 216 ms 34220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Incorrect 1 ms 460 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 99 ms 24104 KB Output is correct
11 Correct 122 ms 27004 KB Output is correct
12 Correct 121 ms 26860 KB Output is correct
13 Correct 126 ms 27780 KB Output is correct
14 Correct 116 ms 30980 KB Output is correct
15 Incorrect 1 ms 460 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 99 ms 24104 KB Output is correct
10 Correct 122 ms 27004 KB Output is correct
11 Correct 121 ms 26860 KB Output is correct
12 Correct 126 ms 27780 KB Output is correct
13 Correct 116 ms 30980 KB Output is correct
14 Correct 108 ms 24096 KB Output is correct
15 Correct 248 ms 28732 KB Output is correct
16 Correct 245 ms 28344 KB Output is correct
17 Correct 253 ms 29392 KB Output is correct
18 Correct 245 ms 32620 KB Output is correct
19 Incorrect 216 ms 34220 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 99 ms 24104 KB Output is correct
11 Correct 122 ms 27004 KB Output is correct
12 Correct 121 ms 26860 KB Output is correct
13 Correct 126 ms 27780 KB Output is correct
14 Correct 116 ms 30980 KB Output is correct
15 Correct 108 ms 24096 KB Output is correct
16 Correct 248 ms 28732 KB Output is correct
17 Correct 245 ms 28344 KB Output is correct
18 Correct 253 ms 29392 KB Output is correct
19 Correct 245 ms 32620 KB Output is correct
20 Incorrect 216 ms 34220 KB Output isn't correct
21 Halted 0 ms 0 KB -