QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#660649 | #5548. Increase the Toll Fees | temmie# | RE | 34ms | 23700kb | C++17 | 4.0kb | 2024-10-20 12:32:40 | 2024-10-20 12:32:40 |
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 = 1.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]) {
ret = max(ret, max(jval[v][j], jval[u][j]));
v = jmp[v][j];
u = jmp[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[ (jmp[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("pM2.in", "r", stdin);
fastio;
int t = 1;
while (t--){
solve1();
}
return 0;
}
// 2048 MB
// 2e3 * 1e6 / 8 = 2.5e8 long long
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11260kb
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: 2ms
memory: 7608kb
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: 11148kb
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: 9044kb
input:
2 1 1 2 1
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 34ms
memory: 23700kb
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: -100
Runtime Error
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...