QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#789966#8941. Even or Odd Spanning TreeBaseAIWA 133ms87580kbC++204.3kb2024-11-27 23:15:162024-11-27 23:15:17

Judging History

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

  • [2024-11-27 23:15:17]
  • 评测
  • 测评结果:WA
  • 用时:133ms
  • 内存:87580kb
  • [2024-11-27 23:15:16]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define Endl '\n'
#define ll long long
#define int ll
#define ls first
#define rs second
#define pii pair<int, int>
#define pb push_back
const int N = 2e5 + 3;
const int LOGN = 19;
const int INF = 2e9;
ll n, m;
ll pre[N];
ll dep[N];
ll odd[LOGN + 1][N], even[LOGN + 1][N];
ll fa[LOGN + 1][N];
vector<array<ll, 3>> E;
vector<pii> e[N];
map<pii, ll> mp;
void init(int n)
{
    for (int i = 1; i <= n; i++) {
        pre[i] = i;
    }
}
int find(int x)
{
    if (pre[x] == x) {
        return x;
    }
    return pre[x] = find(pre[x]);
}
bool join(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (fx != fy) {
        pre[fx] = fy;
        return 1;
    }
    return 0;
}
void dfs(int u, int f)
{
    dep[u] = dep[f] + 1;
    for (auto [v, w] : e[u]) {
        if (v == f)
            continue;
        if (w & 1) {
            odd[0][v] = w;
            even[0][v] = 0;
        } else {
            odd[0][v] = 0;
            even[0][v] = w;
        }
        fa[0][v] = u;
        dfs(v, u);
    }
}
pii LCA(int u, int v)
{
    int ODD = 0, EVEN = 0;
    if (dep[u] < dep[v]) {
        swap(u, v);
    }
    for (int i = LOGN; i >= 0; i--) {
        if (dep[fa[i][u]] >= dep[v]) {
            ODD = max(ODD, odd[i][u]);
            EVEN = max(EVEN, even[i][u]);
            u = fa[i][u];
        }
    }
    if (u == v) {
        return { ODD, EVEN };
    }
    for (int i = LOGN; i >= 0; i--) {
        if (fa[i][u] != fa[i][v]) {
            ODD = max({ ODD, odd[i][u], odd[i][v] });
            EVEN = max({ EVEN, even[i][u], even[i][v] });
            u = fa[i][u];
            v = fa[i][v];
        }
    }
    ODD = max(ODD, odd[0][u]);
    EVEN = max(EVEN, even[0][u]);
    return { ODD, EVEN };
}
void solve()
{
    cin >> n >> m;
    mp.clear();
    E.clear();
    init(n);
    for (int i = 0; i <= n; i++) {
        e[i].clear();
        dep[i] = 0;
    }
    for (int i = 0; i <= LOGN; i++) {
        for (int j = 0; j <= n; j++) {
            odd[i][j] = 0;
            even[i][j] = 0;
            fa[i][j] = 0;
        }
    }
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        if (u > v) {
            swap(u, v);
        }
        E.pb({ w, u, v });
    }
    sort(E.begin(), E.end());
    ll ans = 0;
    int cnt = 0;
    for (auto [w, u, v] : E) {
        if (join(u, v)) {
            mp[{ u, v }] = w;
            ans += w;
            e[u].pb({ v, w });
            e[v].pb({ u, w });
            cnt++;
        }
    }
    if (cnt < n - 1) {
        cout << "-1 -1" << Endl;
        return;
    }
    dfs(1, 0);
    for (int i = 1; i <= LOGN; i++) {
        for (int j = 1; j <= n; j++) {
            odd[i][j] = max(odd[i - 1][j], odd[i - 1][fa[i - 1][j]]);
            even[i][j] = max(even[i - 1][j], even[i - 1][fa[i - 1][j]]);
            fa[i][j] = fa[i - 1][fa[i - 1][j]];
        }
    }
    ll res = INF;
    for (auto [w, u, v] : E) {
        auto now = mp.find({ u, v });
        if (now != mp.end()) {
            ll ww = (*now).rs;
            if (ww == w) {
                continue;
            }
            if (((w - ww) & 1) == 0) {
                continue;
            }
            res = min(res, w - ww);
            assert(w > ww);
            continue;
        }
        auto [ODD, EVEN] = LCA(u, v);
        if ((w % 2) && EVEN != 0) {
            res = min(res, w - EVEN);
            assert(w > EVEN);
        } else if ((w % 2 == 0) && ODD != 0) {
            res = min(res, w - ODD);
            assert(w > ODD);
        }
    }
    if (res == INF) {
        res = -1;
    }
    if (ans & 1) {
        if (res == -1) {
            cout << "-1 ";
        } else {
            cout << ans + res << " ";
        }
        cout << ans << Endl;
    } else {
        cout << ans << " ";
        if (res == -1) {
            cout << "-1\n";
        } else {
            cout << ans + res << "\n";
        }

        // cout << ans << " " << (res == -1 ? res : res + ans) << Endl;
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int _ = 1;
    cin >> _;

    while (_--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

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

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: 133ms
memory: 87580kb

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 1307926149
1189242112 1180186165
2248358640 2277656157
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3988876469
2479987500 2535532661
2909126794 294...

result:

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