-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacwing408.cpp
2123 lines (2034 loc) · 49.9 KB
/
acwing408.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by tategotoazarasi on 24-5-7.
//
#include "acwing408.h"
#include "templates.h"
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
namespace acwing {
namespace acwing3378 {
int main(istream &cin, ostream &cout) {
int n;
int rule;
cin >> n >> rule;
unordered_map<student *, unsigned> index;
vector<student *> students(n);
for(int i = 0; i < n; i++) {
students[i] = new student();
cin >> students[i]->name >> students[i]->score;
if(index.find(students[i]) == index.end())
index[students[i]] = i;
}
sort(students.begin(), students.end(), [rule, &index](student *a, student *b) {
if(a->score == b->score) {
return index[a] < index[b];
}
if(rule == 0) {
return a->score > b->score;
} else {
return a->score < b->score;
}
});
for(const auto &s: students) {
cout << s->name << ' ' << s->score << endl;
}
return 0;
}
}// namespace acwing3378
namespace acwing3376 {
int main(istream &cin, ostream &cout) {
int n;
cin >> n;
vector<student> students(n);
for(int i = 0; i < n; i++) {
cin >> students[i].id >> students[i].score;
students[i].id_numeric = stoi(students[i].id);
}
sort(students.begin(), students.end(), [](const student &a, const student &b) {
if(a.score == b.score) {
return a.id_numeric < b.id_numeric;
}
return a.score < b.score;
});
for(const auto &s: students) {
cout << s.id << ' ' << s.score << endl;
}
return 0;
}
}// namespace acwing3376
namespace acwing3374 {
int main(istream &cin, ostream &cout) {
int m, n;
string x;
cin >> m >> n >> x;
if(x == "0") {
cout << "0";
return 0;
}
vector<unsigned short> input = vector<unsigned short>();
vector<unsigned short> ans = vector<unsigned short>();
queue<unsigned short> quotient = queue<unsigned short>();
for(auto i = x.begin(); i != x.end(); i++) {
if(isdigit(*i)) {
input.push_back(*i - '0');
} else if(isupper(*i)) {
input.push_back(*i - 'A' + 10);
}
}
do {
unsigned int current = 0;
quotient = queue<unsigned short>();
for(auto i = input.begin(); i != input.end(); i++) {
current *= m;
current += *i;
quotient.push(current / n);
current %= n;
}
while(!quotient.empty() && quotient.front() == 0) {
quotient.pop();
}
ans.push_back(current);
input = vector<unsigned short>(quotient.size());
for(unsigned int i = 0; i < input.size(); i++) {
input[i] = quotient.front();
quotient.pop();
}
} while(!input.empty());
for(auto i = ans.rbegin(); i != ans.rend(); i++) {
if(*i < 10) {
cout << *i;
} else {
cout << (char) (*i - 10 + 'a');
}
}
return 0;
}
}// namespace acwing3374
namespace acwing3757 {
void rearrangedList(struct ListNode *head) {
if(head == NULL || head->next == NULL)
return;
int len = 0;
for(struct ListNode *p = head; p != NULL; p = p->next)
len++;
int mid = (len + 1) / 2;
struct ListNode *a = head;
for(int i = 0; i < mid - 1; i++) {
a = a->next;
}
struct ListNode *b = a->next;
struct ListNode *c = b->next;
a->next = NULL;
b->next = NULL;
while(c) {
struct ListNode *tmp = c->next;
c->next = b;
b = c;
c = tmp;
}
struct ListNode *p = head;
struct ListNode *q = b;
while(q) {
auto qq = q->next;
// 插入结点
q->next = p->next;
p->next = q;
// 移动p和q
p = q->next;
q = qq;
}
}
}// namespace acwing3757
namespace acwing3607 {
int main(istream &cin, ostream &cout) {
int year, day;
int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day_of_month_leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int start_day_of_month[14] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INT_MAX};
while(cin >> year >> day) {
bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if(leap) {
for(int i = 1; i <= 12; i++) {
start_day_of_month[i] = start_day_of_month[i - 1] + day_of_month_leap[i - 1];
}
} else {
for(int i = 1; i <= 12; i++) {
start_day_of_month[i] = start_day_of_month[i - 1] + day_of_month[i - 1];
}
}
cout << setw(4) << setfill('0') << year << '-';
for(int i = 1; i <= 13; i++) {
if(start_day_of_month[i] > day) {
cout << setw(2) << setfill('0') << i - 1 << '-';
day -= start_day_of_month[i - 1];
day++;
cout << setw(2) << setfill('0') << day << endl;
break;
}
}
memset(start_day_of_month, 0, sizeof(start_day_of_month));
start_day_of_month[0] = 1;
start_day_of_month[13] = INT_MAX;
}
return 0;
}
}// namespace acwing3607
namespace acwing3573 {
int main(istream &cin, ostream &cout) {
int t, year, month, day, a;
int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day_of_month_leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int(*day_of_month_p)[13] = nullptr;
cin >> t;
while(t--) {
cin >> year >> month >> day >> a;
day_of_month_p = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? &day_of_month_leap : &day_of_month;
day += a;
while(day > (*day_of_month_p)[month]) {
day -= (*day_of_month_p)[month];
month++;
if(month > 12) {
month = 1;
year++;
day_of_month_p = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? &day_of_month_leap : &day_of_month;
}
}
cout << setw(4) << setfill('0') << year << '-' << setw(2) << setfill('0') << month << '-' << setw(2) << setfill('0') << day << endl;
}
return 0;
}
}// namespace acwing3573
namespace acwing3302_408 {
int main(istream &cin, ostream &cout) {
int x;
char y;
unordered_map<char, int> priority = {
{'+', 1},
{'-', 1},
{'*', 2},
{'/', 2}};
stack<char> op = stack<char>();
stack<int> num = stack<int>();
while(!cin.eof() || !cin.fail() || !cin.bad()) {
if(cin.peek() == EOF)
break;
if(isdigit(cin.peek())) {
cin >> x;
num.push(x);
} else {
cin >> y;
if(y == '(') {
op.push(y);
} else if(y == ')') {
while(!op.empty() && op.top() != '(') {
int b = num.top();
num.pop();
int a = num.top();
num.pop();
char c = op.top();
op.pop();
if(c == '+') {
num.push(a + b);
} else if(c == '-') {
num.push(a - b);
} else if(c == '*') {
num.push(a * b);
} else if(c == '/') {
num.push(a / b);
}
}
op.pop();
} else if(priority.find(y) != priority.end()) {
while(!op.empty() && op.top() != '(' && priority[op.top()] >= priority[y]) {
int b = num.top();
num.pop();
int a = num.top();
num.pop();
char c = op.top();
op.pop();
if(c == '+') {
num.push(a + b);
} else if(c == '-') {
num.push(a - b);
} else if(c == '*') {
num.push(a * b);
} else if(c == '/') {
num.push(a / b);
}
}
op.push(y);
}
}
}
while(!op.empty()) {
int b = num.top();
num.pop();
int a = num.top();
num.pop();
char c = op.top();
op.pop();
if(c == '+') {
num.push(a + b);
} else if(c == '-') {
num.push(a - b);
} else if(c == '*') {
num.push(a * b);
} else if(c == '/') {
num.push(a / b);
}
}
cout << num.top();
return 0;
}
}// namespace acwing3302_408
namespace acwing3766 {
int pathSum(TreeNode *root) {
return pathSum(root, 0);
}
int pathSum(TreeNode *root, int level) {
if(root == nullptr) {
return 0;
}
if(root->left == nullptr && root->right == nullptr) {
return level * root->val;
}
int leftVal = pathSum(root->left, level + 1);
int rightVal = pathSum(root->right, level + 1);
return leftVal + rightVal;
}
}// namespace acwing3766
namespace acwing148 {
int main(istream &cin, ostream &cout) {
priority_queue<int, vector<int>, greater<>> pq = priority_queue<int, vector<int>, greater<>>();
int n;
cin >> n;
int x;
while(n--) {
cin >> x;
pq.push(x);
}
int ans = 0;
while(pq.size() > 1) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
ans += a + b;
pq.push(a + b);
}
cout << ans;
return 0;
}
}// namespace acwing148
namespace acwing836_408 {
vector<int> root;
int find(int x) {
if(root[x] < 0)
return x;
else {
int grand_parent = find(root[x]);
root[x] = grand_parent;
return grand_parent;
}
}
void merge(int x, int y) {
int root_x = find(x);
int root_y = find(y);
if(root_x != root_y) {
if(root[root_x] < root[root_y]) {
root[root_x] += root[root_y];
root[root_y] = root_x;
} else {
root[root_y] += root[root_x];
root[root_x] = root_y;
}
}
}
int main(istream &cin, ostream &cout) {
int n, m;
cin >> n >> m;
root = vector<int>(n + 1, -1);
char op;
int a, b;
while(m--) {
cin >> op >> a >> b;
if(op == 'M') {
merge(a, b);
} else {
cout << (find(a) == find(b) ? "Yes" : "No") << endl;
}
}
return 0;
}
}// namespace acwing836_408
namespace acwing18 {
TreeNode *rebuild(vector<int> &inorder, int in_begin, int in_end, vector<int> &preorder, int pre_begin, int pre_end) {
if(in_begin == in_end) {
return new TreeNode(inorder[in_begin]);
}
TreeNode *root = new TreeNode(preorder[pre_begin]);
int left_in_cursor = in_begin;
int left_pre_cursor = pre_begin + 1;
if(inorder[in_begin] == preorder[pre_begin]) {
root->left = nullptr;
} else {
unordered_set<int> left = unordered_set<int>();
while(inorder[left_in_cursor] != preorder[pre_begin]) {
left.insert(inorder[left_in_cursor]);
left_in_cursor++;
}
while(left.find(preorder[left_pre_cursor]) != left.end()) {
left_pre_cursor++;
}
root->left = rebuild(inorder, in_begin, left_in_cursor - 1, preorder, pre_begin + 1, left_pre_cursor - 1);
}
if(inorder[in_end] == preorder[pre_begin]) {
root->right = nullptr;
} else {
root->right = rebuild(inorder, left_in_cursor + 1, in_end, preorder, left_pre_cursor, pre_end);
}
return root;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.empty() || inorder.empty()) {
return nullptr;
}
return rebuild(inorder, 0, inorder.size() - 1, preorder, 0, preorder.size() - 1);
}
}// namespace acwing18
/**
* @brief 3786. 二叉排序树
*/
namespace acwing3786 {
TreeNode *root = nullptr;
const int INF = 2e9;
int main(istream &cin, ostream &cout) {
int n;
cin >> n;
while(n--) {
int t, x;
cin >> t >> x;
if(t == 1)
insert(root, x);
else if(t == 2)
remove(root, x);
else if(t == 3)
cout << get_pre(root, x) << endl;
else
cout << get_suc(root, x) << endl;
}
return 0;
}
// 插入操作
void insert(TreeNode *&root, int x) {
/*
1. 递归找到x的待插入的位置
2. 如果x < 当前root就递归到左子树,反之,递归到右子树。
*/
if(!root)
root = new TreeNode(x);
// 如果发现数值相同的话就判断一下;
if(root->val == x)
return;
else if(x < root->val)
insert(root->left, x);
else
insert(root->right, x);
}
void remove(TreeNode *&root, int x) {
/*
1. 待删除的结点只有左子树。立即可推出,左子树上的结点都是小于待删除的结点的,我们只需要把待删除结点删了然后左子树接上待删除结点的父节点就可以了。
2. 待删除的结点只有右子树。立即可推出,右子树上的结点都是大于待删除的结点的,我们只需要把待删除结点删了然后右子树接上待删除结点的父节点就可以了。
3. 待删除的结点既有左子树又有右子树。这个比上两个情况麻烦一点,但也不麻烦,需要读者理解的是,怎么能删除结点后还不改变中序遍历的结果,并且操作代价最小,显而易见,我们根据待删除结点的左子树可以得到最右下角的最后结点满足$<x$并且最接近x的结点,把这个结点覆盖待删除的结点,然后把这个结点删除,就完成了我们的操作。
*/
// 如果不存在直接return
if(!root)
return;
if(x < root->val)
remove(root->left, x);
else if(x > root->val)
remove(root->right, x);
else {
if(!root->left && !root->right)
root = NULL;
else if(!root->left)
root = root->right;
else if(!root->right)
root = root->left;
else {
auto p = root->left;
while(p->right)
p = p->right;
root->val = p->val;
remove(root->left, p->val);
}
}
}
// 输出数值 x 的前驱(前驱定义为现有所有数中小于 x 的最大的数)。
int get_pre(TreeNode *root, int x) {
if(!root)
return -INF;
if(root->val >= x)
return get_pre(root->left, x);
else
return max(root->val, get_pre(root->right, x));
}
// 输出数值 x 的后继(后继定义为现有所有数中大于 x 的最小的数)。
int get_suc(TreeNode *root, int x) {
if(!root)
return INF;
if(root->val <= x)
return get_suc(root->right, x);
else
return min(root->val, get_suc(root->left, x));
}
}// namespace acwing3786
namespace acwing149 {
bool Compare::operator()(huff_tree *const &a, huff_tree *const &b) {
if(a->val != b->val)
return a->val > b->val;
else
return a->height > b->height;
}
int main(istream &cin, ostream &cout) {
u_int64_t n, k, w;
cin >> n >> k;
priority_queue<huff_tree *, vector<huff_tree *>, Compare> pq;
for(int i = 0; i < n; i++) {
cin >> w;
pq.push(new huff_tree(w, 1, k));
}
while((n - 1) % (k - 1)) {
pq.push(new huff_tree(0, 1, k));
n++;
}
while(pq.size() > 1) {
huff_tree *ht = new huff_tree(0, 0, k);
for(int i = 0; i < k && !pq.empty(); i++) {
huff_tree *least = pq.top();
ht->height = max(ht->height, least->height + 1);
ht->children[i] = least;
ht->val += least->val;
pq.pop();
}
pq.push(ht);
}
huff_tree *root = pq.top();
queue<pair<huff_tree *, u_int64_t>> q;
q.push(make_pair(root, 0));
u_int64_t min_level = 0;
u_int64_t sum = 0;
while(!q.empty()) {
auto [ht, level] = q.front();
q.pop();
bool flag = true;
for(int i = 0; i < k; i++) {
if(ht->children[i] != nullptr) {
flag = false;
q.push(make_pair(ht->children[i], level + 1));
}
}
if(flag) {
sum += ht->val * level;
min_level = max(min_level, level);
}
}
cout << sum << endl
<< min_level << endl;
return 0;
}
}// namespace acwing149
/**
* @brief 831. KMP字符串
*/
namespace acwing831_408 {
vector<int> get_next(string p) {
vector<int> next(p.size(), 0);
for(int i = 1, j = 0; i < p.size(); i++) {
while(j && p[i] != p[j])
j = next[j - 1];
if(p[i] == p[j])
j++;
next[i] = j;
}
return next;
}
int main(istream &cin, ostream &cout) {
int n, m;
string p, s;
cin >> n >> p >> m >> s;
vector<int> next = get_next(p);
for(int i = 0, j = 0; i < m; i++) {
while(j && s[i] != p[j])
j = next[j - 1];
if(s[i] == p[j])
j++;
if(j == n) {
cout << i - n + 1 << ' ';
j = next[j - 1];
}
}
return 0;
}
}// namespace acwing831_408
/**
* @brief 3385. 玛雅人的密码
*/
namespace acwing3385 {
int main(istream &cin, ostream &cout) {
int n;
string s;
cin >> n >> s;
unordered_set<string> used;
queue<pair<string, int>> q = queue<pair<string, int>>();
q.push(make_pair(s, 0));
used.insert(s);
while(!q.empty()) {
auto [current_s, current_step] = q.front();
if(current_s.find("2012") != string::npos) {
cout << current_step;
return 0;
}
q.pop();
for(int i = 0, j = 1; j < n; i++, j++) {
string next_s = current_s;
swap(next_s[i], next_s[j]);
if(used.find(next_s) == used.end()) {
used.insert(next_s);
q.push(make_pair(next_s, current_step + 1));
}
}
}
cout << -1;
return 0;
}
}// namespace acwing3385
/**
* @brief 3429. 全排列
*/
namespace acwing3429 {
void dfs(vector<char> &stk, int p, ostream &cout, string s) {
if(p == s.length()) {
for(char c: stk) {
cout << c;
}
cout << endl;
return;
}
for(int i = 0; i < s.length(); i++) {
bool contain = false;
for(int j = 0; j < p; j++) {
if(stk[j] == s[i]) {
contain = true;
break;
}
}
if(!contain) {
stk[p] = s[i];
dfs(stk, p + 1, cout, s);
}
}
}
int main(istream &cin, ostream &cout) {
string s;
cin >> s;
vector<char> stk = vector<char>(s.length(), 0);
dfs(stk, 0, cout, s);
return 0;
}
}// namespace acwing3429
/**
* @brief 858. Prim算法求最小生成树
*/
namespace acwing858_408 {
// Custom hash function for pair<int, int>
template<class T1, class T2>
size_t pair_hash::operator()(const pair<T1, T2> &p) const {
auto h1 = hash<T1>{}(p.first);
auto h2 = hash<T2>{}(p.second);
return h1 ^ h2;
}
// Custom equal function for pair<int, int>
template<class T1, class T2>
bool pair_equal::operator()(const pair<T1, T2> &p1, const pair<T1, T2> &p2) const {
return p1.first == p2.first && p1.second == p2.second;
}
// Custom comparator for tuple<int, int, int>
bool tuple_compare::operator()(const tuple<int, int, int> &t1, const tuple<int, int, int> &t2) {
return get<2>(t1) > get<2>(t2);
}
int main(istream &cin, ostream &cout) {
int n, m;
int ans = 0;
unordered_set<int> mst;
unordered_map<int, unordered_set<pair<int, int>, pair_hash, pair_equal>> g;
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, tuple_compare> pq;
cin >> n >> m;
while(m--) {
int u, v, w;
cin >> u >> v >> w;
if(g.find(u) == g.end()) {
g[u] = unordered_set<pair<int, int>, pair_hash, pair_equal>();
}
g[u].insert(make_pair(v, w));
if(g.find(v) == g.end()) {
g[v] = unordered_set<pair<int, int>, pair_hash, pair_equal>();
}
g[v].insert(make_pair(u, w));
}
mst.insert(g.begin()->first);
for(const auto &p: g[g.begin()->first]) {
pq.push(make_tuple(g.begin()->first, p.first, p.second));
}
while(!pq.empty()) {
auto [u, v, w] = pq.top();
pq.pop();
bool u_in = mst.find(u) != mst.end();
bool v_in = mst.find(v) != mst.end();
if(u_in && v_in)
continue;
int to_insert = u_in ? v : u;
mst.insert(to_insert);
ans += w;
for(const auto &p: g[to_insert]) {
if(mst.find(p.first) != mst.end() && mst.find(to_insert) != mst.end())
continue;
pq.push(make_tuple(to_insert, p.first, p.second));
}
}
if(mst.size() == n) {
cout << ans;
} else {
cout << "impossible";
}
return 0;
}
}// namespace acwing858_408
/**
* @brief 849. Dijkstra求最短路 I
*/
namespace acwing849_408 {
int main(istream &cin, ostream &cout) {
int n, m;
cin >> n >> m;
unordered_map<int, unordered_map<int, int>> g = unordered_map<int, unordered_map<int, int>>();
unordered_set<int> visited = unordered_set<int>();
while(m--) {
int x, y, z;
cin >> x >> y >> z;
if(g[x].count(y) == 0 || g[x][y] > z) {
g[x][y] = z;// Only keep the smallest weight for multiple edges
}
}
visited.insert(1);
// Custom comparator for priority_queue to prioritize smaller weights
auto cmp = [](pair<int, int> left, pair<int, int> right) {
return left.second > right.second;// Compare by distance
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);
pq.emplace(1, 0);
while(!pq.empty()) {
auto [u, d] = pq.top();
pq.pop();
if(u != 1 && (visited.find(u) != visited.end()))
continue;
visited.insert(u);
if(u == n) {
cout << d;
return 0;
}
for(auto [v, w]: g[u]) {
if(visited.find(v) != visited.end())
continue;
pq.emplace(v, d + w);
}
}
cout << -1;
return 0;
}
}// namespace acwing849_408
/**
* @brief 854. Floyd求最短路
*/
namespace acwing854_408 {
int main(istream &cin, ostream &cout) {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g = vector<vector<int>>(n + 1, vector<int>(n + 1, INT_MAX));
for(int i = 1; i <= n; i++) {
g[i][i] = 0;
}
while(m--) {
int x, y, z;
cin >> x >> y >> z;
g[x][y] = min(g[x][y], z);
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(g[i][k] != INT_MAX && g[k][j] != INT_MAX) {
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
while(k--) {
int x, y;
cin >> x >> y;
if(g[x][y] == INT_MAX) {
cout << "impossible" << endl;
} else {
cout << g[x][y] << endl;
}
}
return 0;
}
}// namespace acwing854_408
/**
* @brief 848. 有向图的拓扑序列
*/
namespace acwing848_408 {
int main(istream &cin, ostream &cout) {
ostringstream oss;
int n, m;
cin >> n >> m;
vector<unordered_set<int>> g = vector<unordered_set<int>>(n + 1, unordered_set<int>());
vector<short> in = vector<short>(n + 1, 0);
while(m--) {
int x, y;
cin >> x >> y;
if(g[x].find(y) != g[x].end())
continue;
g[x].insert(y);
in[y]++;
}
bool flag = true;
while(flag) {
for(int i = 1; i <= n; i++) {
if(in[i] == 0) {
flag = false;
oss << i << ' ';
for(int j: g[i]) {
in[j]--;
}
in[i] = -1;
}
}
if(flag) {
break;
}
flag = true;
}
for(int i = 1; i <= n; i++) {
if(in[i] != -1) {
cout << -1;
return 0;
}
}
cout << oss.str();
return 0;
}
}// namespace acwing848_408
/**
* @brief 3402. 等差数列
*/
namespace acwing3402 {
int main(istream &cin, ostream &cout) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n + 1, vector<int>(m + 1, 0));
unordered_set<int> filled_rows = unordered_set<int>();
unordered_set<int> filled_cols = unordered_set<int>();
vector<int> row_cnt = vector<int>(n + 1, 0);
vector<int> col_cnt = vector<int>(m + 1, 0);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
if(a[i][j] > 0) {
row_cnt[i]++;
col_cnt[j]++;
}
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(row_cnt[i] == n) {
filled_rows.insert(i);
}
if(col_cnt[j] == m) {
filled_cols.insert(j);
}
}
}
vector<vector<int>> a_cpy(a.begin(), a.end());
queue<pair<bool, int>> q = queue<pair<bool, int>>();// <is_row,index>
for(int i = 1; i < n; i++) {
if(row_cnt[i] > 1) {
filled_rows.insert(i);
q.push(make_pair(true, i));
}
}
for(int i = 1; i < m; i++) {
if(col_cnt[i] > 1) {
filled_cols.insert(i);
q.push(make_pair(false, i));
}
}
while(!q.empty()) {
auto [is_row, index] = q.front();
q.pop();
if(is_row) {
int l = 0;
int r = 0;
for(int j = 1; j <= m; j++) {
if(a[index][j] > 0) {
if(l == 0) {
l = j;
} else if(r == 0) {
r = j;
} else {
break;
}
}
}
int d = (a[index][r] - a[index][l]) / (r - l);
for(int j = 1; j <= m; j++) {
if(a[index][j] == 0) {
a[index][j] = a[index][l] + (j - l) * d;
col_cnt[j]++;
if(col_cnt[j] > 1 && filled_cols.find(j) == filled_cols.end()) {
filled_cols.insert(j);
q.emplace(false, j);
}
}
}
} else {
int l = 0;
int r = 0;
for(int i = 1; i <= n; i++) {
if(a[i][index] > 0) {
if(l == 0) {
l = i;
} else if(r == 0) {
r = i;
} else {
break;
}
}
}
int d = (a[r][index] - a[l][index]) / (r - l);
for(int i = 1; i <= n; i++) {
if(a[i][index] == 0) {
a[i][index] = a[l][index] + (i - l) * d;
row_cnt[i]++;
if(row_cnt[i] > 1 && filled_rows.find(i) == filled_rows.end()) {
filled_rows.insert(i);
q.emplace(true, i);
}
}
}
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(a_cpy[i][j] != a[i][j]) {
cout << i << ' ' << j << ' ' << a[i][j] << endl;
}
}
}
return 0;
}
}// namespace acwing3402
/**
* @brief 3472. 八皇后