Submission #392061

# Submission time Handle Problem Language Result Execution time Memory
392061 2021-04-20T11:17:23 Z Jarif_Rahman Swapping Cities (APIO20_swap) C++17
6 / 100
2000 ms 36864 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 x;
        return get(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 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 102 ms 25752 KB Output is correct
10 Correct 125 ms 29052 KB Output is correct
11 Correct 127 ms 28840 KB Output is correct
12 Correct 129 ms 29932 KB Output is correct
13 Correct 117 ms 33096 KB Output is correct
14 Correct 115 ms 25988 KB Output is correct
15 Correct 251 ms 33088 KB Output is correct
16 Correct 251 ms 32752 KB Output is correct
17 Correct 259 ms 33796 KB Output is correct
18 Correct 251 ms 36836 KB Output is correct
19 Correct 102 ms 9556 KB Output is correct
20 Correct 251 ms 33100 KB Output is correct
21 Correct 252 ms 32472 KB Output is correct
22 Correct 253 ms 33900 KB Output is correct
23 Correct 254 ms 36864 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 Execution timed out 2072 ms 15008 KB Time limit exceeded
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 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Incorrect 2 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 2 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 102 ms 25752 KB Output is correct
11 Correct 125 ms 29052 KB Output is correct
12 Correct 127 ms 28840 KB Output is correct
13 Correct 129 ms 29932 KB Output is correct
14 Correct 117 ms 33096 KB Output is correct
15 Incorrect 2 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 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 102 ms 25752 KB Output is correct
10 Correct 125 ms 29052 KB Output is correct
11 Correct 127 ms 28840 KB Output is correct
12 Correct 129 ms 29932 KB Output is correct
13 Correct 117 ms 33096 KB Output is correct
14 Correct 115 ms 25988 KB Output is correct
15 Correct 251 ms 33088 KB Output is correct
16 Correct 251 ms 32752 KB Output is correct
17 Correct 259 ms 33796 KB Output is correct
18 Correct 251 ms 36836 KB Output is correct
19 Execution timed out 2072 ms 15008 KB Time limit exceeded
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 2 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 102 ms 25752 KB Output is correct
11 Correct 125 ms 29052 KB Output is correct
12 Correct 127 ms 28840 KB Output is correct
13 Correct 129 ms 29932 KB Output is correct
14 Correct 117 ms 33096 KB Output is correct
15 Correct 115 ms 25988 KB Output is correct
16 Correct 251 ms 33088 KB Output is correct
17 Correct 251 ms 32752 KB Output is correct
18 Correct 259 ms 33796 KB Output is correct
19 Correct 251 ms 36836 KB Output is correct
20 Execution timed out 2072 ms 15008 KB Time limit exceeded
21 Halted 0 ms 0 KB -