//```// YF YUSUF
// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <queue>
#include <stack>
#include <cassert>
#include <climits>
#include <string>
#include <cstdlib>
#include <random>
#include <iomanip>
#include <ctime>
using namespace std;
#ifdef YF_CHECK
bool LOCAL = 1;
#else
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("O3")
bool LOCAL = 0;
#endif
using ll = long long;
using ld = long double;
using vll = vector <ll>;
using mll = map <ll,ll>;
using pll = pair <ll,ll>;
using vvl = vector <vll>;
using vpll = vector <pll>;
template<class T>T MIN(T&a,T b){a=min(a,b);return a;}
template<class T>T MAX(T&a,T b){a=max(a,b);return a;}
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sgr v+v+1,(tl+tr)/2+1,tr
#define sgl v+v,tl,(tl+tr)/2
#define pb push_back
#define ins insert
#define S second
#define F first
mt19937_64 MT(time(0));
ll BP(ll a,ll b,ll mod=1e9+7){
if(b==0)return 1;
ll q=BP(a,b/2,mod);
return ((q*q)%mod*(b%2?a:1ll))%mod;
}
ll f(ll x){return x*(x+1)/2;}
ll dup(ll a,ll b){return (a+b-1)/b;}
ll lcm(ll a,ll b){return a/__gcd(a,b)*b;}
ll invf(ll x){return (-1+sqrt(1+8*x))/2;}
ll lg(ll x){return (x ? 63 - __builtin_clzll(x) : -1);}
const ll mod=998244353;
const ll INF=1e18;
const ll inf=1e9+7;
const ll N =1e6+7;
ll a[N];
vpll K,g[N];
ll n,m,k;
struct DSU{
vll p,sz;
vector<pair<ll*, ll>>rb;
ll n;
DSU(ll n):n(n){
build(n);
}
DSU();
void build(ll n){
p.resize(n+1, 0);
sz.resize(n+1, 1);
for(int i=1;i<=n;i++){
p[i] = i;
sz[i] = 1;
}
}
ll get(ll x){
if(p[x]==x)return x;
else return get(p[x]);
}
bool check(ll a,ll b){
a = get(a);
b = get(b);
return (a!=b);
}
bool up(ll a,ll b){
a = get(a);
b = get(b);
if(a==b)return 0;
if(sz[a] < sz[b])swap(a,b);
rb.pb({ &p[b], p[b]});
rb.pb({&sz[a], sz[a]});
p[b] = a;
sz[a] += sz[b];
return 1;
}
void rollback(int last){
while(rb.size() > last){
*rb.back().F = rb.back().S;
rb.pop_back();
}
}
};
ll up[5][N];
void YF_MAIN(ll TEST){
cin>>n>>m>>k;
vvl E, A, B;
{
vvl e;
DSU graph(n);
for(int i=1;i<=m;i++){
ll v,u,w;
cin>>v>>u>>w;
e.pb({w,v,u});
}
sort(all(e));
for(auto V : e){
if(graph.up(V[1], V[2]))E.pb(V);
}
}
DSU dsu(n);
{
DSU graph(n);
for(int i=1;i<=k;i++){
ll v,u;
cin>>v>>u;
graph.up(v, u);
K.pb({v,u});
}
for(auto V : E){
if(graph.up(V[1], V[2])){
A.pb(V);
dsu.up(V[1], V[2]);
} else{
B.pb(V);
}
}
}
ll color[n+1]{}, suma[n+1]{}, b[n+1]{};
ll sz=0;
{
set<ll>st;
mll mp;
for(int i=1;i<=n;i++){
cin>>a[i];
color[i] = dsu.p[i];
st.insert(color[i]);
}
for(auto i : st)
mp[i] = ++sz;
for(int i=1;i<=n;i++)
color[i] = mp[color[i]];
for(int i=1;i<=n;i++){
suma[color[i]] += a[i];
}
}
for(auto &[v, u] : K){ v = color[v]; u = color[u]; }
for(auto &V : A){ V[1] = color[V[1]]; V[2] = color[V[2]]; }
for(auto &V : B){ V[1] = color[V[1]]; V[2] = color[V[2]]; }
DSU cycle(n), msu(n);
vpll g[n+1]{};
vll d(n+1, inf);
ll W[sz+1][sz+1]{};
ll ans = 0;
for(int mask = 1; mask < (1<<k); mask++){
cycle.rollback(0);
msu.rollback(0);
bool can = 1;
for(int i=0; i<k; i++)if((mask>>i)&1){
auto [v, u] = K[i];
W[v][u] = W[u][v] = inf;
if(!cycle.up(v, u)){
can = 0;
break;
}
else{
g[v].pb({1, u});
g[u].pb({1, v});
}
}
if(!can)continue;
vvl nd;
for(auto V : B){
ll v = V[1], u = V[2];
if(cycle.check(v, u)){
g[v].pb({0, u});
g[u].pb({0, v});
} else{
nd.pb(V);
}
}
queue<ll>q;
q.push(color[1]);
vvl e;
d[color[1]] = 0;
while(q.size()){
ll v = q.front();
q.pop();
for(auto [b, to] : g[v])if(d[to] > d[v] + 1){
d[to] = d[v] + 1;
e.pb({b, v, to});
q.push(to);
up[0][to] = v;
}
}
// for(int j=1;j<5;j++){
// for(int v=1; v<=sz; v++){
// up[j][v] = up[j-1][up[j-1][v]];
// }
// }
reverse(all(e));
// im so alone, nothink feels like home
// come fly with me,
for(auto V : nd){
ll w = V[0], v = V[1], u = V[2];
while(d[v] < d[u]){
ll p = up[0][u];
// cout<<u<<" "<<p<<" u\n";
W[u][p] = W[p][u] = min(W[p][u], V[0]);
u = p;
}
swap(v, u);
while(d[v] < d[u]){
ll p = up[0][u];
// cout<<u<<" "<<p<<" v\n";
W[u][p] = W[p][u] = min(W[p][u], V[0]);
u = p;
}
while(v != u){
ll p = up[0][u];
ll P = up[0][v];
W[u][p] = W[p][u] = min(W[p][u], V[0]);
W[v][P] = W[P][v] = min(W[P][v], V[0]);
v = P;
u = p;
}
}
for(int i=1;i<=sz;i++){
b[i] = suma[i];
}
ll now = 0;
for(auto V : e){
b[V[1]] += b[V[2]];
}
for(auto V : e){
now += W[V[1]][V[2]] * b[V[2]];
}
MAX(ans, now);
for(int i=1;i<=sz;i++){
g[i].clear();
b[i] = 0;
}
}
cout<<ans;
}
const bool TECT=0;
const bool FLSH=0;
const ll SN=1e0 + 7; ll SM[SN];
const ll FN=1e0 + 7; ll FACT[FN], inv[FN], FMOD=inf;
ll PNK(ll n,ll k){return FACT[n] *inv[n-k]%FMOD;}
ll CNK(ll n,ll k){return PNK(n,k)*inv[k ]%FMOD;}
void BEFORE(){
for(ll i=2;i<SN;i++){
if(SM[i])continue;
for(ll j=i;j<SN;j+=i)
MAX(SM[j],i);
}
FACT[0]=inv[0]=1;
for(int i=1;i<FN;i++){
FACT[i]=FACT[i-1]*i%FMOD;
inv[i]=BP(FACT[i],FMOD-2,FMOD);
}
}
signed main(){
// freopen(("input.txt"),"r",stdin);freopen(("output.txt"),"w",stdout);
if(FLSH){
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(0);
cout.tie(0);
cin.tie(0);
}
int TEST=1;
if(TECT)
cin>>TEST;
BEFORE();
for(int T=1;T<=TEST;T++){
// cout<<"Case "<<T<<": ";
YF_MAIN(T);
cout<<(T==TEST ? "" : "\n");
}
return 0;
}
// YF YUSUF ```
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |