QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#688231#8941. Even or Odd Spanning TreeHTensor#WA 84ms3812kbC++232.4kb2024-10-30 01:16:532024-10-30 01:16:54

Judging History

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

  • [2024-10-30 01:16:54]
  • 评测
  • 测评结果:WA
  • 用时:84ms
  • 内存:3812kb
  • [2024-10-30 01:16:53]
  • 提交

answer

#include <bits/stdc++.h>
#define dd(x) cout << #x << "\n"
#define d(x) cout << #x  << ": " << x << "\n"
#define SZ(x) ((int)(x).size())
using namespace std;
#define int long long
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vii = vector<vector<int>>;
using a3 = array<int, 3>;
using ll = long long;
const int inf = 0x3f3f3f3f3f3f3f3fLL;

#define MULTI_TEST

class DSU {
public:
    vector<int> fa, sz;
    DSU(int n) : fa(n + 1), sz(n + 1) {
        iota(fa.begin(), fa.end(), 0);
        fill(sz.begin() + 1, sz.end(), 1);
    }
    int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
    void merge(int a, int b) {
        int x = find(a), y = find(b); 
        if(x == y) return ;
        fa[y] = x;
        sz[x] += sz[y];
    }
};

void solve() {
    int n, m; cin >> n >> m;
    vector<array<int, 3>> edg(m);
    vector<int> used(m);
    vector adj(n + 1, vector<int> ());
    for(int i = 0; i < m; i++) {
        int u, v, w; cin >> u >> v >> w;
        edg[i] = {w, u, v};
    }
    ranges::sort(edg);
    DSU dsu(n);
    int sum = 0;
    for(int i = 0; i < SZ(edg); i++) {
        auto [w, u, v] = edg[i];
        int f1 = dsu.find(u), f2 = dsu.find(v);
        
        if(f1 == f2) continue;
        dsu.merge(f1, f2);
        used[i] = 1;
        sum += w;

        // adj[f1].push_back(f2);
        // adj[f1].push_back(f1);
    }

    if(dsu.sz[dsu.find(1)] != n) {
        cout << "-1 -1\n";
        return ;
    }

    array<int, 2> ans{sum, inf};
    array<int, 2> minx{inf, inf};

    for(int i = 0; i < m; i++) {
        auto [w, u, v] = edg[i];
        if(!used[i]) {
            minx[w % 2] = min(minx[w % 2], w);
        }
    }

    for(int i = 0; i < m; i++) {
        auto [w, u, v] = edg[i];
        if(used[i]) {
            ans[1] = min(ans[1], sum - w + minx[(w % 2) ^ 1]);
        }
    }

    if(ans[1] == inf) {
        if(ans[0] % 2 == 0) cout << ans[0] << " -1\n";
        else cout << "-1 " << ans[0] << "\n";
        return ;
    }

    if(ans[0] % 2 != 0) {
        swap(ans[0], ans[1]);
    } 
    cout << ans[0] << " " << ans[1] << "\n";
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
#ifdef MULTI_TEST
    int T; cin >> T;
#else
    int T = 1;
#endif
    while(T--) solve();
    return 0;
}

/*

3
2 1
1 2 5

3 1
1 3 1

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

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 1
1 2 5
3 1
1 3 1
4 4
1 2 1
1 3 1
1 4 1
2 4 2

output:

-1 5
-1 -1
4 3

result:

ok 6 numbers

Test #2:

score: -100
Wrong Answer
time: 84ms
memory: 3608kb

input:

10000
16 50
1 2 649251632
2 3 605963017
3 4 897721528
4 5 82446151
5 6 917109168
6 7 79647183
7 8 278566178
7 9 573504723
3 10 679859251
8 11 563113083
1 12 843328546
10 13 594049160
11 14 997882839
7 15 569431750
2 16 244494799
16 7 960172373
13 4 317888322
13 3 446883598
9 3 678142319
5 8 43208692...

output:

3140067932 2653082333
1262790434 1126848677
1263932600 1307926149
910589338 1180186165
2248358640 2094849259
3585304388 3738936375
960749696 1058677117
3168165864 3402127725
956365412 1187873655
1395482806 1132501971
3456885934 3177730743
3943951826 3628098353
2479987500 2281098739
2909126794 243324...

result:

wrong answer 2nd numbers differ - expected: '3159441841', found: '2653082333'