Submission #882622

# Submission time Handle Problem Language Result Execution time Memory
882622 2023-12-03T12:21:08 Z MisterReaper Toll (APIO13_toll) C++17
0 / 100
1 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int MAXN = 1E5 + 5;

int par[MAXN];
struct DSU {
    int n;
    DSU(int n) : n(n) {
        iota(par, par + n, 0);
    }

    int get(int a) {
        return par[a] = (par[a] == a ? a : get(par[a]));
    }

    bool same(int a, int b) {
        return get(a) == get(b);
    }

    bool unite(int a, int b) {
        if(same(a, b)) {
            return false;
        }

        a = get(a), b = get(b);
        par[b] = a;
        return true;
    }
};

vector <vector <int>> adj;

#define ONLINE_JUDGE
void solve() {
    int n, m, k;
    cin >> n >> m >> k;

    using T = tuple <int, int, int>;
    vector <T> edg(m);
    for(auto &[u, v, c] : edg) {
        cin >> u >> v >> c;
    }

    sort(edg.begin(), edg.end(), [&](const T &a, const T &b) -> bool {
        return get <2> (a) < get <2> (b);
    });

    vector <pair <int, int>> greedy(k);
    for(auto &[u, v] : greedy) {
        cin >> u >> v;
    }

    vector <int> p(n +1);
    for(int i = 1; i <= n; i++) {
        cin >> p[i];
    }

    i64 ans = 0;
    for(int mask = 1; mask < (1 << k); mask++) {
        DSU dsu(n +1);
        adj.clear();
        adj.assign(n +1, vector <int> ());
        bool ok = true;
        for(int i = 0; i < k; i++) {
            if(mask & (1 << i)) {
                auto [u, v] = greedy[i];
                if(dsu.unite(u, v)) {
                    adj[u].emplace_back(v);
                    adj[v].emplace_back(u);
                } else {
                    ok = false;
                    break;
                }  
            }
        }   

        if(!ok) {
            continue;
        }

        int mn = int(1E9);
        for(auto [u, v, c] : edg) {
            if(dsu.unite(u, v)) {
                adj[u].emplace_back(v);
                adj[v].emplace_back(u);
            } else {
                mn = min(mn, c);
            }
        }

        vector <i64> sub(n +1);
        function <i64(int, int)> dfs = [&](int node, int par) -> i64 {
            i64 res = p[node];
            for(int &child : adj[node]) {
                if(child != par) {
                    res += dfs(child, node);
                }
            }

            return sub[node] = res;
        };

        dfs(1, 0);

        vector <i64> anss;
        i64 calc = 0;
        for(int i = 0; i < k; i++) { 
            if(mask & (1 << i)) {
                auto [u, v] = greedy[i];
                anss.emplace_back(min(sub[u], sub[v]));
            }
        }

        ans = max(ans, mn * accumulate(anss.begin(), anss.end(), 0LL));
    }

    cout << ans;
    
    return;
}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t = 1; //cin >> t;
    for(int i = 1; i <= t; i++) {
        solve();
    }

    return 0;
}

Compilation message

toll.cpp: In function 'void solve()':
toll.cpp:108:13: warning: unused variable 'calc' [-Wunused-variable]
  108 |         i64 calc = 0;
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -