QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607368#8941. Even or Odd Spanning TreeITC_TL#WA 245ms36716kbC++206.2kb2024-10-03 14:48:152024-10-03 14:48:19

Judging History

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

  • [2024-10-03 14:48:19]
  • 评测
  • 测评结果:WA
  • 用时:245ms
  • 内存:36716kb
  • [2024-10-03 14:48:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fore(i, l, r) for (ll i = (int)l; i <= (int)r; i++)
#define ford(i, r, l) for (ll i = (int)r; i >= (int)l; i--)
const int MAXN = 2123456LL;
ll T, n, m, a[MAXN], zu[MAXN];
ll tot, hand[MAXN], nxt[MAXN], to[MAXN], val[MAXN], dep[MAXN];
ll fa[MAXN][31], qi[MAXN][31], ou[MAXN][31];
struct node
{
    ll l, r, w;
} ken[MAXN];
void add(ll x, ll y, ll w)
{
    tot++;
    to[tot] = y;
    nxt[tot] = hand[x];
    val[tot] = w;
    hand[x] = tot;
}
bool cmp(node x, node y)
{
    return x.w < y.w;
}
ll getzu(ll x)
{
    if (x == zu[x])
        return x;
    return zu[x] = getzu(zu[x]);
}
void merge(ll x, ll y)
{
    zu[getzu(y)] = zu[getzu(x)];
}
ll sum1 = 0, ans1, ans2;
void sou(ll t)
{
    fore(i, 1, 30)
    {
        fa[t][i] = fa[fa[t][i - 1]][i - 1];
        qi[t][i] = max(qi[t][i - 1], qi[fa[t][i - 1]][i - 1]);
        ou[t][i] = max(ou[t][i - 1], ou[fa[t][i - 1]][i - 1]);
    }
    sum1++;
    for (int i = hand[t]; i; i = nxt[i])
    {
        if (to[i] != 1 && fa[to[i]][0] == 0)
        {
            dep[to[i]] = dep[t] + 1;
            ans1 += val[i];
            fa[to[i]][0] = t;
            if (val[i] % 2 == 1)
            {
                qi[to[i]][0] = val[i];
                ou[to[i]][0] = 0;
            }
            else
            {
                qi[to[i]][0] = 0;
                ou[to[i]][0] = val[i];
            }
            sou(to[i]);
        }
    }
}
bool vis[MAXN];
ll lca1(ll x, ll y, ll w)
{
    if (dep[x] < dep[y])
        swap(x, y);
    ll retou = 0;
    ford(i, 30, 0)
    {
        if (dep[fa[x][i]] > dep[y])
            retou = max(retou, ou[x][i]), x = fa[x][i];
    }
    fore(i, 0, 30)
    {
        if (dep[fa[x][i]] == dep[y])
        {
            retou = max(retou, ou[x][i]), x = fa[x][i];
            break;
        }
    }

    if (x == y)
    {
        if (retou == 0)
            return 999999999999ll;
        return ans1 - retou + w;
    }
    ford(i, 30, 0)
    {
        if (fa[x][i] != fa[y][i])
        {
            retou = max(retou, ou[x][i]), x = fa[x][i];
            retou = max(retou, ou[y][i]), y = fa[y][i];
        }
    }
    fore(i, 0, 30)
    {
        if (fa[x][i] == fa[y][i])
        {
            retou = max(retou, ou[x][i]), x = fa[x][i];
            retou = max(retou, ou[y][i]), y = fa[y][i];
            if (retou == 0)
                return 999999999999ll;
            return ans1 - retou + w;
        }
    }
}
ll lca2(ll x, ll y, ll w)
{
    // cout << x << "    " << y << "   " << qi[x][10] << endl;
    if (dep[x] < dep[y])
        swap(x, y);
    ll retqi = 0;
    ford(i, 30, 0)
    {
        if (dep[fa[x][i]] > dep[y])
            retqi = max(retqi, qi[x][i]), x = fa[x][i];
    }
    fore(i, 0, 30)
    {
        if (dep[fa[x][i]] == dep[y])
        {
            retqi = max(retqi, qi[x][i]), x = fa[x][i];
            break;
        }
    }
    if (x == y)
    {
        if (retqi == 0)
            return 999999999999ll;
        return ans1 - retqi + w;
    }
    ford(i, 30, 0)
    {
        if (fa[x][i] != fa[y][i])
        {
            retqi = max(retqi, qi[x][i]), x = fa[x][i];
            retqi = max(retqi, qi[y][i]), y = fa[y][i];
        }
    }
    fore(i, 0, 30)
    {
        if (fa[x][i] == fa[y][i])
        {
            retqi = max(retqi, qi[x][i]), x = fa[x][i];
            retqi = max(retqi, qi[y][i]), y = fa[y][i];
            if (retqi == 0)
                return 999999999999ll;
            return ans1 - retqi + w;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        fore(i, 1, n)
        {
            zu[i] = i;
            fa[i][0] = 0;
            fore(j, 0, 30) qi[i][j] = 0, ou[i][j] = 0;
        }

        fore(i, 1, m)
        {
            cin >> ken[i].l >> ken[i].r >> ken[i].w;
        }
        sort(ken + 1, ken + 1 + m, cmp);
        tot = 0;
        fore(i, 1, n) hand[i] = 0;
        fore(i, 1, m)
        {
            vis[i] = 0;
            if (getzu(ken[i].l) != getzu(ken[i].r))
            {
                merge(ken[i].l, ken[i].r);
                add(ken[i].l, ken[i].r, ken[i].w);
                add(ken[i].r, ken[i].l, ken[i].w);
                vis[i] = 1;
            }
        }
        sum1 = 0;
        ans1 = 0;
        sou(1);
        if (sum1 != n)
        {
            cout << -1 << " " << -1 << endl;
            continue;
        }
        if (ans1 % 2 == 1)
        {
            ans2 = 999999999999ll;
            fore(i, 1, m)
            {
                if (!vis[i])
                {
                    if (ken[i].w % 2 == 1)
                    {
                        ans2 = min(lca1(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                    else
                    {
                        ans2 = min(lca2(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                }
            }
            if (ans2 == 999999999999ll)
            {
                cout << -1 << " " << ans1 << endl;
            }
            else
            {
                cout << ans2 << " " << ans1 << endl;
            }
        }
        else
        {
            ans2 = 999999999999ll;
            fore(i, 1, m)
            {
                if (!vis[i])
                {
                    if (ken[i].w % 2 == 1)
                    {
                        ans2 = min(lca1(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                    else
                    {
                        ans2 = min(lca2(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                }
            }
            if (ans2 == 999999999999ll)
            {
                cout << ans1 << " " << -1 << endl;
            }
            else
            {
                cout << ans1 << " " << ans2 << endl;
            }
        }
    }
    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

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

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: 0
Accepted
time: 123ms
memory: 22172kb

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:

ok 20000 numbers

Test #3:

score: 0
Accepted
time: 172ms
memory: 22120kb

input:

100
1806 5000
1 2 139994119
2 3 184924112
3 4 670813536
4 5 443325848
5 6 767909046
6 7 531742851
7 8 364385440
8 9 918195219
9 10 731896671
10 11 671688362
11 12 558665746
12 13 154497842
13 14 28636459
14 15 570343686
15 16 419626123
16 17 817852951
17 18 517701907
18 19 952451718
19 20 141747977
...

output:

380509244078 380509217939
236564714828 236564588423
317690341848 317690193297
416922743878 416923542879
411997066938 411996924725
231780454372 231780901543
156856996316 156856345151
239278757000 239278493321
288480848080 288481502909
347301406524 347301269265
362642903994 362643090967
158361335208 1...

result:

ok 200 numbers

Test #4:

score: -100
Wrong Answer
time: 245ms
memory: 36716kb

input:

10
15547 50000
1 2 762013057
2 3 473514078
3 4 73078629
4 5 932961699
5 6 157436174
6 7 190061912
7 8 140204258
8 9 737720271
9 10 386190699
10 11 92718916
11 12 68384258
12 13 389445848
13 14 906761733
14 15 644882062
15 16 429774551
16 17 330771475
17 18 815808541
18 19 239714301
19 20 350847429
2...

output:

2833348523676 -1
4133065384586 -1
3395129351174 -1
-1 4109233300341
4201582590330 -1
-1 4055209286787
4274470524320 -1
4221591172170 -1
-1 3155641574871
-1 3656162010817

result:

wrong answer 2nd numbers differ - expected: '2833348592065', found: '-1'