QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#604924#8941. Even or Odd Spanning TreePandaGhostTL 3ms102148kbC++143.9kb2024-10-02 14:36:282024-10-02 14:36:30

Judging History

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

  • [2024-10-02 14:36:30]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:102148kb
  • [2024-10-02 14:36:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
#define int long long 
#define FT first
#define SD second
const int N=200009;
int n,m,t,cnt;
pair<int,PII> e[N];
vector<PII> a[N];
vector<int> b;
int fa[N],vis[N],f[22][N],mx_od[22][N],mx_ev[22][N],dep[N];
LL ans,uans;
int find(int x)
{
    if(fa[x]!=x) fa[x]=find(fa[x]);
    return fa[x];
}
void merge(int x,int y)
{
    fa[find(x)]=y;
}
void dfs(int x,int fa)
{
    f[0][x]=fa;
    dep[x]=dep[fa]+1;
    for(PII i:a[x])
    {
        if(i.FT==fa) continue;
        if(i.SD%2) mx_od[0][i.FT]=i.SD;
        else mx_ev[0][i.FT]=i.SD;
    }
}
signed main()
{
    scanf("%lld",&t);
    while(t--)
    {
        ans=cnt=0;
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld%lld%lld",&e[i].SD.FT,&e[i].SD.SD,&e[i].FT);
        }
        sort(e+1,e+m+1);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++)
        {
            if(find(e[i].SD.FT)!=find(e[i].SD.SD))
            {
                merge(e[i].SD.FT,e[i].SD.SD),ans+=e[i].FT,cnt++;
                a[e[i].SD.FT].emplace_back(e[i].SD.SD,e[i].FT);
                a[e[i].SD.SD].emplace_back(e[i].SD.FT,e[i].FT);
            }
            else b.push_back(i);
        }
        for(int i=1;i<=n;i++) vis[i]=0;
        if(cnt==n-1)
        {
            uans=0x3f3f3f3f3f3f+ans;
            mx_od[0][1]=mx_ev[0][1]=0;
            dfs(1,1);
            for(int i=1;i<=20;i++)
                for(int j=1;j<=n;j++)
                {
                    f[i][j]=f[i-1][f[i-1][j]];
                    mx_od[i][j]=max(mx_od[i-1][j],mx_od[i-1][f[i-1][j]]);
                    mx_ev[i][j]=max(mx_ev[i-1][j],mx_ev[i-1][f[i-1][j]]);
                }
            for(int i:b)
            {
                if(e[i].FT%2)
                {
                    int x=e[i].SD.FT,y=e[i].SD.SD,oans=0;
                    if(dep[y]>dep[x]) swap(x,y);
                    if(dep[x]!=dep[y])
                    {
                        for(int i=20;~i;i--)
                        {
                            if(dep[y]+(1<<i)>=dep[x]) oans=max(oans,mx_ev[i][x]),x=f[i][x];
                        }
                    }
                    if(x!=y)
                    {
                        for(int i=20;~i;i--)
                        {
                            if(f[i][x]!=f[i][y])
                                oans=max({oans,mx_ev[i][x],mx_ev[i][y]}),x=f[i][x],y=f[i][y];
                        }
                        oans=max({oans,mx_ev[0][x],mx_ev[0][y]});
                    }
                    uans=min(uans,ans-oans+e[i].FT);
                }
                else
                {
                    int x=e[i].SD.FT,y=e[i].SD.SD,oans=0;
                    if(dep[y]>dep[x]) swap(x,y);
                    if(dep[x]!=dep[y])
                    {
                        for(int i=20;~i;i--)
                        {
                            if(dep[y]+(1<<i)>=dep[x]) oans=max(oans,mx_od[i][x]),x=f[i][x];
                        }
                    }
                    if(x!=y)
                    {
                        for(int i=20;~i;i--)
                        {
                            if(f[i][x]!=f[i][y])
                                oans=max({oans,mx_od[i][x],mx_od[i][y]}),x=f[i][x],y=f[i][y];
                        }
                        oans=max({oans,mx_od[0][x],mx_od[0][y]});
                    }
                    uans=min(uans,ans-oans+e[i].FT);
                }
            }
            if(uans-ans>2e9) uans=-1;
            if(ans%2==0) printf("%lld %lld\n",ans,uans);
            else printf("%lld %lld\n",uans,ans);
        }
        else printf("-1 -1\n");
        for(int i=1;i<=n;i++) a[i].clear();
    }
}
/*
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
  
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 102148kb

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
Time Limit Exceeded

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 3222514083
1262790434 940235397
1263932600 1233489575
799178204 1180186165
2248358640 2106419765
3568922930 3738936375
792950958 1058677117
3228900960 3402127725
854486110 1187873655
1395482806 1053662753
3456885934 3217554857
3943951826 3669734227
2479987500 2366608175
2909126794 2754466...

result: