QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695753#8941. Even or Odd Spanning Tree2018haha#RE 0ms20556kbC++172.3kb2024-10-31 20:43:302024-10-31 20:43:32

Judging History

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

  • [2024-10-31 20:43:32]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:20556kb
  • [2024-10-31 20:43:30]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using ll=long long;

const int N=500010;

int n,m;
vector<pair<int,int>> G[N];
int fa[N];
int dep[N];
int F[N][22],W[N][22][2];
void Dfs(int u,int fa)
{
    for(int j=1;j<21;j++)
    {
        F[u][j]=F[F[u][j-1]][j-1];
        W[u][j][0]=min(W[u][j-1][0],W[F[u][j-1]][j-1][0]);
        W[u][j][1]=min(W[u][j-1][1],W[F[u][j-1]][j-1][1]);
    }
    for(auto [v,w]:G[u])
    {
        if(v==fa) continue;
        F[v][0]=u;
        W[v][0][0]=W[v][0][1]=1000000010;
        W[v][0][w%2]=w;
        dep[v]=dep[u]+1;
        Dfs(v,u);
    }
}
int Find(int x)
{
    if(x==fa[x]) return x;
    return fa[x]=Find(fa[x]);
}
int Query(int x,int y,int id)
{
    if(dep[x]<dep[y]) swap(x,y);
    int ret=1000000010;
    for(int j=21;j>=0;j--)
        if(dep[F[x][j]]>=dep[y])
            ret=min(ret,W[x][j][id]),x=F[x][j];
    if(x==y) return ret;
    for(int j=21;j>=0;j--)
        if(F[x][j]!=F[y][j])
        {
            ret=min(ret,W[x][j][id]);
            ret=min(ret,W[y][j][id]);
            x=F[x][j];
            y=F[y][j];
        }
    ret=min(ret,W[x][0][id]);
    ret=min(ret,W[y][0][id]);
    return ret;
}
void Solve()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        G[i].clear(),fa[i]=i;
    int cnt=0;
    vector<array<int,3>> input(m),edge;
    for(int i=0;i<m;i++)
    {
        auto &[z,x,y]=input[i];
        cin>>x>>y>>z;
    }
    sort(input.begin(),input.end());
    ll Ans=0;
    for(int i=0;i<m;i++)
    {
        auto [z,x,y]=input[i];
        int X=Find(x),Y=Find(y);
        if(X==Y)
        {
            edge.push_back({x,y,z});
            continue;
        }
        G[x].push_back({y,z});
        G[y].push_back({x,z});
        fa[X]=Y;
        Ans+=z;
        cnt++;
    }
    if(cnt<n-1)
    {
        cout<<"-1 -1\n";
        return;
    }
    dep[1]=1;
    Dfs(1,0);
    ll Ans2=10000000000000000;
    for(auto [z,x,y]:edge)
    {
        int ans=Query(x,y,(z%2)^1);
        if(ans>1000000000) continue;
        Ans2=min(Ans2,Ans-ans+z);
    }
    if(Ans2==10000000000000000) Ans2=-1;
    if(Ans%2!=0) swap(Ans,Ans2);
    cout<<Ans<<" "<<Ans2<<"\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--) Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Runtime Error

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:


result: