#include<bits/stdc++.h>
using namespace std;
#define N 300100
#define int long long
vector<tuple<int,int,int>>adj4,could;
vector<int>spec,adj[N];
vector<pair<int,bool>>adj2[N];
int R[N],peop[N],cur,ans;
bitset<300100>good,vis;
priority_queue<int,vector<int>,greater<int>>upd[N];
vector<pair<int,int>> adj3;
struct unionfind{
int par[N];
void init(){
for(int i=0;i<N;i++)
par[i]=i;
}
int abp(int x){
return (par[x]-x?par[x]=abp(par[x]):x);
}
bool unite(int a,int b){
a=abp(a),b=abp(b);
par[a]=b;
return a-b;
}
} tuf;
void dfs(int n,int rt){
if(vis[n])return;
vis[n]=1,R[n]=rt;
peop[rt]+=(n>rt)*peop[n];
for(auto i:adj[n])
dfs(i,rt);
}
long long dfsA(int n,int p){
long long sum=peop[n],q;
for(auto[i,j]:adj2[n]){
if(i==p)continue;
q=dfsA(i,n);
sum+=q;
if(upd[i].empty())continue;
int K=upd[i].top();
upd[i].pop();
while(upd[i].size()&&upd[i].top()==K) {
upd[i].pop(),K=upd[i].top();
if(upd[i].size())upd[i].pop();
}
upd[i].push(K);
cur+=j*q*K;
if(upd[i].size()>upd[n].size())
swap(upd[i],upd[n]);
while(upd[i].size())
upd[n].push(upd[i].top()),upd[i].pop();
}
return sum;
}
signed main(){
tuf.init();
cin.tie(0)->sync_with_stdio(0);
int n,m,k;
cin>>n>>m>>k;
for(int i=m;i--;){
int a,b,c;
cin>>a>>b>>c;
adj4.push_back({c,a,b});
}
for(int i=0;i<k;i++){
int a,b;
cin>>a>>b;
adj3.push_back({a,b});
}
for(int i=1;i<=n;i++)
cin>>peop[i];
for(auto[i,j]:adj3)
tuf.unite(i,j);
sort(adj4.begin(),adj4.end());
for(auto[w,i,j]:adj4) if(tuf.unite(i,j))
good[w]=1,adj[i].push_back(j),
adj[j].push_back(i);
tuf.init();
for(auto[w,i,j]:adj4)
if(tuf.unite(i,j)&&!good[w])
could.push_back({i,j,w});
for(int i=1;i<=n;i++)
if(!vis[i])dfs(i,i),
spec.push_back(i);
for(auto&[i,j]:adj3)
i=R[i],j=R[j];
for(auto&[i,j,w]:could)
i=R[i],j=R[j];
for(int i=1;i<1<<k;i++){
for(auto i:spec)
adj2[i].clear(),
tuf.par[i]=i;
int ok=1;
for(int j=0;j<k;j++) if(i&1<<j)
ok&=tuf.unite(adj3[j].first,adj3[j].second),
adj2[adj3[j].first].push_back({adj3[j].second,1}),
adj2[adj3[j].second].push_back({adj3[j].first,1});
if(!ok) continue;
for(auto[i,j,w]:could) if(!tuf.unite(i,j))
upd[i].push(w),upd[j].push(w);
else adj2[i].push_back({j,0}),
adj2[j].push_back({i,0});
dfsA(1,0);
ans=max(ans,cur);
cur=0;
while(upd[1].size())
upd[1].pop();
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Incorrect |
7 ms |
31260 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Incorrect |
7 ms |
31260 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Incorrect |
7 ms |
31260 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |