QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#589057#5548. Increase the Toll FeesMay_27thWA 43ms12728kbC++201.9kb2024-09-25 15:57:232024-09-25 15:57:23

Judging History

你现在查看的是最新测评结果

  • [2024-09-25 15:57:23]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:12728kb
  • [2024-09-25 15:57:23]
  • 提交

answer

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
  
using namespace std;
  
#define i64 long long
#define int long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
  
const int MAXN = 5e5 + 5;
const i64 INF = LLONG_MAX/2;
struct DSU{
  int N, ccp;
  vector<int> lab, sz;
  vector<int64_t> sum;
  DSU(int _N) : N(_N), ccp(_N), sum(N + 5, 0), sz(_N + 5, 1), lab(_N + 5) {
    for (int i = 1; i <= N; i ++) {
      lab[i] = i;
    }
  }; 
  int findLab(int u) { return (u == lab[u] ? u : lab[u] = findLab(lab[u])); }
  bool unite(int u, int v, int64_t w) {
    int labu = findLab(u);
    int labv = findLab(v);
    if (labu == labv) return false;
    if (sz[labu] < sz[labv]) swap(labu, labv);
    sz[labu] += sz[labv];
    lab[labv] = labu;
    sum[labu] += sum[labv] + w;
    ccp --;
    return true;
  }
};
int N, M;
struct Edge {
  int u, v, w, id;
};
void Solve(void) {
  cin >> N >> M;
  vector<Edge> edges;
  vector<bool> used(M + 2, false);
  for (int i = 1; i <= M; i ++) {
    int u, v, w; cin >> u >> v >> w;
    edges.pb({u, v, w, i});
  }
  sort(all(edges), [&](Edge &a, Edge &b) {
    return a.w < b.w;
  });
  DSU MST(N);
  for (auto [u, v, w, id] : edges) {
    if (MST.unite(u, v, w)) {
      used[id] = true;
    }
  }
  int root = MST.findLab(1);
  i64 add = MST.sum[root];
  DSU NMST(N);
  for (auto [u, v, w, id] : edges) {
    if (used[id]) continue;
    if (NMST.unite(u, v, w)) {
    }
  }
  root = NMST.findLab(1);
  if (NMST.ccp != 1) {
    cout << -1 << "\n";
    return;
  }
  add = NMST.sum[root] - add + N - 1;
  cout << add << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(10);
  int Tests = 1; // cin >> Tests; 
  while (Tests --) {
    Solve();    
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3572kb

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: 3868kb

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: 0ms
memory: 3568kb

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: 3632kb

input:

2 1
1 2 1

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 13ms
memory: 7644kb

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: 43ms
memory: 12728kb

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: -100
Wrong Answer
time: 15ms
memory: 6404kb

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:

14315443217237

result:

wrong answer 1st lines differ - expected: '28822649262260', found: '14315443217237'