QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#617778#8941. Even or Odd Spanning TreeLegend_dy#WA 134ms3636kbC++203.3kb2024-10-06 17:04:322024-10-06 17:04:32

Judging History

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

  • [2024-10-06 17:04:32]
  • 评测
  • 测评结果:WA
  • 用时:134ms
  • 内存:3636kb
  • [2024-10-06 17:04:32]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define int long long

constexpr int inf = 1e18;

struct Edge {
    int u, v, w, id;
    bool operator<(const Edge A) const {
        return w <= A.w;
    }
};

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

    vector g(n + 1, vector<pair<int, int>>());
    vector<Edge> e;
    for (int i = 1, u, v, w; i <= m; i++) {
        cin >> u >> v >> w;
        e.push_back({u, v, w, i});
    }

    sort(e.begin(), e.end());
    vector<int> p(n + 1);
    iota(p.begin(), p.end(), 0);
    auto find = [&](auto self, int x) -> int {
        return x == p[x] ? x : p[x] = self(self, p[x]);
    };

    vector<bool> vis(m + 1, false);
    vector<int> ans(3, inf);
    ans[0] = 0;
    for (auto [u, v, w, id] : e) {
        int fx = find(find, u);
        int fy = find(find, v);
        if (fx == fy) continue;
        vis[id] = true;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
        ans[0] += w;
        p[fx] = fy;
    }

    int num = 0;
    for (int i = 1; i <= n; i++) {
        if (find(find, i) == i) num++;
    }

    if (num != 1) {
        cout << "-1 -1\n";
        return;
    }

    vector<int> dep(n + 1);
    vector f(n + 1, vector<int>(20));
    vector val(n + 1, vector<array<int, 2>>(20, {-inf, -inf}));
    auto dfs = [&](auto self, int u, int fa) -> void {
        f[u][0] = fa;
        dep[u] = dep[fa] + 1;
        for (auto [v, w] : g[u]) {
            if (v == fa) continue;
            if (w & 1) val[v][0][1] = w;
            else val[v][0][0] = w;
            self(self, v, u);
        }   
    };
    dfs(dfs, 1, 0);
    
    int opt;
    if (ans[0] & 1) ans[2] = ans[0], opt = 1;
    else ans[1] = ans[0], opt = 2;

    for (int j = 1; j <= 19; j++) {
        for (int i = 1; i <= n; i++) {
            f[i][j] = f[f[i][j - 1]][j - 1];
            for (int k = 0; k < 2; k++) {
                val[i][j][k] = max(val[i][j - 1][k], val[f[i][j - 1]][j - 1][k]);
            }
        }
    }

    auto Lca = [&](int u, int v) {
        if (dep[u] < dep[v]) swap(u, v);
        for (int i = 19; i >= 0; i--) {
            if (dep[f[u][i]] >= dep[v]) {
                u = f[u][i];
            }
        }
        if (u == v) return u;
        for (int i = 19; i >= 0; i--) {
            if (f[u][i] != f[v][i]) {
                u = f[u][i], v = f[v][i];
            }
        }
        return f[u][0];
    };

    for (auto [u, v, w, id] : e) {
        if (vis[id]) continue;
        int lca = Lca(u, v);
        int odd_mx = -inf, even_mx = -inf;
        for (int j = 19; j >= 0; j--) {
            if (dep[f[u][j]] >= dep[lca]) {
                even_mx = max(even_mx, val[u][j][0]);
                odd_mx = max(odd_mx, val[u][j][1]);
                u = f[u][j];
            }
        }
        if (w & 1) {
            ans[opt] = min(ans[opt], ans[0] + w - even_mx);
        }
        else ans[opt] = min(ans[opt], ans[0] + w - odd_mx);
    }
    if (ans[1] == inf) ans[1] = -1;
    if (ans[2] == inf) ans[2] = -1;
    cout << ans[1] << " " << ans[2] << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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

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: 134ms
memory: 3636kb

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 3191118501
1262790434 1309454727
1263932600 1332398901
1189242112 1180186165
2248358640 2261370885
3867619550 3738936375
1093500444 1058677117
3549645952 3402127725
1201099898 1187873655
1395482806 1411327105
3456885934 3535801445
3943951826 3988876469
2479987500 2727710253
2909126794 293...

result:

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