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 <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, q;
int parent[505], E[505], invE[505];
int visited[505];
deque<int> graph[505];
vector<pair<int, pii> > edge;
vector<pair<int, pii> > coef;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
edge.push_back({-(int)(1e9), {0, 0}});
for(int i = 1; i <= m; ++i) {
int a, b, w; cin >> a >> b >> w;
edge.push_back({w, {a, b}});
}
sort(edge.begin(), edge.end());
edge.push_back({(int)(1e9) + 1, {0, 0}});
auto addEdge = [&](int i) -> void {
int a = edge[i].second.first, b = edge[i].second.second;
graph[a].push_back(b);
graph[b].push_back(a);
};
auto removeEdge = [&](int i) -> void {
int a = edge[i].second.first, b = edge[i].second.second;
graph[a].pop_front();
graph[b].pop_front();
};
auto getParent = [&](auto&& self, int node) -> int {
if(parent[node] == node) return node;
return parent[node] = self(self, parent[node]);
};
auto unionParent = [&](int u, int v) -> void {
u = getParent(getParent, u);
v = getParent(getParent, v);
if(u < v) swap(u, v);
parent[v] = u;
};
auto dfs = [&](auto&& self, int node) -> void {
visited[node] = 1;
for(auto v : graph[node]) if(!visited[v]) self(self, v);
};
int p = 1;
E[1] = 1; addEdge(1);
for(int i = 2; i <= m; ++i) {
int a = edge[i].second.first, b = edge[i].second.second;
int p = i - 1;
for(int j = 1; j <= n; ++j) parent[j] = j;
while(p) {
unionParent(edge[p].second.first, edge[p].second.second);
if(getParent(getParent, a) == getParent(getParent, b)) break;
--p;
}
E[i] = p + 1;
}
p = m; invE[m] = m; addEdge(m);
for(int i = m - 1; i >= 1; --i) {
int a = edge[i].second.first, b = edge[i].second.second;
int p = i + 1;
for(int j = 1; j <= n; ++j) parent[j] = j;
while(p <= m) {
unionParent(edge[p].second.first, edge[p].second.second);
if(getParent(getParent, a) == getParent(getParent, b)) break;
++p;
}
invE[i] = p - 1;
}
for(int i = 1; i <= m; ++i) {
coef.push_back({edge[i].first, {2, -2 * edge[i].first}});
coef.push_back({(edge[i].first + edge[E[i] - 1].first)/2 + 1, {-1, edge[i].first}});
coef.push_back({(edge[i].first + edge[invE[i] + 1].first)/2 + 1, {-1, edge[i].first}});
}
sort(coef.begin(), coef.end());
cin >> q;
int i = 0; ll a = 0, b = 0;
while(q--) {
ll x; cin >> x;
while(i < (int)coef.size() && coef[i].first <= x) {
a += 1ll * coef[i].second.first;
b += 1ll * coef[i].second.second;
++i;
}
cout << a * x + b << '\n';
}
return 0;
}
Compilation message (stderr)
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:39:10: warning: variable 'removeEdge' set but not used [-Wunused-but-set-variable]
39 | auto removeEdge = [&](int i) -> void {
| ^~~~~~~~~~
reconstruction.cpp:57:10: warning: variable 'dfs' set but not used [-Wunused-but-set-variable]
57 | auto dfs = [&](auto&& self, int node) -> void {
| ^~~
reconstruction.cpp:62:9: warning: variable 'p' set but not used [-Wunused-but-set-variable]
62 | int p = 1;
| ^
# | 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... |