QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#539440#8941. Even or Odd Spanning Treeucup-team3699#WA 113ms98100kbC++203.1kb2024-08-31 14:50:292024-08-31 14:50:29

Judging History

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

  • [2024-08-31 14:50:29]
  • 评测
  • 测评结果:WA
  • 用时:113ms
  • 内存:98100kb
  • [2024-08-31 14:50:29]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define F first 
#define S second 

const int mol=998244353;

vector<pair<int, int>>node[200005];
int dsu[200005];
int find(int g){
    if(g==dsu[g]) return g;
    return dsu[g]=find(dsu[g]);
}
void un(int g, int h){
    g=find(g), h=find(h);
    if(g==h) return;
    dsu[g]=h;
}
int p[20][200005], q[20][200005], lcc[20][200005];
int dep[200005];
void dfs(int v, int pre){
    dep[v]=dep[pre]+1;
    lcc[0][v]=pre;
    if(v==pre) p[0][v]=1e18, q[0][v]=1e18;
    for(auto k: node[v]){
        if(k.F==pre) continue;
        q[0][k.F]=1e18, p[0][k.F]=1e18;
        if(k.S%2) p[0][k.F]=k.S;
        else q[0][k.F]=k.S;
        dfs(k.F, v);
    }
}
int lccp(int g, int h){
    if(dep[g]<dep[h]) swap(g, h);
    int t=19, ans=1e18;
    while(t>=0){
        int tt=lcc[t][g];
        if(dep[tt]>=dep[h]) ans=min(ans, p[t][g]), g=tt;
        t--;
    }
    t=19;
    if(g==h) return ans;
    while(t>=0){
        int t1=lcc[t][g], t2=lcc[t][h];
        if(t1!=t2){
            ans=min({ans, p[t][g], p[t][h]}), g=t1, h=t2;
        }
        t--;
    }
    ans=min({ans, p[0][g], p[0][h]});
    return ans;
}
int lccq(int g, int h){
    if(dep[g]<dep[h]) swap(g, h);
    int t=19, ans=1e18;
    while(t>=0){
        int tt=lcc[t][g];
        if(dep[tt]>=dep[h]) ans=min(ans, q[t][g]), g=tt;
        t--;
    }
    t=19;
    if(g==h) return ans;
    while(t>=0){
        int t1=lcc[t][g], t2=lcc[t][h];
        if(t1!=t2){
            ans=min({ans, q[t][g], q[t][h]}), g=t1, h=t2;
        }
        t--;
    }
    ans=min({ans, q[0][g], q[0][h]});
    return ans;
}


void solve(){
    int n, m;
    cin>>n>>m;
    vector<tuple<int, int, int>>mm, nn;
    for(int i=1;i<=n;i++) node[i].clear(), dsu[i]=i;
    dep[1]=0;
    for(int i=1;i<=m;i++){
        int g, h, c;
        cin>>g>>h>>c;
        mm.pb({c, g, h});
    }   
    int ans1=-1, ans2=-1, cc=0, ans=0;
    sort(mm.begin(), mm.end());

    for(auto[t1, t2, t3]: mm){
        int g=t2, h=t3;
        if(find(g)==find(h)) {nn.pb({t1, t2, t3});continue;}
        un(g, h), ans+=t1;
        cc++;
        node[t2].pb({t3, t1});
        node[t3].pb({t2, t1});
    }
    if(cc!=n-1){
        cout<<"-1 -1\n";
        return;
    }
    if(ans%2) ans1=ans;
    else ans2=ans;
    dfs(1, 1);
    for(int i=1;i<=19;i++) for(int j=1;j<=n;j++) lcc[i][j]=lcc[i-1][lcc[i-1][j]], p[i][j]=min(p[i-1][j], p[i-1][lcc[i-1][j]]), q[i][j]=min(q[i-1][j], q[i-1][lcc[i-1][j]]);
    int tot=1e18;
    for(auto [t1, t2, t3]: nn){
        int v;
        if(t1%2) {
            v=lccq(t2, t3);
            if(v==1e18) continue;
            tot=min(tot, t1-v);
        }
        else {
            v=lccp(t2, t3);
            if(v==1e18) continue;
            tot=min(tot, t1-v);
        }
    }
    if(tot!=1e18){
        if(ans2==-1) ans2=ans1+tot;
        else ans1=ans2+tot;
    }
    cout<<ans2<<" "<<ans1<<"\n";
}

signed main(){
    ios::sync_with_stdio(0);cin.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: 97760kb

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: 113ms
memory: 98100kb

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 1332462897
1263932600 1395151561
1189242112 1180186165
2248358640 2277656157
3776889482 3738936375
1093500444 1058677117
3444549292 3402127725
1258609116 1187873655
1395482806 1411327105
3456885934 3486092007
3943951826 3988876469
2479987500 2733874051
2909126794 317...

result:

wrong answer 4th numbers differ - expected: '1309454727', found: '1332462897'