Submission #554546

# Submission time Handle Problem Language Result Execution time Memory
554546 2022-04-28T16:30:46 Z ddy888 Cities (BOI16_cities) C++17
0 / 100
88 ms 19144 KB
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long 
#define pb push_back
#define fi first
#define si second
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef tuple<int,int,int> ti;  
template<typename T> bool chmin(T &a, T b) {return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b) {return (b > a) ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 100010;

int n, m, k;
vector<pi> g[MAXN];
int big[10], depth[MAXN], dist[MAXN], twok[MAXN][20];
struct edge {int u, v, c;} el[MAXN * 2];
vector<edge> crit;
set<int> me;
int ans;

struct DSU {    
    int par[10010];
    void init(int x) {for (int i = 0; i <= x; ++i) par[i] = i;}
    int root(int x) { return (par[x]==x)? x:par[x]=root(par[x]); }
    bool same(int a, int b) { return root(a) == root(b); }
    void merge(int a, int b) { // b absorbs a
        if (same(a, b)) return;
        par[root(a)] = root(b);
    }
};

struct TWOK {
    void init() {memset(twok, -1, sizeof twok);}
    void dfs(int x, int p, int d, int wd) {
        depth[x] = d;
        dist[x] = wd;
        twok[x][0] = p;
        for (int i = 1; i <= 17; i++) {
            if (twok[x][i - 1] == -1) break;
            twok[x][i] = twok[twok[x][i - 1]][i - 1];
        }
        for (auto it: g[x]) {
            if (it.fi != p) dfs(it.fi,x,d+1,wd+it.si);
        }
    }
    int kth_parent(int x, int w) {
        for (int i = 0; i <= 17; i++) {
            if (x == -1) return -1;
            if (w & (1 << i)) x = twok[x][i];
        }
        return x;
    }
    int lca(int x, int y) {
        if (depth[x] > depth[y]) {
            int dd = depth[x] - depth[y];
            x = kth_parent(x, dd);
        } else {
            int dd = depth[y] - depth[x];
            y = kth_parent(y, dd);
        }
        if (x == y) return x;
        for (int i = 17; i >= 0; i--) {
            if (twok[x][i] != twok[y][i]) {
                x = twok[x][i];
                y = twok[y][i];
            }
        }
        return twok[x][0];
    }   
    int get(int x, int y) {
        return dist[x] + dist[y] - 2 * dist[lca(x, y)];
    }
};

signed main() {
    fast;
    cin >> n >> k >> m;
    for (int i = 1; i <= k; ++i) {
        cin >> big[i];
        me.insert(big[i]);
    }
    for (int i = 1; i <= m; ++i) 
        cin >> el[i].u >> el[i].v >> el[i].c;
    sort(el + 1, el + 1 + m, [](edge a, edge b) {
        return a.c < b.c;
    });
    DSU dsu; TWOK tk;
    dsu.init(n + 1); tk.init();
    for (auto i: el) {
        if (dsu.same(i.u, i.v))    
            continue;
        dsu.merge(i.u, i.v);
        g[i.u].pb({i.v, i.c});
        g[i.v].pb({i.u, i.c});
    }
    tk.dfs(1, -1, 0, 0);
    for (int i = 1; i <= k; ++i) {
        for (int j = i + 1; j <= k; ++j) 
            me.insert(tk.lca(big[i], big[j]));
    }
    for (auto i: me) {
        for (auto j: me) {
            if (i == j) continue;
            crit.pb({i, j, tk.get(i, j)});
        }
    }
    sort(crit.begin(), crit.end(), [](edge a, edge b) {
        return a.c < b.c;
    });
    dsu.init(n + 1);
    for (auto i: crit) {    
        if (dsu.same(i.u, i.v))
            continue;
        dsu.merge(i.u, i.v);
        ans += i.c;
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18388 KB Output is correct
2 Correct 10 ms 18376 KB Output is correct
3 Incorrect 8 ms 18388 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 82 ms 19144 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 18516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 83 ms 19028 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 88 ms 19092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -