//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;
vector<int> a;
vector<arr(2)> adds , g[100000];
ll dsuc[100000] , bckc[1000000] , mem[100000];
int dsuinx = -1 , dsucnt = 0 , cnds[21][3] , uss[21][2] , uns[21][3];
int dsuv[100000] , bckv[1000000][2];
bool iscnd[100000] , bckms[100000];
void init(){
for(int v = 0 ; v < n ; v++) dsuv[v] = -1 , dsuc[v] = a[v];
}
void svDsu(int v){
if(!bckms[v]){
bckv[dsucnt][0] = v , bckv[dsucnt][1] = dsuv[v];
bckc[dsucnt++] = dsuc[v] , bckms[v] = 1;
}
}
int findDsu(int v){
if(dsuv[v] == -1) return v;
if(dsuinx != -1) svDsu(v);
return (dsuv[v] = findDsu(dsuv[v]));
}
void uDsu(int b , int a){
a = findDsu(a) , b = findDsu(b);
if(a == b) return;
if(dsuinx != -1) svDsu(a) , svDsu(b);
dsuc[a] += dsuc[b] , dsuv[b] = a;
}
void clDsu(){
for(int i = dsucnt - 1 ; i >= dsuinx ; i--){
dsuv[bckv[i][0]] = bckv[i][1] , dsuc[bckv[i][0]] = bckc[i];
bckms[bckv[i][0]] = 0;
}
dsucnt = dsuinx;
}
void getCnds(){
for(auto &e : adds) uDsu(e[0] , e[1]);
int cnt = 0;
for(int e = 0 ; e < n - 1 ; e++){
if(findDsu(mst[e][1]) == findDsu(mst[e][2])) cnds[cnt][0] = mst[e][0] , cnds[cnt][1] = mst[e][1] , cnds[cnt++][2] = mst[e][2] , iscnd[e] = 1;
else uDsu(mst[e][1] , mst[e][2]);
}
cnds[cnt][0] = -1;
init();
for(int e = 0 ; e < n - 1 ; e++){
if(!iscnd[e]) uDsu(mst[e][1] , mst[e][2]);
}
dsuinx = 0;
}
int bs(int e , int msk){
dsuinx = dsucnt;
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(int i = 0 ; uns[i][0] != -1 ; i++){
if(findDsu(adds[e][0]) == findDsu(adds[e][1])) break;
res = uns[i][0] , uDsu(uns[i][1] , uns[i][2]);
}
clDsu() , dsuinx = 0;
return res;
}
ll dfs1(int v , int p){
ll res = 0;
mem[v] = dsuc[v];
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;
int r1 = findDsu(adds[e][0]) , r2 = findDsu(adds[e][1]);
if(r1 == r2){
dsuinx = 0 , clDsu();
return 0;
}
uDsu(r1 , r2);
}
int c1 = 0 , c2 = 0;
for(int i = 0 ; cnds[i][0] != -1 ; i++){
int r1 = findDsu(cnds[i][1]) , r2 = findDsu(cnds[i][2]);
if(r1 == r2) uns[c1][0] = cnds[i][0] , uns[c1][1] = cnds[i][1] , uns[c1++][2] = cnds[i][2];
else uDsu(r1 , r2) , uss[c2][0] = cnds[i][1] , uss[c2++][1] = cnds[i][2];
}
uns[c1][0] = -1 , uss[c2][0] = -1;
dsuinx = 0 , clDsu();
for(int i = 0 ; uss[i][0] != -1 ; i++) uDsu(uss[i][0] , uss[i][1]);
for(int i = 0 ; i < dsucnt ; i++) bckms[bckv[i][0]] = 0;
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});
}
}
if(k > 1) return 0;
res = dfs1(findDsu(0) , -1);
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++){
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |