QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#720736 | #5548. Increase the Toll Fees | zijinjun# | RE | 2ms | 13708kb | C++17 | 3.3kb | 2024-11-07 13:52:18 | 2024-11-07 13:52:19 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <unordered_set>
#include <functional>
#include <stack>
#include <unordered_map>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
inline i64 read() {
i64 x = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0')
f = c == '-' ? -1 : 1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - 48, c = getchar();
return x * f;
}
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
struct Edge {
int u, v;
i64 w;
bool operator<(const Edge &other)const{
return w < other.w;
}
};
vector<pair<int, i64>> g[maxn];
int pre[maxn];
i64 maxw[20][maxn];
int fa[20][maxn];
int dep[maxn];
int get_pre(int x) {
while (x != pre[x])
x = pre[x] = pre[pre[x]];
return x;
}
vector<Edge> mst(int n, vector<Edge> &e) {
for (int i = 1; i <= n; i++)
pre[i] = i;
vector<Edge> tmp;
sort(e.begin(), e.end());
vector<Edge> mst_edges;
for (auto [u, v, w] : e) {
int fu = get_pre(u), fv = get_pre(v);
if (fu == fv) {
tmp.push_back({u, v, w});
continue;
}
pre[fu] = fv;
mst_edges.push_back({u, v, w});
}
e = tmp;
return mst_edges;
}
void dfs(int u, int pre) {
for (int i = 1; i <= dep[u]; i++) {
fa[i][u] = fa[i - 1][fa[i - 1][u]];
maxw[i][u] = max(maxw[i - 1][u], maxw[i - 1][fa[i - 1][u]]);
}
for (auto [v, w] : g[u]) {
if (v == pre) continue;
dep[v] = dep[u] + 1;
fa[0][v] = u;
maxw[0][v] = w;
dfs(v, u);
}
}
i64 query(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
i64 ans = 0;
for (int i = 19; i >= 0; i--) {
if (dep[x] - (1 << i) >= dep[y]) {
ans = max(ans, maxw[i][x]);
x = fa[i][x];
}
}
if (x == y)
return ans;
for (int i = 19; i >= 0; i--) {
if (fa[i][x] == fa[i][y]) continue;
ans = max(ans, maxw[i][x]);
ans = max(ans, maxw[i][y]);
x = fa[i][x];
y = fa[i][y];
}
ans = max(ans, maxw[0][x]);
ans = max(ans, maxw[0][y]);
return ans;
}
int main() {
int n = read(), m = read();
vector<Edge> e;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
i64 w = read();
e.push_back({u, v, w});
}
vector<Edge> mst1 = mst(n, e);
vector<Edge> mst2 = mst(n, e);
if (mst2.size() != n - 1) {
cout << -1 << endl;
return 0;
}
for (auto [u, v, w] : mst2) {
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(1, 0);
i64 ans = 0;
for (auto [u, v, w] : mst1) {
// cout << u << ' ' << v << ' ' << query(u, v) << ' ' << w << endl;
ans += query(u, v) + 1 - w;
}
cout << ans << endl;
return 0;
}
/*
4 6
1 2 2
1 3 5
1 4 5
2 3 3
2 4 5
3 4 4
3 4
1 2 3
2 3 4
1 3 5
1 3 10
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12800kb
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: 6364kb
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: 2ms
memory: 13708kb
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: 7444kb
input:
2 1 1 2 1
output:
-1
result:
ok single line: '-1'
Test #5:
score: -100
Runtime Error
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 ...