QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#261360#7069. Farm2986858916WA 144ms17728kbC++203.1kb2023-11-22 20:32:432023-11-22 20:32:44

Judging History

This is the latest submission verdict.

  • [2023-11-22 20:32:44]
  • Judged
  • Verdict: WA
  • Time: 144ms
  • Memory: 17728kb
  • [2023-11-22 20:32:43]
  • 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;
    int ex=cnt.size();
    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;
        }
        int px=0;
        for(auto &[u,v,w,id]:p){
            if(dp.merge(u,v))cnt+=w,px++;
        }
        for(int j=0;j<b.size();j++){
            if(dp.find(i)!=dp.find(0)||px+q+ex!=n-1){
                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';
}

詳細信息

Test #1:

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

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: 144ms
memory: 17728kb

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'