This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
#define all(v) v.begin(), v.end()
using namespace std;
struct UnionFind
{
vector<int> par;
void init(int n){par.resize(n+1); for(int i=1;i<=n;i++) par[i] = i;}
int fin(int i){return i==par[i]?i:(par[i] = fin(par[i]));}
void uni(int u, int v){par[fin(u)] = fin(v);}
bool isuni(int u, int v){return fin(u) == fin(v);}
};
int N, M;
int stop[100005];
struct Edge
{
int s, e;
int w;
bool operator<(const Edge &e)
{
return w < e.w;
}
};
Edge edge[100005];
vector<pair<int, int>> adj[505];
bool visited[505];
pair<int, int> par[505];
bool add[100005];
signed main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; i++) {
int a, b, w; cin >> a >> b >> w;
edge[i] = {a, b, w};
}
sort(edge + 1, edge + M + 1);
for (int i = 1; i <= M; i++) {
fill(visited + 1, visited + N + 1, false);
int a = edge[i].s, b = edge[i].e;
visited[a] = true;
queue<int> q;
q.push(a);
while (!q.empty()) {
int cur = q.front(); q.pop();
for (pair<int, int> p:adj[cur]) {
if (!visited[p.second]) {
par[p.second] = make_pair(p.first, cur);
visited[p.second] = true; q.push(p.second);
}
}
}
if (visited[b]) {
int mn = i, now = b;
while (1) {
mn = min(mn, par[now].first);
now = par[now].second;
if (now == a) break;
}
int s = edge[mn].s, e = edge[mn].e;
adj[s].erase(remove(all(adj[s]), make_pair(mn, e)), adj[s].end());
adj[e].erase(remove(all(adj[e]), make_pair(mn, s)), adj[e].end());
stop[mn] = i;
add[i] = true;
}
adj[a].push_back(make_pair(i, b));
adj[b].push_back(make_pair(i, a));
}
vector<pair<int, pair<bool, int>>> vc;
for (int i = 1; i <= M; i++) {
if (stop[i]) {
vc.push_back(make_pair((edge[i].w+edge[stop[i]].w)/2+1, make_pair(false, i)));
vc.push_back(make_pair((edge[i].w+edge[stop[i]].w)/2+1, make_pair(true, stop[i])));
}
}
for (int i = 1; i <= M; i++) {
if (!add[i]) vc.push_back(make_pair(0, make_pair(true, i)));
}
sort(vc.begin(), vc.end());
UnionFind uf;
int pv = 0;
int pv2 = 0;
int Q; cin >> Q;
pair<long long, long long> lin = make_pair(0LL, 0LL);
for (int i = 0; i < Q; i++) {
long long X; cin >> X;
while (pv < M && edge[pv+1].w <= X) {
pv++;
lin.second -= 2 * edge[pv].w;
lin.first += 2;
}
while (pv2 < (int)vc.size() && vc[pv2].first <= X) {
lin.first -= 1;
lin.second += edge[vc[pv2].second.second].w;
pv2++;
}
cout << X * lin.first + lin.second << '\n';
}
}
# | 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... |