QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#254152#5548. Increase the Toll Feeswarner1129WA 47ms6512kbC++202.6kb2023-11-18 02:23:552023-11-18 02:23:56

Judging History

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

  • [2023-11-18 02:23:56]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:6512kb
  • [2023-11-18 02:23:55]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
template<class... T> void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); }
template<class T> void org(T l, T r) { while (l != r) cerr << ' ' << *l++; cerr << '\n'; }
#define debug(x...) dbg(#x, '=', x, '\n')
#define olist(x...) dbg(#x, '='), org(x)
#else
#define debug(...) ((void)0)
#define olist(...) ((void)0)
#endif
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;

template<class T>
inline constexpr T inf = numeric_limits<T>::max() / 2;
constexpr int mod = 998244353;

template<class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template<class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }
template<class... T> int add(T... x) { int t{}; return (((t += x) %= mod), ...), t; }
template<class... T> int mul(T... x) { i64 t{1}; return (((t *= x) %= mod), ...), t; }

struct DSU {
    vector<int> f, g, e;
    DSU(int n) : f(n, -1), g(n, 1), e(n, -1) {}
    int Find(int x) { return f[x] < 0 ? x : f[x] = Find(f[x]); }
    bool Union(int x, int y, int w) {
        x = Find(x), y = Find(y);
        if (x == y) return false;
        if (g[x] > g[y]) swap(x, y);
        g[y] += g[x];
        f[x] = y;
        e[x] = w;
        return true;
    }
    int Size(int x) {
        return g[Find(x)];
    }
    int qry(int x, int y) {
        int r = 0;
        while (x != y) {
            if (g[x] > g[y]) swap(x, y);
            chmax(r, e[x]);
            x = f[x];
        }
        return r;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;

    vector<array<int, 3>> edg(m);
    for (auto &[w, u, v] : edg) {
        cin >> u >> v >> w;
        u--, v--;
    }

    sort(all(edg));

    DSU dsu(n);
    
    vector<bool> use(m);
    for (int i = 0; i < m; i++) {
        auto [w, u, v] = edg[i];
        use[i] = dsu.Union(u, v, -1);
    }

    dsu = DSU(n);
    for (int i = 0; i < m; i++) {
        if (use[i]) continue;
        auto [w, u, v] = edg[i];
        dsu.Union(u, v, w);
    }

    if (dsu.Size(0) != n) {
        cout << "-1\n";
        return;
    }

    i64 ans = 0;
    for (int i = 0; i < m; i++) if (use[i]) {
        auto [w, u, v] = edg[i];
        ans += max(dsu.qry(u, v) + 1 - w, 0);
    }

    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

input:

2 1
1 2 1

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 26ms
memory: 5196kb

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: 47ms
memory: 6512kb

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

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:

28822601178070

result:

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