QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203828#7069. FarmqzezWA 133ms12908kbC++142.1kb2023-10-06 21:02:172023-10-06 21:02:17

Judging History

This is the latest submission verdict.

  • [2023-10-06 21:02:17]
  • Judged
  • Verdict: WA
  • Time: 133ms
  • Memory: 12908kb
  • [2023-10-06 21:02:17]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=1e5+10,M=5e5+10,K=40,INF=1e9;
int n,m,q,cnt;
struct edges{
	int u,v,w;
}e[M],t[K*K];
struct opts{
	int x,y;
}o[K];
int k,fa[N],id[N],cur[M];
bool cmp(int x,int y){
	return e[x].w<e[y].w;
}
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return 0;
	else return fa[x]=y,1;
}
int main(){
	scanf("%d%d",&n,&m);
	iota(fa,fa+1+n,0);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		merge(e[i].u,e[i].v);
	}
	for(int i=2;i<=n;i++){
		if(find(i)^find(1))puts("-1"),exit(0);
	}
	iota(fa,fa+1+n,0);
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d%d",&o[i].x,&o[i].y);
		merge(e[o[i].x].u,e[o[i].x].v);
		merge(e[o[i].y].u,e[o[i].y].v);
	}
	iota(cur,cur+1+m,0);
	sort(cur+1,cur+1+m,cmp);
	int sum=0;
	vector<pair<int,int> >E;
	for(int x=1,i;i=cur[x],x<=m;x++){
		if(merge(e[i].u,e[i].v)){
			sum+=e[i].w;
			E.push_back({e[i].u,e[i].v});
		}
	}
	iota(fa,fa+1+n,0);
	for(auto et:E)merge(et.first,et.second);
	for(int i=1;i<=n;i++){
		if(!id[find(i)])id[find(i)]=++k;
		id[i]=id[find(i)];
	}
	for(int x=1,i;i=cur[x],x<=m;x++){
		if(merge(e[i].u,e[i].v)){
			t[++cnt]=e[i];
		}
	}
	int ans=INF;
	// for(int i=1;i<=cnt;i++)debug(t[i].u,t[i].v);
	for(int S=0;S<(1<<q);S++){
		int res=sum*(n>1);
		iota(fa,fa+1+k,0);
		for(int i=0;i<q;i++){
			int x=S>>i&1?o[i+1].x:o[i+1].y;
			res+=e[x].w,merge(id[e[x].u],id[e[x].v]);
		}
		if(n>1)for(int i=1;i<=cnt;i++){
			if(merge(id[t[i].u],id[t[i].v])){
				res+=t[i].w;
			}
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 6016kb

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: 74ms
memory: 10580kb

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: 133ms
memory: 12908kb

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: -100
Wrong Answer
time: 100ms
memory: 12248kb

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:

3212

result:

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