QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820656#8941. Even or Odd Spanning TreeLinver#WA 92ms3564kbC++232.1kb2024-12-18 22:39:162024-12-18 22:39:16

Judging History

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

  • [2024-12-18 22:39:16]
  • 评测
  • 测评结果:WA
  • 用时:92ms
  • 内存:3564kb
  • [2024-12-18 22:39:16]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int N=4e5+10,M=1e5+10;
const int INF=1e18;
const int mod=998244353;
// const int mod=1e9+7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); 
struct DSU {
    std::vector<int> f, siz;
    
    DSU() {}
    DSU(int n) {
        init(n);
    }
    
    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }
    
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) {
        return siz[find(x)];
    }
};
struct Edge{
    int u,v,w,op;
};
bool cmp(const Edge& x,const Edge &y){
    return x.w<y.w;
}
void solve(){
    int n,m;
    cin>>n>>m;
    vector<Edge> e;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        e.push_back({u,v,w});
    }
    sort(e.begin(),e.end(),cmp);
    DSU dsu(n+1);
    int ans=0,odd=INF,even=INF;
    for(auto &[u,v,w,op]:e){
        if(dsu.same(u,v))continue;
        dsu.merge(u,v);
        op=1;
        ans+=w;
        if(w&1)odd=min(odd,w);
        else even=min(even,w);
    }
    for(int i=1;i<=n;i++)dsu.find(i);
    if(dsu.size(1)!=n){
        cout<<"-1 -1\n";
        return;
    }
    vector<int> W;
    for(auto [u,v,w,op]:e){
        if(!op)W.push_back(w);
    }
    sort(W.begin(),W.end());
    int res=INF;
    for(auto &x:W){
        if(x%2==1&&even!=INF)res=min(res,ans-even+x);
        if(x%2==0&&odd!=INF)res=min(res,ans-odd+x);
    }
    if(ans&1)swap(ans,res);
    cout<<(ans==INF?-1:ans)<<" ";
    cout<<(res==INF?-1:res)<<"\n";
}
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int T=1;
    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: 3564kb

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: 92ms
memory: 3548kb

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 1334624821
1263932600 1565384431
1211478768 1180186165
2248358640 2475857269
4039559282 3738936375
1181933712 1058677117
3585912752 3402127725
1244594022 1187873655
1395482806 1440596255
3456885934 3600986359
3943951826 4089919585
2479987500 2559672535
2909126794 306...

result:

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