QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607076#8941. Even or Odd Spanning TreeITC_TL#WA 129ms18004kbC++206.0kb2024-10-03 13:51:302024-10-03 13:51:30

Judging History

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

  • [2024-10-03 13:51:30]
  • 评测
  • 测评结果:WA
  • 用时:129ms
  • 内存:18004kb
  • [2024-10-03 13:51:30]
  • 提交

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 getzu(zu[x]);
}
void merge(ll x, ll y)
{
    zu[getzu(y)] = 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 0;
        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 0;
            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 0;
        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 0;
            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(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 = 0;
            fore(i, 1, m)
            {
                if (!vis[i])
                {
                    if (ken[i].w % 2 == 1)
                    {
                        ans2 = max(lca1(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                    else
                    {
                        ans2 = max(lca2(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                }
            }
            if (ans2 == 0)
            {
                cout << -1 << " " << ans1 << endl;
            }
            else
            {
                cout << ans2 << " " << ans1 << endl;
            }
        }
        else
        {
            ans2 = 0;
            fore(i, 1, m)
            {
                if (!vis[i])
                {
                    if (ken[i].w % 2 == 1)
                    {
                        ans2 = max(lca1(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                    else
                    {
                        ans2 = max(lca2(ken[i].l, ken[i].r, ken[i].w), ans2);
                    }
                }
            }
            if (ans2 == 0)
            {
                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: 18004kb

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: 129ms
memory: 17948kb

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 3941554631
1262790434 2124445815
1263932600 2177809565
2128472300 1180186165
2248358640 3162318131
4696867870 3738936375
2011213264 1058677117
4144333014 3402127725
2081445350 1187873655
1395482806 2324672773
3456885934 4302070719
3943951826 4702420591
2479987500 3216859457
2909126794 388...

result:

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