QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#145196#4272. Phone PlansLeasier0 359ms124172kbC++985.4kb2023-08-21 23:44:542023-08-21 23:44:55

Judging History

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

  • [2023-08-21 23:44:55]
  • 评测
  • 测评结果:0
  • 用时:359ms
  • 内存:124172kb
  • [2023-08-21 23:44:54]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cstdio>

using namespace std;

typedef long long ll;

typedef struct Edge_tag {
	int nxt;
	int start;
	int end;
	int dis;
	Edge_tag(){}
	Edge_tag(int start_, int end_, int dis_){
		start = start_;
		end = end_;
		dis = dis_;
	}
} Edge;

inline ll comb_2(int n){
	return (ll)n * (n - 1) / 2;
}

typedef struct {
	int id;
	int root[400007];
	int val[400007];
	int ls[400007];
	int rs[400007];
	int size[400007];
	ll sum[400007];
	bool vis[400007];
	Edge edge[400007];
	vector<int> v[400007];
	
	inline void init(int n){
		for (register int i = 1; i <= n; i++){
			root[i] = i;
		}
	}
	
	int get_root(int x){
		if (root[x] == x) return x;
		return root[x] = get_root(root[x]);
	}
	
	inline void kruskal(int n, int m, int k){
		id = n;
		init(n * 2 - 1);
		for (register int i = 1; i <= n; i++){
			size[i] = 1;
			vis[i] = true;
		}
		sort(edge + 1, edge + m + 1);
		for (register int i = 1; i <= m; i++){
			int x_root = get_root(edge[i].start), y_root = get_root(edge[i].end);
			if (x_root != y_root){
				id++;
				root[x_root] = root[y_root] = id;
				val[id] = edge[i].dis;
				vis[id] = true;
				vis[x_root] = vis[y_root] = false;
				if (size[x_root] > size[y_root]){
					ls[id] = x_root;
					rs[id] = y_root;
				} else {
					ls[id] = y_root;
					rs[id] = x_root;
				}
				size[id] = size[x_root] + size[y_root];
				sum[val[id]] += comb_2(size[id]) - comb_2(size[x_root]) - comb_2(size[y_root]);
				v[val[id]].push_back(id);
			}
		}
		for (register int i = 1; i <= k; i++){
			sum[i] += sum[i - 1];
		}
	}
} Kruskal;

int dfn[400007];

typedef struct {
	bool operator ()(const int a, const int b){
		return dfn[a] > dfn[b];
	}
} Compare;

Kruskal kru1, kru2;
int w1[200007], w2[200007], pos1[400007], ref[400007], rnk[200007], pos2[400007];
bool vis1[200007], vis2[200007];
set<int> s1[200007];
set<int, Compare> s2[200007];
map<int, int> mp[200007];

bool operator <(const Edge a, const Edge b){
	return a.dis < b.dis;
}

inline int read_int(){
	int ans = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9'){
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9'){
		ans = ans * 10 + (ch ^ 48);
		ch = getchar();
	}
	return ans;
}

inline ll read_ll(){
	ll ans = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9'){
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9'){
		ans = ans * 10 + (ch ^ 48);
		ch = getchar();
	}
	return ans;
}

void dfs(int u, int n, int &id){
	if (u <= n){
		id++;
		dfn[u] = id;
		rnk[id] = u;
		return;
	}
	dfs(kru2.ls[u], n, id);
	dfn[u] = dfn[kru2.ls[u]];
	dfs(kru2.rs[u], n, id);
}

int main(){
	int n = read_int(), a = read_int(), b = read_int(), cnt1 = 0, cnt2 = 0, dfn_id = 0, set_id = 0, lst = -1, ans = 0x7fffffff;
	ll k = read_ll(), sum = 0;
	w1[++cnt1] = 0;
	for (register int i = 1; i <= a; i++){
		int u = read_int(), v = read_int(), l = read_int();
		w1[++cnt1] = l;
		kru1.edge[i] = Edge(u, v, l);
	}
	sort(w1 + 1, w1 + cnt1 + 1);
	cnt1 = unique(w1 + 1, w1 + cnt1 + 1) - w1 - 1;
	for (register int i = 1; i <= a; i++){
		kru1.edge[i].dis = lower_bound(w1 + 1, w1 + cnt1 + 1, kru1.edge[i].dis) - w1;
	}
	kru1.kruskal(n, a, cnt1);
	w2[++cnt2] = 0;
	for (register int i = 1; i <= b; i++){
		int u = read_int(), v = read_int(), l = read_int();
		w2[++cnt2] = l;
		kru2.edge[i] = Edge(u, v, l);
	}
	sort(w2 + 1, w2 + cnt2 + 1);
	cnt2 = unique(w2 + 1, w2 + cnt2 + 1) - w2 - 1;
	for (register int i = 1; i <= b; i++){
		kru2.edge[i].dis = lower_bound(w2 + 1, w2 + cnt2 + 1, kru2.edge[i].dis) - w2;
	}
	kru2.kruskal(n, b, cnt2);
	dfs(kru2.id, n, dfn_id);
	for (register int i = 1; i <= n; i++){
		pos1[i] = ref[i] = i;
		s1[i].insert(i);
	}
	for (register int i = 1; i <= kru2.id; i++){
		if (kru2.vis[i]){
			int t = dfn_id + 1;
			dfs(i, n, dfn_id);
			pos2[i] = ++set_id;
			for (register int j = t; j <= dfn_id; j++){
				s2[set_id].insert(rnk[j]);
				mp[rnk[j]][set_id] = 1;
			}
		}
	}
	for (register int i = 1, j = cnt2; i <= cnt1; i++){
		int size = kru1.v[i].size();
		for (register int k = 0; k < size; k++){
			int x = kru1.v[i][k];
			pos1[x] = pos1[kru1.ls[x]];
			for (register set<int>::iterator l = s1[pos1[kru1.rs[x]]].begin(); l != s1[pos1[kru1.rs[x]]].end(); l++){
				ref[*l] = pos1[x];
				s1[pos1[x]].insert(*l);
			}
			s1[pos1[kru1.rs[x]]].clear();
			for (register map<int, int>::iterator l = mp[pos1[kru1.rs[x]]].begin(); l != mp[pos1[kru1.rs[x]]].end(); l++){
				sum -= comb_2(mp[pos1[x]][l->first]);
				mp[pos1[x]][l->first] += l->second;
				sum += comb_2(mp[pos1[x]][l->first]);
			}
			mp[pos1[kru1.rs[x]]].clear();
		}
		while (j >= 1 && kru1.sum[i] + kru2.sum[j] - sum >= k){
			lst = j;
			for (register int k = (int)kru2.v[j].size() - 1; k >= 0; k--){
				int x = kru2.v[j][k];
				pos2[kru2.ls[x]] = pos2[x];
				pos2[kru2.rs[x]] = ++set_id;
				while (true){
					int cur = *s2[pos2[x]].begin();
					if (dfn[cur] < dfn[kru2.rs[x]]) break;
					if (--mp[ref[cur]][pos2[x]] == 0){
						mp[ref[cur]].erase(pos2[x]);
					} else {
						sum -= mp[ref[cur]][pos2[x]];
					}
					s2[pos2[x]].erase(cur);
					sum += mp[ref[cur]][set_id]++;
					s2[set_id].insert(cur);
				}
			}
			j--;
		}
		if (lst != -1) ans = min(ans, w1[i] + w2[lst]);
	}
	if (ans == 0x7fffffff){
		cout << -1;
	} else {
		cout << 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: 9ms
memory: 83436kb

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: 1ms
memory: 68296kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #3:

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

input:

2 0 0 1

output:

-1

result:

ok single line: '-1'

Test #4:

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

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

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: 0
Accepted
time: 58ms
memory: 83556kb

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:

9768257

result:

ok single line: '9768257'

Test #7:

score: 0
Accepted
time: 70ms
memory: 77812kb

input:

2000 200000 0 1937051
1325 1770 628367155
105 1670 728177982
358 778 959944062
826 1691 651665248
1119 1906 382208628
1684 1232 677646622
807 265 902880211
1685 1660 405567549
1853 1982 988679307
1241 1054 385406922
862 1049 356441384
1837 673 443580113
1082 1795 738355567
1703 663 221254144
1792 84...

output:

20263921

result:

ok single line: '20263921'

Test #8:

score: 0
Accepted
time: 128ms
memory: 89448kb

input:

2000 200000 200000 1999000
1303 1065 135024256
1400 1409 157921645
1208 515 422761224
466 1398 267944161
40 1202 560552418
722 1817 773826339
1022 1534 720452215
1512 200 145291518
538 230 98848391
434 529 911575234
470 1050 103133355
1800 351 180303134
1527 1871 779519820
258 1872 279904732
1 512 4...

output:

1999

result:

ok single line: '1999'

Test #9:

score: 0
Accepted
time: 119ms
memory: 94352kb

input:

2000 200000 200000 1999000
1566 1720 828051227
411 209 634755980
293 258 80524979
1149 1694 253684780
71 597 899974207
1934 440 11614281
1846 870 794303074
800 1090 576722282
1022 1619 486756658
1135 1004 589873220
1300 326 565865513
114 341 308227260
310 134 735603010
437 291 714079414
930 1131 136...

output:

1999

result:

ok single line: '1999'

Test #10:

score: 0
Accepted
time: 120ms
memory: 93876kb

input:

2000 200000 200000 1999000
1505 1008 194848918
955 1466 251380587
306 119 329528655
1072 1067 684468438
1798 755 822161204
1475 1987 886864916
609 790 525637795
1937 1639 534864650
63 792 948290690
1282 553 998185081
1995 243 367638087
1517 982 238895274
1891 358 612397768
229 1599 453255817
200 115...

output:

1999

result:

ok single line: '1999'

Test #11:

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

input:

2000 200000 200000 1999000
888 998 519088944
896 217 366603047
963 1703 281897731
1419 1583 296352197
318 1644 449932680
1252 683 299241251
1763 1736 16908823
400 673 940814267
1864 1654 16539370
21 1360 526031095
320 1052 879613936
383 555 433309121
243 869 603865861
567 236 829646077
1057 1138 545...

output:

2000

result:

ok single line: '2000'

Test #12:

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

input:

2000 0 0 0

output:

0

result:

ok single line: '0'

Test #13:

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

input:

2000 0 0 1

output:

-1

result:

ok single line: '-1'

Test #14:

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

input:

2000 0 0 1999000

output:

-1

result:

ok single line: '-1'

Test #15:

score: 0
Accepted
time: 118ms
memory: 89908kb

input:

2000 200000 200000 0
584 721 144517612
1593 1184 19655376
572 91 267489352
441 830 206284803
326 901 399207297
1220 164 270714861
1760 1765 242123575
1341 1187 778391819
247 104 618482901
1650 876 469853007
1338 660 237312298
593 1856 254405769
477 1212 387844191
603 1896 336160240
1397 444 77343103...

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 117ms
memory: 94288kb

input:

2000 200000 200000 1
1961 656 558726882
677 479 191436128
1411 291 100142168
932 1506 28223846
1264 1394 780504516
1276 1479 56569386
1363 65 480245792
1598 78 760054359
78 36 593906829
1112 535 999996793
1792 953 155333434
149 239 901391869
791 1800 803139774
1856 629 626568419
1931 779 330675974
8...

output:

2823

result:

ok single line: '2823'

Test #17:

score: 0
Accepted
time: 126ms
memory: 88068kb

input:

2000 200000 200000 4
1165 1872 797718594
1169 1055 624587802
866 899 658187098
1376 1679 853872729
635 1186 401602136
1058 241 170579264
1988 1953 539060919
1695 772 816154776
1551 1453 798051114
1678 810 856500252
637 1519 373040351
1663 1237 499974912
1222 174 950339946
952 1956 666630512
1053 17 ...

output:

13422

result:

ok single line: '13422'

Test #18:

score: 0
Accepted
time: 127ms
memory: 92084kb

input:

2000 200000 200000 51
825 28 972433150
472 1380 981558124
178 1129 462887818
496 1916 874585551
1806 1093 459363528
853 1063 835229701
1473 1816 509538236
1762 139 445678501
600 826 116492230
1924 1426 768569961
1093 802 138198370
1285 101 612512942
1696 527 863059072
1729 1091 238488099
1799 1725 9...

output:

207785

result:

ok single line: '207785'

Test #19:

score: 0
Accepted
time: 125ms
memory: 93816kb

input:

2000 200000 200000 346
479 1073 12925746
1739 528 708374552
1669 1597 187841053
420 1886 665883774
264 1753 39508213
1618 1695 153537785
734 950 163927318
946 639 332538805
1151 821 988862824
981 1265 633217016
1627 199 830812032
116 579 44204899
511 403 764449881
1828 538 469036862
953 1145 6928974...

output:

1325200

result:

ok single line: '1325200'

Test #20:

score: 0
Accepted
time: 117ms
memory: 89360kb

input:

2000 200000 200000 7700
1618 867 271795108
1951 151 702948614
463 231 446592273
882 479 999691562
1160 1496 458774157
862 177 365391789
1483 1529 152692222
1086 1926 981642023
650 1846 36577190
1541 1521 287755177
1549 145 326097587
1355 1965 846861888
968 1069 919410456
557 6 666217744
499 1453 429...

output:

4833174

result:

ok single line: '4833174'

Test #21:

score: 0
Accepted
time: 108ms
memory: 94032kb

input:

2000 200000 200000 10274
427 1831 50634939
924 761 973624360
1716 77 896823723
1770 286 368244715
281 1652 693233017
782 982 856834549
1990 872 586803005
1104 545 550493462
438 75 19630828
1939 95 811185364
1691 325 517180701
1832 1560 781228281
687 1611 15723447
651 73 423147727
1750 768 923943157
...

output:

4715135

result:

ok single line: '4715135'

Test #22:

score: 0
Accepted
time: 131ms
memory: 93876kb

input:

2000 200000 200000 353724
828 1686 612966370
1332 36 325102273
50 1233 839762664
1389 740 256158152
640 18 76640380
1324 508 966364428
1791 314 236880043
1092 1586 81535148
602 1178 352534909
640 1224 733613612
970 297 526950089
1654 1677 277859934
1583 1933 511147393
56 1854 778804499
1051 17 52473...

output:

6546895

result:

ok single line: '6546895'

Test #23:

score: -6
Wrong Answer
time: 123ms
memory: 93160kb

input:

2000 200000 200000 1999000
1499 1526 627651931
378 1689 609556194
1326 237 371224081
98 527 251964139
1280 1750 293612965
905 1848 881495354
1870 900 902279678
1496 267 634703468
59 800 164035702
109 1934 882309468
1966 702 519776748
1020 666 951531959
385 850 241053535
1211 255 903671592
66 1285 65...

output:

35305376

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #53:

score: 5
Accepted
time: 359ms
memory: 123508kb

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: 0
Accepted
time: 260ms
memory: 118884kb

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
Wrong Answer
time: 151ms
memory: 124172kb

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:

84656100

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 6
Accepted
time: 1ms
memory: 68336kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #104:

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

input:

2 0 0 1

output:

-1

result:

ok single line: '-1'

Test #105:

score: 0
Accepted
time: 54ms
memory: 83488kb

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:

10104

result:

ok single line: '10104'

Test #106:

score: 0
Accepted
time: 26ms
memory: 86244kb

input:

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

output:

1000000000

result:

ok single line: '1000000000'

Test #107:

score: -6
Wrong Answer
time: 237ms
memory: 123068kb

input:

200000 10 200000 17900
199675 76237 290240030
50211 6922 761990536
92097 120746 607490
192856 130409 616541101
50427 80049 330957286
129588 180294 466199684
8674 110653 155297749
91380 156344 798960399
102127 40858 801752583
94673 105892 152356242
185676 6183 263664373
169026 112948 812501808
93517 ...

output:

163588040

result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

0%