QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#261409 | #7069. Farm | 2986858916 | RE | 157ms | 12492kb | C++20 | 3.5kb | 2023-11-22 20:59:55 | 2023-11-22 20:59:57 |
Judging History
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);
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;
}
for(int i=1;i<=n;i++)b.emplace_back(dsu.find(i));
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){
if(b.size()==1)cout<<ans<<'\n';
else cout<<-1<<'\n';
return 0;
}
int mi=1e9;
vector<bool>use(n+1);
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){
if(use[t[j].first])continue;
use[t[j].first]=1;
dp.merge(g[t[j].first].u,g[t[j].first].v),cnt+=g[t[j].first].w;
}else{
if(use[t[j].second])continue;
use[t[j].second]=1;
dp.merge(g[t[j].second].u,g[t[j].second].v),cnt+=g[t[j].second].w;
}
}
for(int j=0;j<q;j++){
if(i>>j&1)use[t[j].first]=0;
else use[t[j].second]=0;
}
int px=0;
for(auto &[u,v,w,id]:p){
if(dp.merge(u,v))cnt+=w,px++;
}
// cout<<px<<' '<<ex<<' '<<q<<'\n';
for(int j=0;j<b.size();j++){
if(dp.find(j)!=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';
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3436kb
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: 0
Accepted
time: 125ms
memory: 12476kb
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:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 140ms
memory: 12492kb
input:
100000 500000 34497 87538 658 69862 2776 861 93620 16992 904 77910 81200 149 83935 83752 880 17602 75791 259 85887 53289 710 4200 79358 181 8518 19264 737 94665 47462 822 50632 51994 143 55224 59127 656 615 92858 150 48450 9465 58 35713 45287 140 64861 32248 517 70296 45113 153 11189 90316 809 40673...
output:
12148224
result:
ok single line: '12148224'
Test #4:
score: 0
Accepted
time: 116ms
memory: 11024kb
input:
1 500000 1 1 963 1 1 349 1 1 157 1 1 6 1 1 312 1 1 377 1 1 783 1 1 42 1 1 18 1 1 327 1 1 499 1 1 824 1 1 343 1 1 798 1 1 193 1 1 667 1 1 378 1 1 641 1 1 692 1 1 622 1 1 584 1 1 590 1 1 324 1 1 858 1 1 914 1 1 601 1 1 734 1 1 61 1 1 559 1 1 681 1 1 825 1 1 888 1 1 585 1 1 55 1 1 818 1 1 190 1 1 278 1...
output:
1605
result:
ok single line: '1605'
Test #5:
score: 0
Accepted
time: 141ms
memory: 10944kb
input:
5 500000 5 1 817 2 1 273 3 5 674 1 5 15 5 2 872 3 4 728 3 2 807 5 3 28 2 5 96 1 5 100 4 2 224 4 4 980 5 5 727 2 2 520 4 1 29 2 1 142 4 2 963 4 4 118 4 4 615 4 3 719 5 3 200 5 2 746 4 2 68 5 4 859 1 3 182 3 4 286 3 1 229 4 1 895 2 1 730 1 2 622 2 4 913 2 1 697 5 5 130 4 5 507 5 2 425 2 4 716 2 1 884 ...
output:
3097
result:
ok single line: '3097'
Test #6:
score: 0
Accepted
time: 157ms
memory: 10960kb
input:
10 500000 3 8 138 10 7 593 4 3 8 7 5 516 10 4 49 3 8 601 6 7 481 8 5 429 6 4 241 1 6 504 6 2 252 7 1 656 5 1 350 5 9 485 7 8 669 5 8 630 9 9 324 1 3 427 1 2 309 5 10 236 4 6 926 8 7 34 5 1 336 7 5 581 4 5 228 10 3 909 2 9 726 4 2 444 10 1 55 1 2 244 5 8 261 2 7 556 10 2 165 6 3 657 7 5 580 7 1 827 1...
output:
1533
result:
ok single line: '1533'
Test #7:
score: -100
Runtime Error
input:
100 500000 10 46 133 79 13 987 26 2 743 8 47 390 79 19 737 11 64 197 16 65 207 73 9 944 77 58 841 50 3 245 81 100 293 21 12 713 60 65 155 89 87 865 88 67 278 9 15 920 46 52 704 26 26 731 44 98 525 20 68 346 14 95 932 84 19 697 41 21 290 83 24 750 3 71 369 54 80 396 20 70 208 25 55 456 40 22 938 90 1...