제출 #1299987

#제출 시각아이디문제언어결과실행 시간메모리
1299987InvMOD통행료 (APIO13_toll)C++17
100 / 100
1791 ms5500 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

struct Edge{
    int u, v, w;
    Edge(): u(0), v(0), w(0) {}
    Edge(int u, int v, int w): u(u), v(v), w(w) {}
    bool operator < (const Edge& q) const{
        return w < q.w;
    }
};

struct DSU{
    vector<int> lab;

    DSU(int n = 0): lab(n + 1, -1) {}

    int asc(int x){
        return lab[x] < 0 ? x : lab[x] = asc(lab[x]);
    }

    bool join(int u, int v){
        u = asc(u), v = asc(v);
        if(u != v){
            if(lab[u] > lab[v]) swap(u, v);
            lab[u] += lab[v], lab[v] = u;
            return true;
        }
        return false;
    }
};


void Main()
{
    int N, M, K; cin >> N >> M >> K;

    vector<Edge> E(M + K + 1);

    FOR(i, 1, M) cin >> E[i].u >> E[i].v >> E[i].w;
    FOR(i, 1, K) cin >> E[i + M].u >> E[i + M].v;
    sort(1 + all(E));

    vector<ll> csz(N + 1);
    FOR(i, 1, N) cin >> csz[i];

    vector<bool> inMST(sz(E)); DSU dsu(N);
    FOR(i, 1, sz(E) - 1){
        if(dsu.join(E[i].u, E[i].v)){
            if(i > K){
                inMST[i] = true;
            }
        }
    }

    dsu = DSU(N);
    FOR(i, K + 1, sz(E) - 1) if(inMST[i]){
        dsu.join(E[i].u, E[i].v);
    }

    vi component(N + 1); vector<ll> sz_comp(1); int max_comp = 0;
    {
        map<int, int> id;
        FOR(i, 1, N){
            if(!id.count(dsu.asc(i))){
                id[dsu.asc(i)] = sz(id) + 1;
                sz_comp.eb(0), max_comp++;
            }
            component[i] = id[dsu.asc(i)];
            sz_comp[component[i]] += 1ll * csz[i];
        }
    }

    vector<Edge> valueE, mrG;
    dsu = DSU(max_comp);
    FOR(i, 1, sz(E) - 1) if(!inMST[i]){
        E[i].u = component[E[i].u];
        E[i].v = component[E[i].v];

        if(i <= K) mrG.emplace_back(E[i]);
        else{
            if(dsu.join(E[i].u, E[i].v)){
                valueE.emplace_back(E[i]);
            }
        }
    }

    ll ans = 0;
    FOR(mask, 1, (1 << K) - 1){
        vector<vi> tr(max_comp + 1);
        dsu = DSU(max_comp);

        bool chk = true;
        FOR(i, 0, K - 1) if(mask >> i & 1){
            if(dsu.join(mrG[i].u, mrG[i].v)){
                tr[mrG[i].u].eb(i + sz(valueE));
                tr[mrG[i].v].eb(i + sz(valueE));
            }
            else chk = false;
        }
        if(!chk) continue; // before we don't have but not mean when compress

        FOR(i, 0, sz(valueE) - 1){
            if(dsu.join(valueE[i].u, valueE[i].v)){
                tr[valueE[i].u].eb(i);
                tr[valueE[i].v].eb(i);
            }
        }

        vi h(max_comp + 1), par(max_comp + 1);
        vector<ll> sz = vector<ll>(all(sz_comp));

        auto dfs = [&](auto&& self, int x, int p) -> void{
            for(int i : tr[x]) if(i != p){
                int v = (i < sz(valueE) ? (valueE[i].u ^ valueE[i].v ^ x):
                                    (mrG[i - sz(valueE)].u ^ mrG[i - sz(valueE)].v ^ x));
                h[v] = h[x] + 1, par[v] = x;
                self(self, v, i);
                sz[x] += sz[v];
            }
        };
        dfs(dfs, component[1], -1);

        vi valueW(max_comp + 1, 1e9);
        FOR(i, 0, sz(valueE) - 1){
            int u = valueE[i].u, v = valueE[i].v;
            int curW = valueE[i].w;
            while(u != v){
                if(h[u] < h[v]) swap(u, v);
                ckmn(valueW[u], curW), u = par[u];
            }
        }

        ll tot = 0;
        FOR(i, 0, K - 1) if(mask >> i & 1){
            int u = mrG[i].u, v = mrG[i].v;
            if(h[u] > h[v]) swap(u, v);
            tot += 1ll * valueW[v] * sz[v];
        }
        ckmx(ans, tot);
    }
    cout << ans << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

/*
    core idea: we don't need to care about how we assign value to MrG's edge -> (2^k) MST only
    and why ? look at how we merge edge on DSU, it can give us the idea on how to fully solve this problem too
*/

컴파일 시 표준 에러 (stderr) 메시지

toll.cpp: In function 'int32_t main()':
toll.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...