QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#255088#7069. Farmhl666WA 107ms26304kbC++172.0kb2023-11-18 14:44:382023-11-18 14:44:38

Judging History

This is the latest submission verdict.

  • [2023-11-18 14:44:38]
  • Judged
  • Verdict: WA
  • Time: 107ms
  • Memory: 26304kb
  • [2023-11-18 14:44:38]
  • Submitted

answer

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#define RI register int
#define CI const int&
using namespace std;
const int N=500005;
struct edge
{
	int x,y,z;
	friend inline bool operator < (const edge& A,const edge& B)
	{
		return A.z<B.z;
	}
}; int n,m,q,ans,x[N],y[N],z[N],u[N],v[N],sp[N],fa[N],bel[N],rst[N],vis[N];
inline int getfa(CI x)
{
	return fa[x]!=x?fa[x]=getfa(fa[x]):x;
}
inline void merge(CI x,CI y)
{
	fa[getfa(x)]=getfa(y);
}
int main()
{
	//freopen("D.in","r",stdin); freopen("D.out","w",stdout);
	RI i,j; for (scanf("%d%d",&n,&m),i=1;i<=m;++i)
	scanf("%d%d%d",&x[i],&y[i],&z[i]);
	for (i=1;i<=n;++i) fa[i]=i;
	for (i=1;i<=m;++i) merge(x[i],y[i]);
	for (i=2;i<=n;++i) if (getfa(i)!=getfa(1)) return puts("-1"),0;
	for (scanf("%d",&q),i=0;i<q;++i)
	scanf("%d%d",&u[i],&v[i]),sp[u[i]]=sp[v[i]]=1;
	for (i=1;i<=n;++i) fa[i]=i; vector <edge> E;
	for (i=1;i<=m;++i) if (sp[i]) merge(x[i],y[i]);
	else E.push_back((edge){x[i],y[i],z[i]});
	sort(E.begin(),E.end()); vector <edge> cs;
	for (auto [x,y,z]:E)
	{
		if (getfa(x)==getfa(y)) continue;
		cs.push_back((edge){x,y,z}); merge(x,y); ans+=z;
	}
	for (i=1;i<=n;++i) fa[i]=i;
	for (auto [x,y,z]:cs) merge(x,y);
	int idx=0; for (i=1;i<=n;++i)
	{
		int tmp=getfa(i); if (!rst[tmp]) rst[tmp]=++idx; bel[i]=rst[tmp];
	}
	int ret=1e9; for (i=0;i<(1<<q);++i)
	{
		for (j=1;j<=idx;++j) fa[j]=j; int cur=0;
		for (j=0;j<q;++j) vis[u[j]]=vis[v[j]]=0;
		vector <edge> left; for (j=0;j<q;++j)
		if ((i>>j)&1)
		{
			if (!vis[v[j]]) cur+=z[v[j]],vis[v[j]]=1; merge(bel[x[v[j]]],bel[y[v[j]]]);
			left.push_back((edge){bel[x[u[j]]],bel[y[u[j]]],z[u[j]]});
		} else
		{
			if (!vis[u[j]]) cur+=z[u[j]],vis[u[j]]=1; merge(bel[x[u[j]]],bel[y[u[j]]]);
			left.push_back((edge){bel[x[v[j]]],bel[y[v[j]]],z[v[j]]});
		}
		sort(left.begin(),left.end());
		for (auto [x,y,z]:left)
		{
			if (getfa(x)==getfa(y)) continue; merge(x,y); cur+=z;
		}
		ret=min(ret,cur);
	}
	return printf("%d",ans+ret),0;
}

詳細信息

Test #1:

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

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: 75ms
memory: 12568kb

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: 105ms
memory: 23384kb

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: 89ms
memory: 26304kb

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

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:

3219

result:

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