Submission #247051

# Submission time Handle Problem Language Result Execution time Memory
247051 2020-07-10T20:18:31 Z keko37 Toll (APIO13_toll) C++14
78 / 100
2500 ms 28660 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long llint;
typedef pair <int, int> pi;

const int MAXN = 300005;

llint n, m, k, sol, cnt;
int a[MAXN], b[MAXN], c[MAXN], na[25], nb[25];
int par[MAXN], red[MAXN], dub[MAXN], p[MAXN], ok[MAXN], comp[MAXN];
llint siz[MAXN], cost[MAXN], mn[MAXN], val[MAXN];
vector <int> e, rip, g[MAXN];
vector <pi> v[MAXN];

void dsu () {
    for (int i = 1; i <= n; i++) {
        par[i] = i;
    }
}

int nadi (int x) {
    if (x == par[x]) return x;
    return par[x] = nadi(par[x]);
}

inline bool spoji (int x, int y) {
    x = nadi(x); y = nadi(y);
    par[x] = y;
    return x != y;
}

bool cmp (int e1, int e2) {
    return c[e1] < c[e2];
}

void oboji (int x) {
    if (comp[x] != 0) return;
    comp[x] = cnt;
    val[cnt] += cost[x];
    for (auto sus : g[x]) oboji(sus);
}

void edge_init () {
    sort(red, red + m, cmp);
    dsu();
    for (int i = 0; i < m; i++) {
        if (spoji(a[red[i]], b[red[i]])) e.push_back(red[i]);
    }
    dsu();
    for (int i = 0; i < k; i++) {
        spoji(na[i], nb[i]);
        ok[na[i]] = ok[nb[i]] = 1;
    }
    for (auto i : e) {
        if (spoji(a[i], b[i]) && ok[a[i]] == 0 && ok[b[i]] == 0) {
            g[a[i]].push_back(b[i]);
            g[b[i]].push_back(a[i]);
        } else {
            rip.push_back(i);
        }
    }
    e = rip;
    for (int i = 1; i <= n; i++) {
        if (comp[i] == 0) {
            cnt++;
            oboji(i);
        }
    }
    for (auto i : e) {
        a[i] = comp[a[i]];
        b[i] = comp[b[i]];
    }
    for (int i = 0; i < k; i++) {
        na[i] = comp[na[i]];
        nb[i] = comp[nb[i]];
    }
}

void dfs (int x, int rod) {
    siz[x] = val[x];
    dub[x] = 1 + dub[rod];
    p[x] = rod;
    for (auto par : v[x]) {
        int sus = par.first;
        if (sus == rod) {
            if (par.second == 1) ok[x] = 1;
            continue;
        }
        dfs(sus, x);
        siz[x] += siz[sus];
    }
}

void digni (int &x, llint w) {
    mn[x] = min(mn[x], w);
    x = p[x];
}

void lca (int x, int y, llint w) {
    if (dub[x] < dub[y]) swap(x, y);
    while (dub[x] != dub[y]) digni(x, w);
    while (x != y) digni(x, w), digni(y, w);
}

void solve (int mask) {
    for (int i = 1; i <= cnt; i++) {
        par[i] = i;
        v[i].clear();
        mn[i] = 1e9;
        ok[i] = 0;
    }
    for (int i = 0; i < k; i++) {
        if ((mask & (1 << i)) && spoji(na[i], nb[i])) {
            v[na[i]].push_back({nb[i], 1});
            v[nb[i]].push_back({na[i], 1});
        }
    }
    rip.clear();
    for (auto i : e) {
        if (spoji(a[i], b[i])) {
            v[a[i]].push_back({b[i], 0});
            v[b[i]].push_back({a[i], 0});
        } else {
            rip.push_back(i);
        }
    }
    dfs(1, 0);
    for (auto i : rip) {
        lca(a[i], b[i], c[i]);
    }
    llint res = 0;
    for (int i = 1; i <= cnt; i++) {
        if (ok[i]) res += siz[i] * mn[i];
    }
    sol = max(sol, res);
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        red[i] = i;
    }
    for (int i = 0; i < k; i++) {
        cin >> na[i] >> nb[i];
    }
    for (int i = 1; i <= n; i++) cin >> cost[i];
    edge_init();
    for (int mask = 0; mask < (1 << k); mask++) solve(mask);
    cout << sol;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14592 KB Output is correct
5 Correct 17 ms 14592 KB Output is correct
6 Correct 17 ms 14592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14592 KB Output is correct
5 Correct 17 ms 14592 KB Output is correct
6 Correct 17 ms 14592 KB Output is correct
7 Correct 403 ms 24504 KB Output is correct
8 Correct 423 ms 28452 KB Output is correct
9 Correct 428 ms 28404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14592 KB Output is correct
5 Correct 17 ms 14592 KB Output is correct
6 Correct 17 ms 14592 KB Output is correct
7 Correct 403 ms 24504 KB Output is correct
8 Correct 423 ms 28452 KB Output is correct
9 Correct 428 ms 28404 KB Output is correct
10 Execution timed out 2573 ms 28660 KB Time limit exceeded
11 Halted 0 ms 0 KB -