QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#660602 | #5548. Increase the Toll Fees | temmie# | RE | 4ms | 19624kb | C++17 | 3.9kb | 2024-10-20 12:16:52 | 2024-10-20 12:16:53 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define fastio ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
#ifdef LOCAL
#define cout cout << "\033[0;32m"
#define cerr cerr << "\033[0;32m"
#define endl "\n" << "\033[0m"
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt")
#define endl "\n"
#endif
#define pii pair<int,int>
#define all(x) (x).begin(), (x).end()
struct dsu_struct{
int n;
vector<int> p, sz;
void init(int _n) {
n = _n + 10;
p.resize(n);
sz.resize(n);
for (int i = 0; i <= _n; ++i) {
p[i] = i;
sz[i] = 1;
}
}
int qry(int i) {
if (p[p[i]] == p[i]) return p[i];
return p[i] = qry(p[i]);
}
bool merg(int u, int v) {
u = qry(u);
v = qry(v);
if (u == v) return false;
if (sz[u] < sz[v]) swap(u, v);
// let sz[v] < sz[u]
sz[u] += sz[v];
p[v] = u;
return true;
}
};
const int MAX_N = 5e5+10;
const int INF = 2e18;
const int mxlg = 18;
vector<pair<int, pii>> EV;
vector<pii> E[MAX_N]; // {to, w}
bool in_tree[MAX_N]; // in the first unique tree
int dep[MAX_N], jmp[MAX_N][mxlg], jval[MAX_N][mxlg];
void dfs(int v, int p) {
for (auto &i:E[v]) {
if (i.first == p) continue;
jmp[i.first][0] = v;
jval[i.first][0] = i.second;
dep[i.first] = dep[v] + 1;
dfs(i.first, v);
}
}
int lca_qry(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
// let dep[v] > dep[u]
int diff = dep[v] - dep[u], ret = 0;
for (int j = mxlg - 1; j >= 0; --j) {
if (diff & (1 << j)) {
ret = max(ret, jval[v][j]);
v = jmp[v][j];
}
}
if (u == v) return ret;
for (int j = mxlg - 1; j >= 0; --j) {
if (jmp[v][j] != jmp[u][j]) {
v = jmp[v][j];
u = jmp[u][j];
ret = max(ret, max(jval[v][j], jval[u][j]));
}
}
return max(ret, max(jval[v][0], jval[u][0]));
}
void solve1(){
int n, m; cin >> n >> m;
EV.resize(m);
for (int i = 0; i < m; ++i) {
int u, v, w; cin >> u >> v >> w;
EV[i] = {w, {u, v}};
}
sort(all(EV));
dsu_struct dsu;
dsu.init(n);
for (int i = 0; i < m; ++i) {
in_tree[i] = dsu.merg(EV[i].second.first, EV[i].second.second);
}
dsu.init(n);
int comp = n;
for (int i = 0; i < m; ++i) {
if (!in_tree[i]) {
comp -= dsu.merg(EV[i].second.first, EV[i].second.second);
}
}
if (comp != 1) {
cout << "-1" << endl;
return;
}
dsu.init(n);
for (int i = 0; i < m; ++i) {
if (!in_tree[i]) {
int u = EV[i].second.first;
int v = EV[i].second.second;
if (dsu.merg(u, v)) {
int w = EV[i].first;
E[u].push_back({v, w});
E[v].push_back({u, w});
// cout << u << ' ' << v << ", w = " << w << endl;
}
}
}
jmp[1][0] = 1;
dfs(1, -1);
for (int j = 1; j < mxlg; ++j) {
for (int i = 1; i <= n; ++i) {
jval[i][j] = max(jval[i][j-1], jval[ (jmp[i][j-1]) ][j-1]);
jmp[i][j] = jmp[ (jval[i][j-1]) ][j-1];
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
if (in_tree[i]) {
int u = EV[i].second.first;
int v = EV[i].second.second;
int w = EV[i].first;
ans += lca_qry(u, v) + 1 - w;
}
}
// cout << jmp[4][0] << ' ' << jmp[2][0] << endl;
// cout << jval[4][0] << ' ' << jval[2][0] << endl;
// cout << jval[4][1] << endl;
cout << ans << endl;
}
signed main(){
// freopen("pM.in", "r", stdin);
fastio;
int t = 1;
while (t--){
solve1();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 19192kb
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: 0ms
memory: 17680kb
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: 4ms
memory: 19624kb
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: 0ms
memory: 16084kb
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 ...