이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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], par[505][10], mn[505][10], dep[505];
vector<pii> tree[505];
vector<pair<int, pii> > edge;
vector<vector<int> > MST;
vector<int> X;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
edge.push_back({0, {0, 0}});
for(int i = 1; i <= m; ++i) {
int a, b, w; cin >> a >> b >> w;
edge.push_back({w, {a, b}});
}
auto getParent = [&](auto&& self, int node) -> int {
if(parent[node] == node) return node;
else 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, int p) -> void {
dep[node] = dep[p] + 1;
for(auto v : tree[node]) if(v.first != p) {
mn[v.first][0] = v.second;
par[v.first][0] = node;
self(self, v.first, node);
}
};
auto LCA = [&](int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v], j = 0;
while(diff) {
if(diff & 1) u = par[u][j];
diff >>= 1; ++j;
}
if(u == v) return u;
for(int i = 9; i >= 0; --i) {
if(par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
};
auto getmn = [&](int u, int p) -> int {
int diff = dep[u] - dep[p], j = 0;
int ret = inf;
while(diff) {
if(diff & 1) {
ret = min(ret, mn[u][j]);
u = par[u][j];
}
diff >>= 1; ++j;
}
return ret;
};
int x = 0;
for(int i = 1; i <= m + 1; ++i) {
if(x == inf) break;
vector<int> tmp;
priority_queue<pii> pq;
for(int j = 1; j <= n; ++j) parent[j] = j, tree[j].clear();
for(int j = 0; j <= n; ++j) for(int k = 0; k < 10; ++k) mn[j][k] = inf;
for(int j = 1; j <= m; ++j) pq.push({-abs(edge[j].first - x), j});
while(!pq.empty()) {
int j = pq.top().second;
int a = edge[j].second.first, b = edge[j].second.second;
pq.pop();
if(getParent(getParent, a) != getParent(getParent, b)) {
tmp.push_back(j);
unionParent(a, b);
tree[a].push_back({b, edge[j].first});
tree[b].push_back({a, edge[j].first});
}
}
dfs(dfs, 1, 0);
for(int k = 1; k < 10; ++k) for(int j = 1; j <= n; ++j) par[j][k] = par[par[j][k - 1]][k - 1];
for(int k = 1; k < 10; ++k) for(int j = 1; j <= n; ++j) mn[j][k] = min(mn[j][k - 1], mn[par[j][k - 1]][k - 1]);
X.push_back(x); MST.push_back(tmp);
int mx = inf;
for(int j = 1; j <= m; ++j) {
int a = edge[j].second.first, b = edge[j].second.second;
int p = LCA(a, b);
int mnv = min(getmn(a, p), getmn(b, p));
if(mnv < edge[j].first && (mnv + edge[j].first + 1)/2 > x)
mx = min(mx, mnv + edge[j].first + 1 >> 1);
}
x = mx;
}
cin >> q;
int i = 0;
while(q--) {
int x; cin >> x;
while(i + 1 < (int)X.size() && X[i + 1] <= x) ++i;
ll ans = 0;
for(auto j : MST[i]) ans += 1ll * abs(edge[j].first - x);
cout << ans << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:111:50: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
111 | mx = min(mx, mnv + edge[j].first + 1 >> 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... |