QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#261340#7069. Farm2986858916WA 146ms16992kbC++203.0kb2023-11-22 20:26:102023-11-22 20:26:10

Judging History

This is the latest submission verdict.

  • [2023-11-22 20:26:10]
  • Judged
  • Verdict: WA
  • Time: 146ms
  • Memory: 16992kb
  • [2023-11-22 20:26:10]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define mid ((l+r)>>1)
#define lowbit(x) x&-x
#define ll long long
const int N=1e4+7,mod=1e9+19;
struct DSU{
    vector<int>p;
    DSU(int n):p(n+1){iota(p.begin(),p.end(),0);}
    const int find(const int x){return p[x]==x?x:p[x]=find(p[x]);}
    bool merge(int x,int y){
        x=find(x),y=find(y);
        if(x==y)return false;
        p[x]=y;
        return true;
    }
};
struct node{
    int u,v,w,id;
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,m;cin>>n>>m;
    vector<node>g(m);
    for(int i=0;i<m;i++){
        cin>>g[i].u>>g[i].v>>g[i].w;
        g[i].id=i;
    }
    DSU dsu(n);
    int q;cin>>q;
    vector<pair<int,int>>t(q);
    for(auto &[u,v]:t){
        cin>>u>>v;
        --u,--v;
        dsu.merge(g[u].u,g[u].v);
        dsu.merge(g[v].u,g[v].v);
    }
    vector<int>cnt;
    sort(g.begin(),g.end(),[&](const auto x,const auto y){
        return x.w<y.w;
    });
    int ans=0;
    for(int i=0;i<m;i++){
        if(dsu.merge(g[i].u,g[i].v)){
            ans+=g[i].w;
            cnt.emplace_back(i);
        }
    }
    dsu=DSU(n);
    for(auto &i:cnt){
        dsu.merge(g[i].u,g[i].v);
    }
    vector<node>p;
    map<pair<int,int>,int>mp;
    vector<int>b;
    for(auto &[u,v,w,id]:g){
        u=dsu.find(u);
        v=dsu.find(v);
        b.emplace_back(u);
        b.emplace_back(v);
        if(u==v)continue;
        if(u>v)swap(u,v);
        if(mp.count({u,v})){
            mp[{u,v}]=min(mp[{u,v}],w);
        }else mp[{u,v}]=w;
    }
    sort(b.begin(),b.end());
    b.erase(unique(b.begin(),b.end()),b.end());
    for(auto &[u,v,w,id]:g){
        u=lower_bound(b.begin(),b.end(),u)-b.begin();
        v=lower_bound(b.begin(),b.end(),v)-b.begin();
    }
    sort(g.begin(),g.end(),[&](const auto x,const auto y){
        return x.id<y.id;
    });
    for(auto &[i,w]:mp){
        auto u=i.first;
        auto v=i.second;
        p.push_back({u,v,w,0});
    }
    for(auto &[u,v,w,id]:p){
        u=lower_bound(b.begin(),b.end(),u)-b.begin();
        v=lower_bound(b.begin(),b.end(),v)-b.begin();
    }
    sort(p.begin(),p.end(),[&](const auto x,const auto y){
        return x.w<y.w;
    });
    if(!q){
        cout<<ans<<'\n';
        return 0;
    }
    int mi=1e9;
    for(int i=0;i<1<<q;i++){
        DSU dp(b.size());
        int cnt=0;
        for(int j=0;j<q;j++){
            if(i>>j&1)dp.merge(g[t[j].first].u,g[t[j].first].v),cnt+=g[t[j].first].w;
            else dp.merge(g[t[j].second].u,g[t[j].second].v),cnt+=g[t[j].second].w;
        }
        for(auto &[u,v,w,id]:p){
            if(dp.merge(u,v))cnt+=w;
        }
        for(int j=0;j<b.size();j++){
            if(dp.find(i)!=dp.find(0)){
                goto next;
            }
        }
        // cout<<i<<' '<<cnt<<'\n';
        mi=min(mi,cnt);
        next:;
    }
    // cout<<ans<<' '<<mi<<'\n';
    if(ans+mi>1e9)cout<<-1<<'\n';
    else cout<<ans+mi<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3452kb

input:

4 6
1 1 2
2 4 3
1 1 4
2 4 4
3 2 4
1 3 4
1
1 2

output:

11

result:

ok single line: '11'

Test #2:

score: -100
Wrong Answer
time: 146ms
memory: 16992kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:

12052953

result:

wrong answer 1st lines differ - expected: '-1', found: '12052953'