Submission #947760

# Submission time Handle Problem Language Result Execution time Memory
947760 2024-03-17T01:52:47 Z parlimoos Toll (APIO13_toll) C++17
78 / 100
998 ms 12724 KB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'

int n , m , k;
vector<arr(3)> mst , cnds , uss , uns;
vector<int> a;
vector<arr(2)> adds , g[100000];
ll dsu[100000][2] , mem[100000];
int dsuinx = -1;
vector<array<ll , 3>> bck;
bool iscnd[100000];

void init(){
    for(int v = 0 ; v < n ; v++) dsu[v][0] = -1 , dsu[v][1] = a[v];
}
void svDsu(int v){
    bck.pb({v , dsu[v][0] , dsu[v][1]});
}
int findDsu(int v){
    if(dsu[v][0] == -1) return v;
    if(dsuinx != -1) svDsu(v);
    return (dsu[v][0] = findDsu(dsu[v][0]));
}
void uDsu(int a , int b){
    a = findDsu(a) , b = findDsu(b);
    if(a == b) return;
    if(dsuinx != -1) svDsu(a) , svDsu(b);
    dsu[a][1] += dsu[b][1] , dsu[b][0] = a;
}
void clDsu(){
    for(int i = (int)bck.size() - 1 ; i >= dsuinx ; i--) dsu[bck[i][0]][0] = bck[i][1] , dsu[bck[i][0]][1] = bck[i][2];
    bck.resize(dsuinx);
}
void getCnds(){
    for(auto &e : adds) uDsu(e[0] , e[1]);
    for(int e = 0 ; e < n - 1 ; e++){
        if(findDsu(mst[e][1]) == findDsu(mst[e][2])) cnds.pb(mst[e]) , iscnd[e] = 1;
        else uDsu(mst[e][1] , mst[e][2]);
    }
    init();
    for(int e = 0 ; e < n - 1 ; e++){
        if(!iscnd[e]) uDsu(mst[e][1] , mst[e][2]);
    }
    dsuinx = 0;
}
bool can(int msk){
    bool res = 1;
    dsuinx = (int)bck.size();
    for(int i = 0 ; i < k ; i++){
        if(!((msk >> i) & 1)) continue;
        if(findDsu(adds[i][0]) == findDsu(adds[i][1])){
            res = 0;
            break;
        }
        uDsu(adds[i][0] , adds[i][1]);
    }
    clDsu() , dsuinx = 0;
    return (res and (k <= 15));
}
int bs(int e , int msk){
    if(k > 15) return 0;
    dsuinx = (int)bck.size();
    for(int i = 0 ; i < k ; i++){
        if(i == e or !((msk >> i) & 1)) continue;
        uDsu(adds[i][0] , adds[i][1]);
    }
    int res = 0;
    for(auto &ee : uns){
        if(findDsu(adds[e][0]) == findDsu(adds[e][1])) break;
        res = ee[0] , uDsu(ee[1] , ee[2]);
    }
    clDsu() , dsuinx = 0;
    return res;
}
ll dfs1(int v , int p){
    ll res = 0;
    mem[v] = dsu[v][1];
    for(auto &e : g[v]){
        int u = e[0] - v;
        if(u == p) continue;
        res += dfs1(u , v) , mem[v] += mem[u];
        res += (1ll * e[1]) * mem[u];
    }
    return res;
}
ll getCst(int msk){
    ll res;
    for(int e = 0 ; e < k ; e++){
        if(!((msk >> e) & 1)) continue;
        uDsu(adds[e][0] , adds[e][1]);
    }
    for(auto &e : cnds){
        if(findDsu(e[1]) == findDsu(e[2])) uns.pb(e);
        else uDsu(e[1] , e[2]) , uss.pb(e);
    }
    clDsu();
    for(auto &e : uss) uDsu(e[1] , e[2]);
    for(int e = 0 ; e < k ; e++){
        if(((msk >> e) & 1)){
            int r1 = findDsu(adds[e][0]) , r2 = findDsu(adds[e][1]) , d = bs(e , msk);
            g[r1].pb({r1 + r2 , d}) , g[r2].pb({r1 + r2 , d});
        }
    }
    res = dfs1(findDsu(0) , -1) , uss.cl() , uns.cl();
    for(int e = 0 ; e < k ; e++) if(((msk >> e) & 1)) g[findDsu(adds[e][0])].pp() , g[findDsu(adds[e][1])].pp();
    dsuinx = 0 , clDsu();
    return res;
}
ll f(){
    ll res = 0;
    for(int msk = 1 ; msk < (1 << k) ; msk++){
        if(can(msk)) res = max(res , getCst(msk));
    }
    return res;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> k;
    vector<arr(3)> es;
    for(int i = 0 ; i < m ; i++){
        int v , u , c;
        cin >> v >> u >> c;
        v-- , u--;
        es.pb({c , v , u});
    }
    for(int i = 0 ; i < k ; i++){
        int v , u;
        cin >> v >> u;
        adds.pb({v - 1 , u - 1});
    }
    for(int i = 0 ; i < n ; i++){
        int d;
        cin >> d;
        a.pb(d);
    }
    init();
    sort(es.bg() , es.end());
    for(auto e : es){
        if(findDsu(e[1]) == findDsu(e[2])) continue;
        mst.pb(e) , uDsu(e[1] , e[2]);
    }
    init() , getCnds();
    cout << f();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
3 Correct 4 ms 4956 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
3 Correct 4 ms 4956 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 7 ms 5212 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
3 Correct 4 ms 4956 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 7 ms 5212 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
7 Correct 483 ms 12512 KB Output is correct
8 Correct 437 ms 11708 KB Output is correct
9 Correct 410 ms 12724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
3 Correct 4 ms 4956 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 7 ms 5212 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
7 Correct 483 ms 12512 KB Output is correct
8 Correct 437 ms 11708 KB Output is correct
9 Correct 410 ms 12724 KB Output is correct
10 Incorrect 998 ms 12472 KB Output isn't correct
11 Halted 0 ms 0 KB -