UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203640#3563. 最大生成树snow_trace1001661ms14948kbC++112.6kb2024-02-29 09:40:512024-02-29 13:01:08

answer

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(3)
//#define int long long
int n,k;
long long x[100005][6];
int fa[100005];
int find(int x){
	if(fa[x] == x)return x;
	return fa[x] = find(fa[x]);
}void merge(int x,int y){
	fa[find(x)] = find(y);
}long long mx[100005];int mxpos[100005];
int col[100005],ok[100005];vector<int>c[100005];int cnt =0 ;
void Bhoumianwangle(){
	long long mxx1[55],mxx2[55],mxxpos1[55],mxxpos2[55];
	for(int i =0;i<=(1<<k);i++)mxx1[i] = mxx2[i] = -1000000000000;
	cnt =0 ;
	for(int i =0;i<=n;i++)col[i] = ok[i] = 0,mx[i] = 0,c[i].clear();
	for(int i =1;i<=n;i++){
		if(!ok[find(i)])ok[find(i)] = ++cnt;
		col[i] = ok[find(i)];c[col[i]].push_back(i);
	}for(int i =1;i<=n;i++){
		for(int j=0;j<(1<<k);j++){
			long long sum = 0;
			for(int l = 0;l<k;l++){
				if(j>>l&1)sum = sum+x[i][l];
				else sum = sum-x[i][l];
			}if(sum>=mxx1[j]){
				mxx1[j] = sum;mxxpos1[j] = i;
			}
		}
	}for(int i = 1;i<=n;i++){
		for(int j =0;j<(1<<k);j++){
			long long sum = 0;
			for(int l =0;l<k;l++){
				if(j>>l&1)sum = sum+x[i][l];
				else sum = sum-x[i][l];
			}if(sum>=mxx2[j] and col[i]!=col[mxxpos1[j]]){
				mxx2[j] = sum;mxxpos2[j] = i;
			}
		}
	}for(int i = 1;i<=cnt;i++){
		for(int xx:c[i]){
			for(int j =0;j<(1<<k);j++){
				long long sum =0 ;
				for(int l =0;l<k;l++){
					if(j>>l&1)sum = sum-x[xx][l];
					else sum = sum+x[xx][l];
				}if(col[mxxpos1[j]] == i){
					if(sum+mxx2[j]>=mx[i]){
						mx[i] = sum+mxx2[j];mxpos[i] = mxxpos2[j];
					}
				}else{
					if(sum+mxx1[j]>=mx[i]){
						mx[i] = sum+mxx1[j];mxpos[i] = mxxpos1[j];
					}
				}
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >>n >> k;
	for(int i =1;i<=n;i++){
		for(int j =0;j<k;j++)cin >> x[i][j];
	}for(int i = 1;i<=n;i++)fa[i] = i;long long sum =0 ;
	while(1){
		Bhoumianwangle();
		for(int i = 1;i<=cnt;i++){
			if(find(c[i].back()) !=find(mxpos[i]))sum+=mx[i];
			merge(c[i].back(),mxpos[i]);
		}int ok = 0;
		for(int i= 1;i<=n;i++)if(fa[i] == i)ok++;
		//for(int i = 1;i<=cnt;i++)cout << mx[i] << " " << mxpos[i] << endl;
		if(ok == 1)break;
	}cout << sum << endl;
	return 0;
}
/*
考虑使用 B后面忘了 生成树算法。
问题变成求到一个点最远的点。
由于绝对值的性质,把所有大小关系枚举一遍显然很对。
复杂度 O(2^kn\log^2 n) 
这题挺有意思。 

牛魔,居然要支持删数,这复杂度看着不大好啊。 
牛魔,不用支持删数,维护次大值就可以了。
写题五分钟,卡常一小时。 
*/

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 1ms
memory: 3644kb

input:

16 3
953426878 284054286 838711178
668499263 -224496686 -499321118
53682534 172847348 -741761953
-37...

output:

49588152186

result:

ok single line: '49588152186'

Test #2:

score: 5
Accepted
time: 1ms
memory: 3644kb

input:

16 5
529897563 42718560 -764294904 546345843 -131064377
-464339380 -913723519 628560030 154631027 25...

output:

71232810493

result:

ok single line: '71232810493'

Test #3:

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

input:

16 5
225834194 -621694937 198912695 61859803 -275724766
287589126 863297652 -126251336 -446563133 84...

output:

70463691200

result:

ok single line: '70463691200'

Test #4:

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

input:

16 3
675758216 925664673 -100467753
-255044844 -119320564 -348819717
104489563 624402309 621346091
7...

output:

45574278940

result:

ok single line: '45574278940'

Test #5:

score: 5
Accepted
time: 2ms
memory: 3868kb

input:

2000 3
101319809 -23603507 546078402
375224051 279401121 -472811178
-969206443 864857136 405994073
-...

output:

8550423308185

result:

ok single line: '8550423308185'

Test #6:

score: 5
Accepted
time: 4ms
memory: 3884kb

input:

2000 4
-484778857 -615354594 133978805 172344089
675220875 275063317 -997782885 856068164
570760530 ...

output:

10870195379896

result:

ok single line: '10870195379896'

Test #7:

score: 5
Accepted
time: 3ms
memory: 3868kb

input:

2000 3
-354104546 610541166 61483675
696986274 207824440 292659920
-611661771 395152481 756286059
-9...

output:

8559275453639

result:

ok single line: '8559275453639'

Test #8:

score: 5
Accepted
time: 3ms
memory: 3880kb

input:

2000 5
966093478 -222359543 173876502 525342202 -365543374
898040767 -660210505 733626218 -870086230...

output:

13100825958903

result:

ok single line: '13100825958903'

Test #9:

score: 5
Accepted
time: 7ms
memory: 3872kb

input:

2000 5
-52237322 -19466788 -924090413 -297261236 -18045574
831291316 940625731 -67474115 -253926074 ...

output:

12934762781492

result:

ok single line: '12934762781492'

Test #10:

score: 5
Accepted
time: 4ms
memory: 3872kb

input:

2000 5
-18204930 -137807275 983978274 -114214924 701701812
-771383102 752985230 715153447 -737257726...

output:

13071831165291

result:

ok single line: '13071831165291'

Test #11:

score: 5
Accepted
time: 12ms
memory: 13764kb

input:

100000 1
-858072701
-959717109
944301573
8592817
565934230
66303209
-920672756
636118541
37167333
73...

output:

149959435811831

result:

ok single line: '149959435811831'

Test #12:

score: 5
Accepted
time: 21ms
memory: 13760kb

input:

100000 1
-634013498
440958380
736196013
950185571
100133154
263742865
626594999
297364348
-390787569...

output:

149998851497510

result:

ok single line: '149998851497510'

Test #13:

score: 5
Accepted
time: 32ms
memory: 14300kb

input:

100000 2
616484717 -218883820
536683727 -592361183
412299173 13246418
-693428127 -424485654
76654342...

output:

299201628231431

result:

ok single line: '299201628231431'

Test #14:

score: 5
Accepted
time: 41ms
memory: 14260kb

input:

100000 2
-231183420 -378090485
328624250 135605118
-856104723 -297979528
545394926 -966624173
117263...

output:

299382013637474

result:

ok single line: '299382013637474'

Test #15:

score: 5
Accepted
time: 156ms
memory: 14948kb

input:

100000 4
471608796 -775067240 953624653 319464968
-314055874 -215087101 385004262 651284620
92260848...

output:

579124678038141

result:

ok single line: '579124678038141'

Test #16:

score: 5
Accepted
time: 283ms
memory: 14652kb

input:

100000 5
133088008 -625818358 580219437 43098825 -316105521
235451619 562584437 303605408 145357481 ...

output:

706431825137853

result:

ok single line: '706431825137853'

Test #17:

score: 5
Accepted
time: 281ms
memory: 14788kb

input:

100000 5
166369337 -206548158 -433644757 290540289 178032746
-895124105 -847288875 -294639705 624198...

output:

707341802314105

result:

ok single line: '707341802314105'

Test #18:

score: 5
Accepted
time: 258ms
memory: 14736kb

input:

100000 5
-717013989 -822017275 534756773 907064833 -680422704
-271305944 -227110360 -671423139 -5923...

output:

703654594430108

result:

ok single line: '703654594430108'

Test #19:

score: 5
Accepted
time: 279ms
memory: 14788kb

input:

100000 5
472023095 120730360 677466207 689979178 -310652220
-435574711 897762285 883858915 -30436199...

output:

707007494019674

result:

ok single line: '707007494019674'

Test #20:

score: 5
Accepted
time: 273ms
memory: 14792kb

input:

100000 5
-181841793 -852953085 272276165 528776289 620825974
-245365795 -413151026 296716527 -701182...

output:

701964080014126

result:

ok single line: '701964080014126'

Extra Test:

score: 0
Extra Test Passed