QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#383249 | #5548. Increase the Toll Fees | FOY# | WA | 156ms | 23188kb | C++23 | 2.2kb | 2024-04-09 07:27:09 | 2024-04-09 07:27:09 |
Judging History
answer
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <algorithm>
#include <cassert>
using namespace std;
using ll = long long;
using a3 = array<ll, 3>;
using pii = pair<ll, ll>;
const ll mn = 2e5+5;
vector<a3> edge;
struct DSU {
vector<ll> p;
DSU(ll n): p(n, -1) {}
ll find(ll a) {
return p[a] < 0 ? a : (p[a] = find(p[a]));
}
bool same(ll a, ll b) {
return find(a) == find(b);
}
void merge(ll a, ll b) {
a = find(a);
b = find(b);
if (p[a] > p[b]) swap(a,b);
p[a] += p[b];
p[b] = a;
}
};
vector<pair<ll, ll>> child[mn];
ll best[mn][20], par[mn][20], dep[mn];
void dfs(ll i, ll p=0) {
for (auto [j, cost] : child[i]) {
if (j == p) continue;
dep[j] = dep[i]+1;
best[j][0] = cost;
par[j][0] = i;
dfs(j, i);
}
}
ll bestOnPath(ll i, ll j) {
ll w = 0;
if (dep[i] > dep[j]) swap(i,j);
while (dep[i] < dep[j]) {
ll dif = dep[i]-dep[j];
if (j == 0) exit(1);
w = max(w, best[j][__builtin_ctz(dif)]);
j = par[j][__builtin_ctz(dif)];
}
ll x = 19;
while (par[i][0] != par[j][0]) {
w = max(w, best[i][x]);
i = par[i][x];
w = max(w, best[j][x]);
j = par[j][x];
x--;
}
if (i != j) {
x = 0;
w = max(w, best[i][x]);
i = par[i][x];
w = max(w, best[j][x]);
j = par[j][x];
}
assert(i == j);
return w;
}
int main() {
ll n, m; cin >> n >> m;
for (ll i = 0; i < m; i++) {
ll u, v, w; cin >> u >> v >> w;
edge.push_back({w, u, v});
}
DSU mst(n+1), mst2(n+1);
sort(edge.begin(), edge.end());
ll cnt = 0;
map<pii, ll> vals;
for (auto [w, i, j] : edge) {
if (!mst.same(i, j)) {
mst.merge(i, j);
vals[{i, j}] = w;
}
else if (!mst2.same(i, j)) {
mst2.merge(i, j);
child[i].push_back({ j, w });
child[j].push_back({ i, w });
cnt++;
}
}
if (cnt != n-1) {
assert(cnt < n-1);
cout << -1 << endl;
}
else {
dfs(1, 0);
par[1][0] = 1;
for (ll i = 0; i < 19; i++) {
for (ll j = 1; j <= n; j++) {
best[j][i+1] = max(best[j][i], best[par[j][i]][i]);
par[j][i+1] = par[par[j][i]][i];
}
}
ll out = 0;
for (auto [ed, w] : vals) {
ll cost = bestOnPath(ed.first, ed.second) - w+1;
out += cost;
}
cout << out << endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5660kb
input:
4 6 1 2 2 1 3 5 1 4 5 2 3 3 2 4 5 3 4 4
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
3 4 1 2 3 2 3 4 1 3 5 1 3 10
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 1ms
memory: 7712kb
input:
5 10 1 2 14 1 3 14 1 4 9 1 5 15 2 3 8 2 3 10 2 4 13 3 4 8 4 5 10 4 5 15
output:
21
result:
ok single line: '21'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
2 1 1 2 1
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 68ms
memory: 23188kb
input:
29171 100000 7223 21138 270743473 5598 27967 847631606 12666 26050 847631606 75 15747 270743473 8522 12955 847631606 6069 23750 270743473 18708 22605 847631606 16814 22169 847631606 11550 27119 847631606 562 15959 847631606 9400 11110 270743473 15325 23805 270743473 19169 24404 270743473 6649 12062 ...
output:
16827826868780
result:
ok single line: '16827826868780'
Test #6:
score: 0
Accepted
time: 156ms
memory: 15388kb
input:
47977 200000 10970 47321 440845807 1166 29708 767952745 319 37520 546280762 17581 29425 558321466 22079 26884 344816304 7479 44260 791002634 14685 44163 837529020 1537 10359 330017953 8634 27064 969738917 32950 37192 728271930 34751 42782 63025978 32540 34226 86057211 36786 46050 826927874 30444 436...
output:
-1
result:
ok single line: '-1'
Test #7:
score: 0
Accepted
time: 55ms
memory: 23052kb
input:
28825 57648 9446 22014 286256842 14902 20222 14175 3246 20218 80493268 1783 13768 931622563 11107 24862 918832025 25070 27312 98899079 8535 20222 16037 9184 17491 294248461 8799 17834 456827944 1152 11687 960740527 17849 23045 9706 5774 21436 444202963 5417 23045 3092 20222 20370 11232 16585 20222 1...
output:
28822649262260
result:
ok single line: '28822649262260'
Test #8:
score: -100
Wrong Answer
time: 138ms
memory: 19804kb
input:
22253 200000 46 10310 674985053 2403 19582 788128238 5203 7897 985236456 2709 9034 824557880 14337 20524 230824854 19901 22177 99959362 5067 8568 570267383 13707 21474 610729058 7494 7860 109319713 14473 16182 794578653 21 17055 852110939 19320 21640 844993191 2443 21673 170358534 5941 9705 16465049...
output:
6523198162596
result:
wrong answer 1st lines differ - expected: '3270266304988', found: '6523198162596'