/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, m, q, dep[N], L[N], R[N];
ll pref[N];
pii g[501][501], par[N];
vector<array<int, 3>> E;
bitset<501> vis;
int ptr[501];
void dfs(int v, int p){
vis[v] = 1;
dep[v] = dep[p] + 1;
for(int i = 0; i < ptr[v]; ++i){
auto [u, idx] = g[v][i];
if(!vis[u]){
par[u] = {v, idx};
dfs(u, v);
}
}
}
int get(int u, int v){
int min_w = -1;
while(u != v){
if(par[u].ff == u && par[v].ff == v) return -1;
if(dep[u] > dep[v]){
min_w = min_w == -1 ? par[u].ss : (E[par[u].ss][0] < E[min_w][0] ? par[u].ss : min_w);
u = par[u].ff;
}else{
min_w = min_w == -1 ? par[v].ss : (E[par[v].ss][0] < E[min_w][0] ? par[v].ss : min_w);
v = par[v].ff;
}
}
return min_w;
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= m; ++i){
int u, v, w; cin >> u >> v >> w;
E.pb({w, u, v});
}
sort(all(E));
cerr << "edge: \n";
for(auto [w, u, v]: E){
cerr << w << ' ' << u << ' ' << v << '\n';
}
cerr << '\n';
vector<int> C; // current edges
for(int j = 0; j < m; ++j){
auto [w, u, v] = E[j];
// for(int x: C) cout << x << endl;
// cout << endl;
for(int i = 1; i <= n; ++i){
ptr[i] = 0;
}
for(auto idx: C){
auto [W, U, V] = E[idx];
g[U][ptr[U]++] = {V, idx};
g[V][ptr[V]++] = {U, idx};
}
vis = 0;
for(int i = 1; i <= n; ++i){
if(!vis[i]){
dep[i] = 0;
par[i] = {i, 0};
dfs(i, i);
}
}
int minimal = get(u, v);
if(minimal == -1){
L[j] = 0;
C.pb(j);
}else{
for(int i = 0; i < C.size(); ++i){
if(C[i] == minimal){
C.erase(C.begin() + i);
break;
}
}
C.pb(j);
R[minimal] = (E[j][0] + E[minimal][0]) / 2;
L[j] = (E[j][0] + E[minimal][0] + 2) / 2;
}
}
for(int idx: C) R[idx] = MOD;
// for(int j = 0; j < m; ++j) cout << L[j] << ' ' << R[j] << '\n';
cin >> q;
vector<array<int, 3>> A, B;
for(int j = 0; j < m; ++j) A.pb({L[j], R[j], E[j][0]});
for(int j = 0; j < m; ++j) B.pb({R[j], L[j], E[j][0]});
sort(all(A));
sort(all(B));
vector<int> W;
int cnt = 0, cnt2 = 0;
for(int i = 1; i <= q; ++i){
ll x; cin >> x;
while(cnt2 < m && B[cnt2][0] < x){
for(int j = 0; j < W.size(); ++j){
if(W[j] == B[cnt2][2]){
W.erase(W.begin() + j);
break;
}
}
++cnt2;
}
while(cnt < m && A[cnt][0] <= x){
bool add = false;
for(int j = 0; j < W.size(); ++j){
if(W[j] >= A[cnt][2]){
add = true;
W.insert(W.begin() + j, A[cnt][2]);
break;
}
}
if(!add) W.pb(A[cnt][2]);
++cnt;
}
pref[0] = 0;
for(int i = 1; i <= n; ++i){
pref[i] = pref[i - 1] + W[i - 1];
}
ll sum = 0;
int pos = upper_bound(all(W), (int)x) - W.begin();
cout << x * pos - pref[pos] + (pref[n] - pref[pos] - x * (n-pos-1)) << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |