QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606969#8941. Even or Odd Spanning TreeKomorebie#WA 160ms3752kbC++173.2kb2024-10-03 13:20:022024-10-03 13:20:03

Judging History

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

  • [2024-10-03 13:20:03]
  • 评测
  • 测评结果:WA
  • 用时:160ms
  • 内存:3752kb
  • [2024-10-03 13:20:02]
  • 提交

answer

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

#define ll long long
#define PII pair<int, ll>
const ll N = 2e5 + 10, M = 5e5 + 10, inf = 0x3f3f3f3f;
ll n, m, p[N];
struct edge {
    ll a, b, w;
    bool operator<(const edge& a) const { return this->w < a.w; }
};

ll find(ll x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void solve()
{
    cin >> n >> m;
    vector<edge> ed(m + 1);
    for (int i = 1; i <= m; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        ed[i] = {a, b, c};
    }
    sort(ed.begin() + 1, ed.end());
    for (int i = 1; i <= n; i++) p[i] = i;
    unordered_set<ll> st;
    vector<vector<PII>> G(n + 1);
    ll res = 0;
    for (int i = 1; i <= m; i++) {
        ll x = ed[i].a, y = ed[i].b, z = ed[i].w;
        if (find(x) != find(y)) {
            p[find(x)] = find(y);
            res += z;
            st.insert(i);
        }
    }

    if (st.size() < n - 1) {
        cout << -1 << " " << -1 << endl;
        return;
    }

    for (int i = 1; i <= m; i++) {
        if (st.count(i)) {
            ll x = ed[i].a, y = ed[i].b, z = ed[i].w;
            G[x].push_back({y, z}), G[y].push_back({x, z});
        }
    }

    vector<vector<int>> fa(n + 1, vector<int>(31));
    vector<vector<vector<ll>>> F(2, vector<vector<ll>>(n + 1, vector<ll>(31)));
    vector<int> dep(n + 1);

    auto dfs = [&](auto dfs, int u, int f, ll fw) -> void {
        if (fw & 1) {
            F[1][u][0] = fw;
        }
        else {
            F[0][u][0] = fw;
        }
        fa[u][0] = f;
        dep[u] = dep[f] + 1;
        for (int i = 1; i < 31; i++) {
            fa[u][i] = fa[fa[u][i - 1]][i - 1];
            F[1][u][i] = max(F[1][fa[u][i - 1]][i - 1], F[1][u][i - 1]);
            F[0][u][i] = max(F[0][fa[u][i - 1]][i - 1], F[0][u][i - 1]);
        }
        for (auto [v, w] : G[u]) {
            if (v != f) {
                dfs(dfs, v, u, w);
            }
        }
    };

    auto get = [&](int x, int y, int idx) -> ll {
        if (dep[x] < dep[y]) {
            swap(x, y);
        }
        int d = dep[x] - dep[y];
        ll tmp = 0;
        for (int i = 0; (1 << i) <= d; i++) {
            if ((d >> i) & 1) {
                tmp = max(tmp, F[idx][x][i]);
                x = fa[x][i];
            }
        }
        if (x == y) return tmp;
        for (int i = log2(dep[y]); i >= 0; i--) {
            if (fa[x][i] != fa[y][i]) {
                tmp = max(tmp, F[idx][y][i]);
                tmp = max(tmp, F[idx][x][i]);
                x = fa[x][i];
                y = fa[y][i];
            }
        }
        return max({tmp, F[idx][x][0], F[idx][y][0]});
    };

    dfs(dfs, 1, 0, 0);

    ll ans = 1e18;

    for (int i = 1; i <= m; i++) {
        if (!st.count(i)) {
            auto [u, v, w] = ed[i];
            ll t = get(u, v, 1 - (w & 1ll));
            ans = min(res - t + w, ans);
        }
    }
    if (ans == 1e18) ans = -1;
    if (res & 1) {
        cout << ans << " " << res << endl;
    }
    else {
        cout << res << " " << ans << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    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: 3752kb

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: 160ms
memory: 3708kb

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 3159441841
1262790434 1309454727
1263932600 1307926149
1189242112 1180186165
2248358640 2261370885
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3988876469
2479987500 2535532661
2909126794 293...

result:

wrong answer 116th numbers differ - expected: '1207927187', found: '1120243322'