QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#799855#8253. Very Important EdgeYarema#AC ✓1793ms40204kbC++203.6kb2024-12-05 18:54:572024-12-05 18:54:57

Judging History

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

  • [2024-12-05 18:54:57]
  • 评测
  • 测评结果:AC
  • 用时:1793ms
  • 内存:40204kb
  • [2024-12-05 18:54:57]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

struct DSU
{
	int n;
	VI p, sz;
	
	DSU(int _n = 0) 
	{
		n = _n;
		p.resize(n);
		iota(ALL(p), 0);
		sz.assign(n, 1);
	}
	int find(int v)
	{
		if (v == p[v])
			return v;
		return p[v] = find(p[v]);
	}
	bool unite(int u, int v)
	{
		u = find(u);
		v = find(v);
		if (u == v) 
			return false;
		if (sz[u] > sz[v])
			swap(u, v);
		p[u] = v;
		sz[v] += sz[u];
		return true;
	}
};

const int INF = 1e9 + 7;

struct Segtree
{
	int n;
	VI t;
	
	Segtree(int _n = 0)
	{
		n = 1;
		while (n < _n) n *= 2;
		t.assign(n * 2 - 1, INF);
	}
	
	void upd(int v, int tl, int tr, int l, int r, int x)
	{
		if (l <= tl && tr <= r)
		{
			t[v] = min(t[v], x);
			return;
		}
		if (tl >= r || l >= tr)
			return;
		int tm = (tl + tr) / 2;
		upd(2 * v + 1, tl, tm, l, r, x);
		upd(2 * v + 2, tm, tr, l, r, x);
	}
	
	void upd(int l, int r, int x)
	{
		upd(0, 0, n, l, r, x);
	}

	int query(int v, int tl, int tr, int idx)
	{
		if (tl + 1 == tr)
			return t[v];
		int tm = (tl + tr) / 2;
		if (idx < tm)
			return min(t[v], query(v * 2 + 1, tl, tm, idx));
		else
			return min(t[v], query(v * 2 + 2, tm, tr, idx));
	}

	int query(int idx)
	{
		return query(0, 0, n, idx);
	}
} st;

const int N = 100'447;
VI g[N];     
vector<PII> g2[N];     
int sz[N];   
int h[N];    
int p[N];    
int top[N];  
int tin[N];
int tout[N];
int t = 0;

void dfsSZ(int v, int par, int hei)
{
	sz[v] = 1;
	h[v] = hei;
	p[v] = par;
	for (auto& to : g[v])
	{
		if (to == par) 
			continue;
		dfsSZ(to, v, hei + 1);
		sz[v] += sz[to];
		if (g[v][0] == par || sz[g[v][0]] < sz[to])
			swap(g[v][0], to);
	}
}
void dfsHLD(int v, int par, int tp)
{
	tin[v] = t++;
	top[v] = tp;
	FOR (i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		if (to == par)
			continue;
		if (i == 0) 
			dfsHLD(to, v, tp);
		else 
			dfsHLD(to, v, to);
	}
	tout[v] = t - 1;
}
void upd(int u, int v, int w)
{
	while(true)
	{
		int tu = top[u];
		int tv = top[v];
		if (tu == tv)
		{
			int t1 = tin[u];
			int t2 = tin[v];
			if (t1 > t2)
				swap(t1, t2);
			// query [t1, t2] both inclusive
			st.upd(t1 + 1, t2 + 1, w);
			break;
		}
		if (h[tu] < h[tv])
		{
			swap(tu, tv);
			swap(u, v);
		}
		st.upd(tin[tu], tin[u] + 1, w);
		u = p[tu];
	}
}

int add = 0;

void dfs(int v, int par, int w)
{
	if (v)
		add = max(add, st.query(tin[v]) - w);
	for (auto [to, c] : g2[v])
		if (to != par)
			dfs(to, v, c);
}


int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);


	int n, m;
	cin >> n >> m;
	
	vector<pair<int, PII>> edges;
	VI used(m);
	FOR (i, 0, m)
	{
		int a, b, c;
		cin >> a >> b >> c;
		a--, b--;
		edges.PB({c, {a, b}});
	}
	sort(ALL(edges));
	LL ans = 0;
	DSU d(n);
	FOR (i, 0, m)
	{
		auto [w, e] = edges[i];
		if (d.find(e.F) != d.find(e.S))
		{
			used[i] = true;
			d.unite(e.F, e.S);
			ans += w;
			g[e.F].PB(e.S);
			g[e.S].PB(e.F);
			g2[e.F].PB({e.S, w});
			g2[e.S].PB({e.F, w});
		}
	}
	dfsSZ(0, -1, 0);
	dfsHLD(0, -1, 0);
	st = Segtree(t);
	FOR (i, 0, m)
	{
		auto [w, e] = edges[i];
		if (used[i])
			continue;
		upd(e.F, e.S, w);
	}
	dfs(0, -1, 0);
	cout << ans + add << '\n';
		
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 235ms
memory: 12904kb

input:

1000 499500
26 715 732723
850 918 507404
276 643 372190
67 702 972051
397 777 337003
178 185 863117
61 151 943008
83 581 731622
99 501 3260
301 588 948638
17 908 147104
193 480 94512
347 415 416562
519 912 627431
70 959 86892
333 573 757685
129 197 181261
224 636 259176
335 426 567962
193 339 70097
...

output:

1121154

result:

ok single line: '1121154'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3904kb

input:

3 3
1 2 1
2 3 2
1 3 2

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

4 5
2 3 5
1 2 2
1 3 4
1 4 2
3 4 3

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5952kb

input:

5 7
2 5 8
1 3 19
4 5 9
1 5 15
1 2 14
3 4 16
2 4 15

output:

54

result:

ok single line: '54'

Test #5:

score: 0
Accepted
time: 232ms
memory: 13140kb

input:

1000 499500
857 965 96030
394 688 78612
440 754 495692
48 297 76650
206 975 200873
790 854 325696
307 384 472048
94 958 278751
601 806 136479
880 988 278407
265 813 315222
114 470 208185
172 332 425333
504 669 12025
266 315 400789
61 894 120668
675 972 228141
604 855 494399
116 496 234186
116 888 21...

output:

627123

result:

ok single line: '627123'

Test #6:

score: 0
Accepted
time: 6ms
memory: 4160kb

input:

1000 10000
894 939 776049
780 873 504700
126 161 227849
221 391 589404
562 661 697593
8 495 975484
13 527 481938
711 908 92209
147 616 682518
117 849 292092
231 463 868315
296 372 308904
458 886 970257
44 415 858179
2 352 402538
340 431 276296
87 744 48795
30 146 526326
35 109 788908
551 742 146887
...

output:

64112840

result:

ok single line: '64112840'

Test #7:

score: 0
Accepted
time: 485ms
memory: 39012kb

input:

100000 1000000
3496 42038 2
23760 54893 2
40179 73909 2
18246 58964 2
59829 97488 2
46535 89743 2
43598 88684 2
10025 15117 2
25372 39316 2
55988 99623 2
12242 94927 2
91339 99004 2
68898 82761 2
19117 49957 2
24435 85140 2
15302 78102 2
9038 89450 2
82914 88120 2
6227 67500 2
5298 26787 2
27614 518...

output:

100000

result:

ok single line: '100000'

Test #8:

score: 0
Accepted
time: 109ms
memory: 24204kb

input:

100000 150000
10397 97917 1
81023 97767 2
27616 48830 2
17714 90000 2
34151 34285 1
6030 96304 1
50786 80688 1
30193 63737 1
25856 97783 1
735 46702 2
24633 79153 1
27172 85261 1
18963 27646 1
68548 74906 1
45452 65265 2
20050 96249 1
42929 94752 1
30314 52715 2
17457 32389 1
79882 95851 1
20932 436...

output:

100000

result:

ok single line: '100000'

Test #9:

score: 0
Accepted
time: 78ms
memory: 8236kb

input:

1000 249502
53 877 1
475 560 1
559 895 1
672 838 1
68 950 1
105 805 1
636 879 1
597 991 1
601 738 1
26 194 1
169 920 1
47 787 1
470 882 1
253 734 1
454 803 1
302 998 1
523 678 1
191 415 1
279 687 1
261 595 1
373 780 1
28 977 1
393 412 1
315 676 1
6 474 1
142 246 1
164 413 1
548 960 1
316 849 1
157 8...

output:

1000

result:

ok single line: '1000'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

6 8
1 2 1
4 5 2
1 3 3
4 6 4
2 3 5
5 6 6
1 4 7
2 5 1000

output:

1010

result:

ok single line: '1010'

Test #11:

score: 0
Accepted
time: 1793ms
memory: 34832kb

input:

100000 1000000
12367 40267 217042
44689 75139 285000
27281 61254 650783
64350 65848 90802
31547 46704 668855
30690 87250 123830
55229 89914 134555
43197 77248 447563
59620 64482 84718
67546 90659 107138
87307 99160 861726
36383 55308 767446
1561 33387 137971
24268 29109 347562
15682 32464 829425
175...

output:

5000240552

result:

ok single line: '5000240552'

Test #12:

score: 0
Accepted
time: 508ms
memory: 40204kb

input:

100000 1000000
12069 21404 973640
47403 82448 575756
51035 86101 611267
34777 70481 835275
46754 96838 199861
47123 99642 584115
14012 46957 12043
29163 82001 238555
15665 21114 582046
52542 72882 145997
48788 86805 71645
55696 60559 28420
24472 32956 970861
30323 79740 280305
16356 29126 852205
104...

output:

5000949999

result:

ok single line: '5000949999'

Test #13:

score: 0
Accepted
time: 548ms
memory: 35312kb

input:

100000 1000000
7560 27215 539112
11841 89448 458869
18558 25453 534842
20796 39894 269187
19421 40634 197392
81528 88593 924657
18098 35942 220928
37279 85814 326044
7735 33102 988296
54369 81092 443465
28300 86072 323659
53358 67008 686062
37526 71417 187505
32248 50901 664798
21168 82244 131232
12...

output:

5000553263

result:

ok single line: '5000553263'

Test #14:

score: 0
Accepted
time: 1657ms
memory: 35024kb

input:

100000 1000000
39912 98590 65418
53792 91485 598935
6079 71413 704258
67462 97424 592305
14658 25845 119846
470 83284 375037
84027 90827 456066
9301 98887 653954
1180 9895 580186
38111 87780 928796
86121 94716 146680
21193 83340 433490
56653 97338 127399
69900 83977 175519
12579 46905 984148
51122 6...

output:

5000515258

result:

ok single line: '5000515258'

Test #15:

score: 0
Accepted
time: 1529ms
memory: 34420kb

input:

100000 1000000
63768 95110 915242
21998 25477 386830
56334 71336 153720
33761 52484 776686
13944 48585 477294
76970 79883 886960
13051 51987 54917
42442 82083 538512
51802 82217 438246
24719 32605 791507
21071 66257 492497
27776 36916 164929
71814 78797 928531
3713 36716 464025
29004 38111 193644
56...

output:

5000515255

result:

ok single line: '5000515255'

Test #16:

score: 0
Accepted
time: 115ms
memory: 8512kb

input:

1000 250000
243 914 83561
73 808 729584
245 894 449189
195 865 17164
678 915 845681
886 927 284861
483 827 822925
363 528 683053
549 830 482474
39 877 547317
354 387 591427
76 258 110610
98 416 588179
144 956 401086
351 998 905121
71 213 809576
961 986 772298
346 757 862013
114 191 702375
560 665 71...

output:

2518708

result:

ok single line: '2518708'

Test #17:

score: 0
Accepted
time: 742ms
memory: 36472kb

input:

100000 1000000
34605 87209 915242
21998 25477 386830
83807 92720 153720
34324 88320 776686
22930 39507 477294
76970 79883 886960
13051 89000 54917
21575 76701 538512
72651 95538 438246
21440 25945 791507
19158 67707 492497
17422 77826 164929
24505 36789 928531
35013 69955 464025
29004 38111 193644
4...

output:

5000553258

result:

ok single line: '5000553258'

Test #18:

score: 0
Accepted
time: 1603ms
memory: 34396kb

input:

100000 1000000
66930 98590 65418
10570 78604 598935
6079 71413 704258
78358 86581 592305
14658 25845 119846
1600 92825 375037
84027 90827 456066
9301 98887 653954
5981 43116 580186
24424 86607 928796
86121 94716 146680
21193 83340 433490
56653 97338 127399
69900 83977 175519
12579 46905 984148
26219...

output:

5000515256

result:

ok single line: '5000515256'

Test #19:

score: 0
Accepted
time: 581ms
memory: 33796kb

input:

99857 1000000
99855 99856 273
99854 99855 154
99853 99854 130
99852 99853 117
99851 99852 180
99849 99850 212
99846 99847 93
99844 99845 274
99843 99844 3
99842 99843 197
99841 99842 272
99840 99841 198
99838 99839 291
99837 99838 55
99836 99837 58
99834 99835 95
99833 99834 16
99832 99833 253
99831...

output:

16175943

result:

ok single line: '16175943'

Test #20:

score: 0
Accepted
time: 64ms
memory: 23932kb

input:

100000 100000
8837 90645 114668
86577 89419 631368
64622 96884 535878
66681 89298 205850
32773 77428 97512
8043 80274 475896
18113 64154 594477
27618 66753 242080
2170 98658 488268
8491 76457 707807
47299 60368 674987
6419 20503 552599
27472 76520 54214
63994 75084 550876
81997 82247 683831
66671 66...

output:

50089529542

result:

ok single line: '50089529542'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3728kb

input:

100 4950
3 76 69754
14 71 530005
77 80 982767
71 98 719233
6 20 606528
44 73 739817
59 85 271485
15 36 544230
12 74 89964
6 49 720606
18 71 310305
21 58 343628
72 98 595651
45 88 254518
7 9 53864
56 64 274251
53 87 13846
45 92 26726
19 90 335270
10 42 100011
67 87 424900
59 78 12431
28 70 807210
46 ...

output:

1206558

result:

ok single line: '1206558'

Test #22:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

10 45
1 10 866665
2 3 332848
6 7 925675
2 6 557490
7 8 644185
1 8 615727
5 9 294176
4 5 727445
2 9 53923
3 9 488300
3 10 558156
6 10 620105
3 5 383782
2 5 961800
4 9 111719
1 2 404288
5 10 100880
8 9 645893
1 9 118836
4 8 349053
9 10 203246
2 7 559830
1 7 116064
3 8 511685
1 6 413243
4 7 540928
3 7 ...

output:

1485729

result:

ok single line: '1485729'

Test #23:

score: 0
Accepted
time: 24ms
memory: 6360kb

input:

1000 50000
354 439 238425
815 970 442751
588 672 989227
235 387 484642
56 311 714743
36 440 768541
119 273 366294
116 250 80885
165 439 546236
171 408 13199
119 310 134566
538 726 299090
18 866 930667
320 902 511308
437 582 447292
403 639 641894
506 950 929184
476 937 941248
278 503 876561
47 637 41...

output:

12286998

result:

ok single line: '12286998'

Test #24:

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

input:

1000 5000
564 640 771021
702 735 492813
252 559 607761
278 668 851213
247 900 795168
631 917 306170
287 1000 758549
50 854 137491
570 722 160274
52 432 897413
390 964 34554
497 694 794843
196 555 778074
240 798 818595
314 538 600268
423 471 591221
60 806 345979
259 348 949171
734 896 254576
489 538 ...

output:

112766607

result:

ok single line: '112766607'

Test #25:

score: 0
Accepted
time: 38ms
memory: 5304kb

input:

1000 100000
711 879 681232
266 791 673601
59 837 590557
281 856 176713
618 633 547012
195 434 37330
633 983 45562
592 906 663882
62 339 806013
32 364 523964
78 643 38413
265 311 702446
274 946 281906
177 470 44249
192 311 52137
29 979 366556
283 988 775215
435 830 440903
164 223 498193
591 762 60546...

output:

6168422

result:

ok single line: '6168422'

Test #26:

score: 0
Accepted
time: 24ms
memory: 6264kb

input:

1000 50000
741 997 523288
519 797 242608
386 507 676447
340 403 186981
528 918 205608
499 534 423674
5 214 813420
426 682 396150
469 927 945473
402 421 17005
276 352 559850
372 971 902664
10 236 850939
191 592 869342
295 949 651478
6 200 785845
277 776 353461
292 583 140854
649 682 849195
191 434 74...

output:

12254005

result:

ok single line: '12254005'