QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#457063#7069. FarmbobbilykingWA 111ms20884kbC++203.5kb2024-06-29 00:01:002024-06-29 00:01:02

Judging History

This is the latest submission verdict.

  • [2024-06-29 00:01:02]
  • Judged
  • Verdict: WA
  • Time: 111ms
  • Memory: 20884kb
  • [2024-06-29 00:01:00]
  • Submitted

answer


#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <vector>
#pragma GCC target ("avx2")

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)

#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)

template<typename A, typename B>
A& chmax(A &a, B b) { return a < b ? (a=b): a; }

template<typename A, typename B>
A& chmin(A &a, B b) { return a > b ? (a=b): a; }




const ll NN = 1e5 + 10;

//DSU
ll parent[NN];
ll find(ll a){ return a == parent[a] ? a : parent[a] = find(parent[a]); }
bool merge(ll u, ll v) {
    u = find(u), v=find(v);
    parent[v] = u;
    return !(u == v);
}



int main(){
//    freopen("a.in", "r", stdin);
//    freopen("a.out", "w", stdout);

    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(20);

    G(n) G(m)

    vector<array<ll, 3>> edges(m);
    for (auto &[x, y, z]: edges) {
        cin >> x >> y >> z; --x,--y;
    }
    G(q) 
    vector<array<ll, 2>> choice(q);
    for (auto &[x, y]: choice) {
        cin >> x >> y; --x,--y;
    }

    iota(parent, parent+NN, 0ll);

    for (auto [x, y]: choice) {
        merge(edges[x][0], edges[x][1]);
        merge(edges[y][0], edges[y][1]);
    }
    vl ord(m); iota(A(ord), 0ll);
    sort(A(ord), [&](const auto &a, const auto &b) {
        return edges[a][2] < edges[b][2];
    });

    vector<bool> used(m);
    ll tot = 0;
    for (auto x: ord) if (merge(edges[x][0], edges[x][1] ) ) {
        used[x] = 1;
        tot += edges[x][2];
    }

    // need to do connectivity check here, otherwise blows up next part 
    F(i, 0, n) if (find(i) != find(0)) EX(-1)

    ll ans = 1e18;

    iota(parent, parent+NN, 0ll);


    F(i, 0, m) if (used[i]) {
        assert(merge(edges[i][0], edges[i][1]));
    } 

    vl rr(n, -1);

    ll CC = 0;
    F(i, 0, n) if (rr[find(i)] == -1) rr[find(i)] = CC++;

    vector<array<ll, 3>> nedges; // all not used edges  
    // forced edges we access through edges still 

    map<pl, ll> mapnedges;

    F(i, 0, m) {
        F(j, 0, 2) edges[i][j] = rr[find(edges[i][j])];
        if (!used[i]) {
            auto key = minmax(edges[i][0], edges[i][1]);
            if (key.K == key.V) continue;
            if (!mapnedges.count(key)) mapnedges[key] = 1e18;
            chmin(mapnedges[key], edges[i][2]);
        }
    }

    for (auto [k, v]: mapnedges) {
        nedges.push_back({k.K, k.V, v});
    }

    sort(A(nedges), [&](const auto &a, const auto &b) {
        return a[2] < b[2];
    });

    F(bm, 0, 1<<q) {
        iota(parent, parent+CC, 0ll);
        ll tans = 0;
        bool fail = 0;

        F(i, 0, q) {
            auto e = choice[i][bool(bm & 1<<i)];
            merge(edges[e][0], edges[e][1]); // this is fine, it's okay lol, doesn't HAVE to be a tree
            tans += edges[e][2];
        }

        for (auto [x, y, z]: nedges) {
            if (merge(x, y)) tans += z;
        }

        F(i, 0, CC) if (find(0) != find(i)) fail = 1;

        if (!fail) chmin(ans, tans);
    }


    if (ans > 1e17) EX(-1)
    EX(ans + tot)
    
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4372kb

input:

4 6
1 1 2
2 4 3
1 1 4
2 4 4
3 2 4
1 3 4
1
1 2

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 107ms
memory: 20100kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 111ms
memory: 20884kb

input:

100000 500000
34497 87538 658
69862 2776 861
93620 16992 904
77910 81200 149
83935 83752 880
17602 75791 259
85887 53289 710
4200 79358 181
8518 19264 737
94665 47462 822
50632 51994 143
55224 59127 656
615 92858 150
48450 9465 58
35713 45287 140
64861 32248 517
70296 45113 153
11189 90316 809
40673...

output:

12148224

result:

ok single line: '12148224'

Test #4:

score: -100
Wrong Answer
time: 80ms
memory: 20016kb

input:

1 500000
1 1 963
1 1 349
1 1 157
1 1 6
1 1 312
1 1 377
1 1 783
1 1 42
1 1 18
1 1 327
1 1 499
1 1 824
1 1 343
1 1 798
1 1 193
1 1 667
1 1 378
1 1 641
1 1 692
1 1 622
1 1 584
1 1 590
1 1 324
1 1 858
1 1 914
1 1 601
1 1 734
1 1 61
1 1 559
1 1 681
1 1 825
1 1 888
1 1 585
1 1 55
1 1 818
1 1 190
1 1 278
1...

output:

3212

result:

wrong answer 1st lines differ - expected: '1605', found: '3212'