QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#711435#4272. Phone Plansshuopihua0 164ms97368kbC++142.1kb2024-11-05 10:55:392024-11-05 10:55:40

Judging History

你现在查看的是最新测评结果

  • [2024-11-05 10:55:40]
  • 评测
  • 测评结果:0
  • 用时:164ms
  • 内存:97368kb
  • [2024-11-05 10:55:39]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <unordered_map>
#define int long long
using namespace std;

int n,A,B,K,m;
int fa[200005];
int ww[200005];
int ss[200005];
int bl[200005];
vector <int> g[200005];
vector <int> d[200005];
vector <int> p[200005];
struct node{int u,v,w;};
node a[200005];
node b[200005];
unordered_map <int,int> mp[200005];

inline int Find(int x){return fa[x]==x?x:fa[x]=Find(fa[x]);}

inline void in(int &n){
	n=0;
	char c=getchar();
	while(c<'0' || c>'9') c=getchar();
	while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
	return ;
}

signed main(){
	in(n),in(A),in(B),in(K);
	for(int i=1;i<=A;i++) in(a[i].u),in(a[i].v),in(a[i].w);
	for(int i=1;i<=B;i++) in(b[i].u),in(b[i].v),in(b[i].w);
	sort(a+1,a+1+A,[](node p,node q){return p.w<q.w;});
	sort(b+1,b+1+B,[](node p,node q){return p.w<q.w;});
	for(int i=1;i<=n;i++) fa[i]=i,g[i].emplace_back(i);
	for(int i=1;i<=B;i++){
		int r=i;
		while(r<=B&&b[i].w==b[r].w) r++;
		r--;
		ww[++m]=b[i].w;
		ss[m]=ss[m-1];
		while(i<=r){
			int u=Find(b[i].u),v=Find(b[i].v);
			i++;
			if(u==v) continue;
			ss[m]+=1ll*g[v].size()*g[u].size();
			if(g[v].size()>g[u].size()) swap(u,v);
			for(int vv:g[v]) g[u].emplace_back(vv);
			fa[v]=u;
			p[m].emplace_back(v);
		}
		i--;
	}
	for(int i=1;i<=n;i++) bl[i]=Find(i);
	for(int i=1;i<=n;i++) fa[i]=i,d[i].emplace_back(i),mp[i][bl[i]]=1;
	a[A+1].w=1e15;
	int ans=1e18,X=0,Y=0,pos=1;
	for(int i=m;i>=1;i--){
		while(pos<=A&&X+ss[i]-Y<K){
			int u=Find(a[pos].u),v=Find(a[pos].v);
			pos++;
			if(u==v) continue;
			if(d[v].size()>d[u].size()) swap(u,v);
			X+=1ll*d[v].size()*d[u].size();
			for(int vv:d[v]){
				mp[v][bl[vv]]--;
				Y-=mp[v][bl[vv]];
				Y+=mp[u][bl[vv]];
				mp[u][bl[vv]]++;
				d[u].emplace_back(vv);
			}
			fa[v]=u;
		}
		if(X+ss[i]-Y>=K) ans=min(ans,ww[i]+a[pos-1].w);
		else break;
		for(int v:p[i]){
			for(int vv:g[v]){
				mp[Find(vv)][bl[vv]]--;
				Y-=mp[Find(vv)][bl[vv]];
				bl[vv]=v;
				Y+=mp[Find(vv)][bl[vv]];
				mp[Find(vv)][bl[vv]]++;
			}
		}
	}
	printf("%lld\n",ans>2e9?-1:ans);

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 6
Accepted
time: 3ms
memory: 36512kb

input:

6 4 4 9
1 2 1
2 3 2
1 4 3
3 4 4
5 6 40
1 5 30
2 6 20
3 6 10

output:

33

result:

ok single line: '33'

Test #2:

score: 0
Wrong Answer
time: 4ms
memory: 35468kb

input:

1 0 0 0

output:

-1

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #53:

score: 5
Accepted
time: 164ms
memory: 97368kb

input:

200000 100000 100000 7628995
23677 113459 839167193
165893 15365 669621527
26287 109671 214795757
156871 136723 522277985
9957 100463 806718116
104783 161385 156110975
184577 92225 545172629
57373 130083 980035338
167231 49597 919886451
115601 24325 717233004
99413 141311 737488449
83437 62759 76873...

output:

502149991

result:

ok single line: '502149991'

Test #54:

score: 5
Accepted
time: 45ms
memory: 82020kb

input:

200000 200000 87 2694
197233 86229 181875035
85167 196363 328068482
177795 65479 693403443
119609 181977 588691030
115815 139981 486110961
92473 20483 199129328
166989 95385 210368646
98095 54673 357307451
122543 94377 907487846
46611 80735 71787832
158893 117071 521262491
92051 104395 365725125
142...

output:

11965880

result:

ok single line: '11965880'

Test #55:

score: 5
Accepted
time: 162ms
memory: 93420kb

input:

200000 103 199999 1593
75203 150269 64
68675 175215 100
176335 11837 94
33623 63279 56
16617 86741 63
167219 52783 58
6575 134399 1
144065 114171 2
32625 99459 50
35311 152509 36
132975 12211 8
175275 107903 23
17477 21319 95
157759 66683 71
34577 78339 26
154003 26703 18
187863 90945 74
116071 1089...

output:

7037526

result:

ok single line: '7037526'

Test #56:

score: 0
Wrong Answer
time: 0ms
memory: 35880kb

input:

1 0 0 0

output:

-1

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 0
Wrong Answer
time: 3ms
memory: 37020kb

input:

1 0 0 0

output:

-1

result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

0%