QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606812#8941. Even or Odd Spanning TreeUESTC_DECAYALI#WA 127ms12304kbC++202.2kb2024-10-03 12:25:362024-10-03 12:25:37

Judging History

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

  • [2024-10-03 12:25:37]
  • 评测
  • 测评结果:WA
  • 用时:127ms
  • 内存:12304kb
  • [2024-10-03 12:25:36]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<array>
#define int long long
#define RI register int
#define CI const int&
using namespace std;
const int N=500005,INF=1e18;
int t,n,m,fa[N],dep[N],anc[N][20],mx[N][20][2];
array <int,3> E[N]; vector <pair <int,int>> T[N];
inline int getfa(CI x)
{
    return fa[x]!=x?fa[x]=getfa(fa[x]):x;
}
inline void DFS(CI now=1,CI fa=0)
{
    dep[now]=dep[fa]+1; anc[now][0]=fa;
    for (RI i=0;i<19;++i)
    if (anc[now][i])
    {
        anc[now][i+1]=anc[anc[now][i]][i];
        for (RI j=0;j<2;++j) mx[now][i+1][j]=max(mx[now][i][j],mx[anc[now][i]][i][j]);
    } else break;
    for (auto [to,w]:T[now]) if (to!=fa)
    {
        mx[to][0][w&1]=w; mx[to][0][(w&1)^1]=INF;
        DFS(to,now);
    }
}
inline int query(int x,int y,CI tp,int ret=0)
{
    if (dep[x]<dep[y]) swap(x,y);
    for (RI i=19;i>=0;--i) if (dep[anc[x][i]]>=dep[y])
    ret=max(ret,mx[x][i][tp]),x=anc[x][i];
    if (x==y) return ret;
    for (RI i=19;i>=0;--i) if (anc[x][i]!=anc[y][i])
    ret=max(ret,max(mx[x][i][tp],mx[y][i][tp])),x=anc[x][i],y=anc[y][i];
    return max(ret,max(mx[x][0][tp],mx[y][0][tp]));
}
signed main()
{
    for (scanf("%lld",&t);t;--t)
    {
        scanf("%lld%lld",&n,&m);
        for (RI i=1;i<=m;++i) scanf("%lld%lld%lld",&E[i][1],&E[i][2],&E[i][0]);
        sort(E+1,E+m+1); vector <array <int,3>> Rep;
        for (RI i=1;i<=n;++i) fa[i]=i,T[i].clear();
        int sum1=0,cnt=0;
        for (RI i=1;i<=m;++i)
        {
            auto [w,u,v]=E[i];
            if (getfa(u)==getfa(v))
            {
                Rep.push_back(E[i]); continue;
            }
            fa[getfa(u)]=getfa(v); sum1+=w; ++cnt;
            T[u].push_back({v,w}); T[v].push_back({u,w});
        }
        if (cnt!=n-1)
        {
            puts("-1 -1"); continue;
        }
        int sum2=INF; DFS();
        for (auto [w,u,v]:Rep)
        {
            int ww=query(u,v,(w&1)^1);
            if (ww!=INF) sum2=min(sum2,sum1-ww+w);
        }
        if (sum1&1) swap(sum1,sum2);
        if (sum1==INF) sum1=-1;
        if (sum2==INF) sum2=-1;
        printf("%lld %lld\n",sum1,sum2);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 127ms
memory: 12024kb

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 3416475051
1262790434 1468654349
1263932600 1333657263
1189242112 1180186165
2248358640 2277656157
3867619550 3738936375
1207733704 1058677117
3451376434 3402127725
1201099898 1187873655
1395482806 1440596255
3456885934 3486092007
3943951826 4005009799
2479987500 2752449745
2909126794 388...

result:

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