QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#149187#4272. Phone PlansMyrrh0 293ms84476kbC++143.3kb2023-08-24 08:13:122023-08-24 08:13:13

Judging History

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

  • [2023-08-24 08:13:13]
  • 评测
  • 测评结果:0
  • 用时:293ms
  • 内存:84476kb
  • [2023-08-24 08:13:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAXN=4e5+50;

int N,M1,M2;
long long K;

struct Edge
{
	int x,y,Len;
}e1[MAXN],e2[MAXN];
bool cmp(Edge a,Edge b)
{
	return a.Len<b.Len;
}

/*
Mp[i][j]:同时在1的i子树和2的j子树的点数量 
当一个点在另外一个图中跟Mp[i][j]个点联通,那么在这个图中就应该去掉这些贡献

合并:按秩合并
分裂:记录合并时候的Size
每次弹出最后一个
最后要reverse 
*/
map<int,int>Mp[MAXN];

vector<int>Vec1[MAXN],Vec2[MAXN];

int father1[MAXN],father2[MAXN];
int Last[MAXN],Root[MAXN],Size[MAXN];

bool Use[MAXN];


long long Merge(int i)
{
	int fx=father1[e1[i].x],fy=father1[e1[i].y];
	if(fx==fy)
	{
		return 0;
	}
	if(Vec1[fx].size()<Vec1[fy].size())
	{
		swap(fx,fy);
	}
	long long Now=1ll*Vec1[fx].size()*Vec1[fy].size();
	for(int j=0;j<Vec1[fy].size();j++)
	{
		int v=Vec1[fy][j];
		Now-=Mp[fx][father2[v]];
	}
	for(int j=0;j<Vec1[fy].size();j++)
	{
		int v=Vec1[fy][j];
		Vec1[fx].push_back(v);
		father1[v]=fx;
		Mp[fy][father2[v]]--;
		Mp[fx][father2[v]]++;
	}
	return Now;
}
long long Split(int i)
{
	if(Root[i]==0)
	return 0;
	int i1=Root[i],i2=Last[i];
	long long Now=1ll*(Vec2[i1].size()-Vec2[i2].size())*Vec2[i2].size(); 
	for(int j=0;j<Vec2[i2].size();j++)
	{
		int v=Vec2[i2][j];
		Mp[father1[v]][i1]--;
		Mp[father1[v]][i2]++;
		father2[v]=i2;
	}
	for(int j=0;j<Vec2[i2].size();j++)
	{
		int v=Vec2[i2][j];
		Now-=Mp[father1[v]][i1];
	}
	return Now;
}

int main()
{
	scanf("%d%d%d%lld",&N,&M1,&M2,&K); 
	for(int i=1;i<=M1;i++)
	{
		scanf("%d%d%d",&e1[i].x,&e1[i].y,&e1[i].Len); 
	}
	for(int i=1;i<=M2;i++)
	{
		scanf("%d%d%d",&e2[i].x,&e2[i].y,&e2[i].Len); 
	}
	sort(e1+1,e1+M1+1,cmp); 
	sort(e2+1,e2+M2+1,cmp);
	for(int i=1;i<=N;i++)
	{
		father2[i]=i;
		Vec2[i].push_back(i); 
	}
	for(int i=1;i<=M2;i++)
	{
		int fx=father2[e2[i].x],fy=father2[e2[i].y];
		if(fx==fy)
		{
			Root[i]=0;
			continue;
		}
		if(Vec2[fx].size()<Vec2[fy].size())
		{
			swap(fx,fy);
		}
		Root[i]=fx;
		Last[i]=fy;
		Size[i]=Vec2[fy].size();
		for(int j=0;j<Vec2[fy].size();j++)
		{
			Vec2[fx].push_back(Vec2[fy][j]);
			father2[Vec2[fy][j]]=fx;
		}
	}
	
	long long Cnt=0; 
	
	for(int i=1;i<=N;i++)
	{
		Mp[i][father2[i]]++;
		father1[i]=i;
		Vec1[i].push_back(i);
		if(Use[father2[i]]==false)
		{
			Use[father2[i]]=true;
			Cnt+=1ll*Vec2[father2[i]].size()*(Vec2[father2[i]].size()-1)/2;
		}
	}
	int r=M2;
	int ans=2e9+2;
	if(Cnt>=K)
	{
		ans=e2[M2].Len;
	}
	int Now=Split(r);
	while(r&&Cnt-Now>=K)
	{
		Cnt-=Now;
		r--;
		if(r)
		Now=Split(r);
	}
	for(int i=1;i<=M1;i++)
	{
		Cnt+=Merge(i);
		while(r&&Cnt-Now>=K)
		{
			Cnt-=Now;
			r--;
			//cout<<r<<endl;
			if(r)
			Now=Split(r);
		}
		if(Cnt>=K)
		{
			ans=min(ans,e1[i].Len+e2[r].Len);
		}
	}
	if(ans==2e9+2)
	{
		puts("-1");
	}
	else
	{
		printf("%d",ans);
	}
}

/*
stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* i and j are misplaced
	* variable misuse
	* do smth instead of nothing and stay organized
	* vector cause MLE or TLE
	* wrong file name
	* DON'T SLEEP IN THE EXAM
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
	* DON'T GET A SWELLED HEAD
	* YOU MUSTN'T HAVE EMOTION
->be based on Benq's code
*/



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: 42848kb

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
Accepted
time: 9ms
memory: 42752kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 5ms
memory: 42556kb

input:

2 0 0 1

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 3ms
memory: 42312kb

input:

2 10 10 1
1 1 915886339
2 2 430624192
1 1 879808770
1 2 577221915
1 1 439429731
1 2 304911826
1 1 148009333
1 2 51122687
1 1 558282772
1 2 421196385
2 1 436692145
1 2 654020563
2 2 162573477
2 2 989531779
1 1 646687051
2 2 549037477
2 2 699532275
1 1 679375858
2 1 83352965
2 1 415698228

output:

51122687

result:

ok single line: '51122687'

Test #5:

score: 0
Accepted
time: 2ms
memory: 41364kb

input:

2 10 10 1
1 1 1000000000
1 2 1000000000
2 2 1000000000
2 1 1000000000
1 2 1000000000
1 1 1000000000
1 2 1000000000
2 2 1000000000
1 2 1000000000
1 2 1000000000
2 1 1000000000
1 2 1000000000
2 1 1000000000
2 2 1000000000
1 2 1000000000
2 2 1000000000
1 1 1000000000
1 1 1000000000
2 2 1000000000
1 2 1...

output:

1000000000

result:

ok single line: '1000000000'

Test #6:

score: -6
Wrong Answer
time: 50ms
memory: 44936kb

input:

2000 0 200000 1199833
636 1231 120395621
689 1640 497332138
1861 1980 798839749
560 1283 560726905
1332 328 431171189
1203 1764 466367210
1102 347 317109768
1462 789 761470540
350 1093 551905741
1718 1047 548650524
51 546 56733461
58 1519 744207013
826 956 943929583
1969 207 238061756
99 47 99683567...

output:

999997028

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #53:

score: 0
Wrong Answer
time: 293ms
memory: 84476kb

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:

634185692

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 6
Accepted
time: 9ms
memory: 46732kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #104:

score: 0
Accepted
time: 4ms
memory: 46848kb

input:

2 0 0 1

output:

-1

result:

ok single line: '-1'

Test #105:

score: -6
Wrong Answer
time: 49ms
memory: 55104kb

input:

2 10 200000 1
2 1 319832429
1 1 412617159
2 1 337734185
1 2 674652880
1 2 454610992
2 2 688717944
1 1 189208056
2 2 951221780
1 2 594191431
2 2 87592160
1 2 833491749
2 2 732961971
2 1 575969648
2 2 268359887
2 1 436382098
1 2 514959278
1 2 305446083
1 2 365989813
1 2 296840111
1 1 541558213
1 1 497...

output:

87602264

result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

0%