-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
1408 lines (674 loc) · 795 KB
/
search.xml
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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>AcWing139 最长回文子串</title>
<link href="/2021/11/19/AcWing139%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2/"/>
<url>/2021/11/19/AcWing139%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2/</url>
<content type="html"><![CDATA[<h3 id="AcWing139"><a href="#AcWing139" class="headerlink" title="AcWing139"></a><a href="https://www.acwing.com/problem/content/141/">AcWing139</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(x,y,z) for(int x = y; x <= z; x++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(x,y,z) for(int x = y; x < z; x++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(x,y,z) for(int x = y; x >= z; x--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(x,y,z) for(int x = y-1; x >= z; x--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, 0, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> {</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T &x) {</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>()) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>()) x = x * <span class="number">10</span> + c - <span class="string">'0'</span>;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x = -x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e6</span> + <span class="number">20</span>;</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> E[N_];</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">HashS</span> {</span></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> F[N_];</span><br><span class="line"></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> P;</span><br><span class="line"><span class="built_in">HashS</span>(<span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> P_ = <span class="number">13331</span>) { <span class="built_in">FL</span>(F, <span class="number">0</span>); <span class="built_in">FL</span>(E, <span class="number">0</span>); E[<span class="number">0</span>] = <span class="number">1</span>; P = P_; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Init</span><span class="params">(<span class="keyword">char</span> *S, <span class="keyword">int</span> len)</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N_) E[i] = E[i<span class="number">-1</span>] * P;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, len) F[i] = F[i<span class="number">-1</span>] * P + S[i] - <span class="number">26</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Ask</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> F[r] - F[l<span class="number">-1</span>] * E[r - l + <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> P_ = <span class="number">13331</span>)</span> </span>{ <span class="built_in">FL</span>(F, <span class="number">0</span>); <span class="built_in">FL</span>(E, <span class="number">0</span>); E[<span class="number">0</span>] = <span class="number">1</span>; P = P_; }</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> &<span class="keyword">operator</span>[](<span class="keyword">int</span> x) { <span class="keyword">return</span> F[x]; }</span><br><span class="line">} I, G;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> idx;</span><br><span class="line"><span class="keyword">char</span> S[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="keyword">while</span>(<span class="number">1</span>) {</span><br><span class="line"><span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">char</span> ch;</span><br><span class="line">S[++len] = <span class="string">'$'</span>;</span><br><span class="line">ch = <span class="built_in">getchar</span>();</span><br><span class="line"><span class="keyword">while</span>(ch >= <span class="string">'a'</span> && ch <= <span class="string">'z'</span>) {</span><br><span class="line">S[++len] = ch, S[++len] = <span class="string">'$'</span>;</span><br><span class="line">ch = <span class="built_in">getchar</span>();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(ch == <span class="string">'E'</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">I.<span class="built_in">Init</span>(S, len);</span><br><span class="line"><span class="built_in">reverse</span>(S+<span class="number">1</span>, S+<span class="number">1</span>+len);</span><br><span class="line">G.<span class="built_in">Init</span>(S, len);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, len) {</span><br><span class="line"><span class="keyword">if</span>(i - ans < <span class="number">1</span> || i + ans > len) <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">while</span>(i-ans<span class="number">-1</span> >= <span class="number">1</span> && i+ans+<span class="number">1</span> <= len </span><br><span class="line"> && I.<span class="built_in">Ask</span>(i-ans<span class="number">-1</span>, i) == G.<span class="built_in">Ask</span>(len-i-ans, len-i+<span class="number">1</span>)) ans++;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"Case %d: %d\n"</span>, ++idx, ans);</span><br><span class="line"></span><br><span class="line"><span class="built_in">FL</span>(S, <span class="number">0</span>);</span><br><span class="line">I.<span class="built_in">Cl</span>(); G.<span class="built_in">Cl</span>();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 字符串 </tag>
<tag> Hash </tag>
<tag> 回文 </tag>
</tags>
</entry>
<entry>
<title>AcWing407 Steady Cow Assignment G</title>
<link href="/2021/11/16/AcWing407SteadyCowAssignmentG/"/>
<url>/2021/11/16/AcWing407SteadyCowAssignmentG/</url>
<content type="html"><![CDATA[<h3 id="AcWing407"><a href="#AcWing407" class="headerlink" title="AcWing407"></a><a href="https://www.acwing.com/problem/content/409/">AcWing407</a></h3><h3 id="双指针"><a href="#双指针" class="headerlink" title="双指针"></a>双指针</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">20</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N], H[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T C[M<<<span class="number">1</span>];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); <span class="built_in">FL</span>(H, <span class="number">0</span>); <span class="built_in">FL</span>(C, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AdI</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T c)</span> </span>{</span><br><span class="line">Nt[++cnt] = I[u]; C[cnt] = c; To[I[u]=cnt] = v;</span><br><span class="line">Nt[++cnt] = I[v]; C[cnt] = <span class="number">0</span>; To[I[v]=cnt] = u;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); <span class="built_in">FL</span>(H, <span class="number">0</span>); <span class="built_in">FL</span>(C, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<<span class="keyword">int</span>, N_+M_, N_*M_> I; </span><br><span class="line"><span class="keyword">int</span> Lm[N_], A[N_][M_], Far[N_+M_];</span><br><span class="line"><span class="keyword">int</span> n, m, s, t;</span><br><span class="line"><span class="keyword">bool</span> Vq[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Hg</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Ck</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="comment">//freopen("I:/Code/Data/In.in", "r", stdin);</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/P2857_4.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, m) IN >> A[i][v];</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m)IN >> Lm[i];</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = INF;</span><br><span class="line"><span class="keyword">int</span> l = <span class="number">1</span>, r = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line"><span class="keyword">bool</span> fg = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(!(fg = <span class="built_in">Ck</span>(l, r)) && r < m) r++;</span><br><span class="line"><span class="keyword">if</span>(fg) ans = <span class="built_in">min</span>(ans, r - l + <span class="number">1</span>);</span><br><span class="line">}<span class="keyword">while</span>(++l <= m);</span><br><span class="line"></span><br><span class="line">cout << ans;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Hg</span><span class="params">()</span> </span>{</span><br><span class="line">queue<<span class="keyword">int</span>> Q; Q.<span class="built_in">push</span>(s);</span><br><span class="line"> <span class="built_in">FL</span>(Far, <span class="number">0</span>); Far[s] = <span class="number">1</span>; </span><br><span class="line">I.H[s] = I.I[s]; </span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) {</span><br><span class="line"><span class="keyword">int</span> tp = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop</span>();</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,tp,I) <span class="keyword">if</span>(!Far[v] && I.C[i]) {</span><br><span class="line">Far[v] = Far[tp]+<span class="number">1</span>, I.H[v] = I.I[v], Q.<span class="built_in">push</span>(v);</span><br><span class="line"><span class="keyword">if</span>(v == t) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(x == t) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">int</span> res = fl;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> &i = I.H[x], v = I.To[i]; i; i = I.Nt[i], v = I.To[i]) </span><br><span class="line"><span class="keyword">if</span>(I.C[i] && Far[v] == Far[x] + <span class="number">1</span>) {</span><br><span class="line"><span class="keyword">int</span> k = <span class="built_in">Dic</span>(v, <span class="built_in">min</span>(res, I.C[i]));</span><br><span class="line">I.C[i] -= k; I.C[i^<span class="number">1</span>] += k; res -= k;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!res) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">if</span>(!k) Far[v] = <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> fl - res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Ck</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{</span><br><span class="line">I.<span class="built_in">Cl</span>(); s = <span class="number">0</span>; t = n + m + <span class="number">1</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m)I.<span class="built_in">AdI</span>(s, i, Lm[i]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n)I.<span class="built_in">AdI</span>(i+m, t, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, l, r) I.<span class="built_in">AdI</span>(A[i][v], i+m, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(<span class="built_in">Hg</span>())</span><br><span class="line">res += <span class="built_in">Dic</span>(s, INF);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> res == n;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 二分图 </tag>
<tag> 二分图匹配 </tag>
<tag> 网络流 </tag>
<tag> 最大流 </tag>
<tag> Dinic </tag>
<tag> 二分图多重匹配 </tag>
</tags>
</entry>
<entry>
<title>Acwing401</title>
<link href="/2021/11/16/AcWing401GoingFromUtoVorFromVtoU/"/>
<url>/2021/11/16/AcWing401GoingFromUtoVorFromVtoU/</url>
<content type="html"><![CDATA[<h3 id="AcWing401"><a href="#AcWing401" class="headerlink" title="AcWing401"></a><a href="https://www.acwing.com/problem/content/description/403/">AcWing401</a></h3><h3 id="对于缩点后的DAG-考虑若拓扑排序的过程中同时有复数点入队-他们互不可达"><a href="#对于缩点后的DAG-考虑若拓扑排序的过程中同时有复数点入队-他们互不可达" class="headerlink" title="对于缩点后的DAG, 考虑若拓扑排序的过程中同时有复数点入队, 他们互不可达"></a>对于缩点后的DAG, 考虑若拓扑排序的过程中同时有复数点入队, 他们互不可达</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">6e3</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u]=cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N]; <span class="keyword">int</span> num, tp;</span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); num = tp = <span class="number">0</span>; }</span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Num</span><span class="params">()</span> </span>{ <span class="keyword">return</span> num; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ num = --tp; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T&x)</span> </span>{ A[num=++tp] = x; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(A, <span class="number">0</span>); num = tp = <span class="number">0</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Stack<<span class="keyword">int</span>, N_> Sk;</span><br><span class="line">Edge<N_, M_> I, S;</span><br><span class="line">vector<<span class="keyword">int</span>> Scc[N_];</span><br><span class="line"><span class="keyword">int</span> Fn[N_], Lw[N_], Msc[N_], In[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, t, cnt, pos;</span><br><span class="line"><span class="keyword">bool</span> Vsk[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Topo</span><span class="params">(<span class="keyword">int</span> s)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> t;</span><br><span class="line"><span class="keyword">while</span>(t--) {</span><br><span class="line">I.<span class="built_in">Cl</span>(); S.<span class="built_in">Cl</span>(); Sk.<span class="built_in">Cl</span>();</span><br><span class="line"><span class="built_in">FL</span>(Fn, <span class="number">0</span>); <span class="built_in">FL</span>(Lw, <span class="number">0</span>); <span class="built_in">FL</span>(Msc, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Vsk, <span class="number">0</span>);</span><br><span class="line">pos = <span class="number">1</span>; cnt = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> flag = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Fn[i]) </span><br><span class="line"><span class="keyword">if</span>(flag) { flag = <span class="number">2</span>; cout << <span class="string">"No\n"</span>; <span class="keyword">break</span>; }</span><br><span class="line"><span class="keyword">else</span> flag = <span class="number">1</span>, <span class="built_in">Tarjan</span>(i);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(flag == <span class="number">2</span>) <span class="keyword">continue</span>; </span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i,<span class="number">1</span>,n) <span class="built_in">ER</span>(k,v,I,i,I) </span><br><span class="line"><span class="keyword">if</span>(Msc[i] != Msc[v]) S.<span class="built_in">Ad</span>(Msc[i], Msc[v]), In[Msc[v]]++;</span><br><span class="line"></span><br><span class="line">flag = <span class="number">0</span>; <span class="keyword">int</span> tmp = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) <span class="keyword">if</span>(!In[i])</span><br><span class="line"><span class="keyword">if</span>(flag) { flag = <span class="number">2</span>; cout << <span class="string">"No\n"</span>; <span class="keyword">break</span>; }</span><br><span class="line"><span class="keyword">else</span> flag = <span class="number">1</span>, tmp = i;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(flag == <span class="number">2</span>) <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">Topo</span>(tmp)) </span><br><span class="line">cout << <span class="string">"No\n"</span>;</span><br><span class="line"><span class="keyword">else</span> </span><br><span class="line">cout << <span class="string">"Yes\n"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Sk.<span class="built_in">Ph</span>(x); Vsk[x] = <span class="number">1</span>;</span><br><span class="line">Lw[x] = Fn[x] = ++pos;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) </span><br><span class="line"><span class="keyword">if</span>(!Fn[v]) <span class="built_in">Tarjan</span>(v), Lw[x] = <span class="built_in">min</span>(Lw[x], Lw[v]);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Vsk[v]) Lw[x] = <span class="built_in">min</span>(Lw[x], Fn[v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Fn[x] == Lw[x]) {</span><br><span class="line"><span class="keyword">int</span> y = Sk.<span class="built_in">Tp</span>(); ++cnt;</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">Scc[cnt].<span class="built_in">push_back</span>(y); Msc[y] = cnt;</span><br><span class="line">Sk.<span class="built_in">Pp</span>(); Vsk[y] = <span class="number">0</span>;</span><br><span class="line">}<span class="keyword">while</span>(y != x && (y = Sk.<span class="built_in">Tp</span>()));</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Topo</span><span class="params">(<span class="keyword">int</span> s)</span> </span>{</span><br><span class="line"><span class="keyword">int</span> x = s;</span><br><span class="line"><span class="built_in">Ir</span>(i,<span class="number">1</span>,cnt) {</span><br><span class="line"><span class="keyword">int</span> flag = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">ER</span>(i,v,S,x,I) {</span><br><span class="line">In[v]--; </span><br><span class="line"><span class="keyword">if</span>(!In[v]) x = v, flag++;</span><br><span class="line"><span class="keyword">if</span>(flag > <span class="number">1</span>) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) <span class="keyword">if</span>(In[i]) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"><span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> Tarjan </tag>
<tag> 缩点 </tag>
<tag> SCC </tag>
<tag> 拓扑排序 </tag>
</tags>
</entry>
<entry>
<title>AcWing398 Traffic Real Time Query System</title>
<link href="/2021/11/16/AcWing398TrafficRealTimeQuerySystem/"/>
<url>/2021/11/16/AcWing398TrafficRealTimeQuerySystem/</url>
<content type="html"><![CDATA[<h3 id="AcWing398"><a href="#AcWing398" class="headerlink" title="AcWing398"></a><a href="https://www.acwing.com/problem/content/400/">AcWing398</a></h3><h3 id="然而luogu过不了"><a href="#然而luogu过不了" class="headerlink" title="然而luogu过不了"></a>然而luogu过不了</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码:"></a>代码:</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> E></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[E<<<span class="number">1</span>], To[E<<<span class="number">1</span>], I[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u]=cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> u, v;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> u_ = <span class="number">0</span>, <span class="keyword">int</span> v_ = <span class="number">0</span>) { u = u_; v = v_; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_<<<span class="number">1</span>, E_<<<span class="number">1</span>> I, S;</span><br><span class="line">Node T[E_<<<span class="number">1</span>];</span><br><span class="line">stack<<span class="keyword">int</span>> Sk;</span><br><span class="line"><span class="keyword">int</span> Fn[N_<<<span class="number">1</span>], Lw[N_<<<span class="number">1</span>], Dep[N_<<<span class="number">1</span>], F[N_<<<span class="number">1</span>][<span class="number">22</span>];</span><br><span class="line"><span class="keyword">int</span> n, m, pos, tot, rt, q;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> vlt)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line">rt = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">I.<span class="built_in">Cl</span>(); S.<span class="built_in">Cl</span>(); </span><br><span class="line"> </span><br><span class="line"><span class="built_in">FL</span>(Fn, <span class="number">0</span>); <span class="built_in">FL</span>(Lw, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Dep, <span class="number">0</span>); <span class="built_in">FL</span>(F, <span class="number">0</span>); <span class="built_in">FL</span>(T, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">tot = n;</span><br><span class="line">pos = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(Sk.<span class="built_in">size</span>()) Sk.<span class="built_in">pop</span>();</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">T[i] = <span class="built_in">Node</span>(u, v);</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v); I.<span class="built_in">Ad</span>(v, u);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">Dep[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">Pre</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">Dp</span>();</span><br><span class="line"></span><br><span class="line">IN >> q;</span><br><span class="line"><span class="keyword">int</span> tmp = q;</span><br><span class="line"><span class="keyword">while</span>(q--) {</span><br><span class="line"><span class="keyword">int</span> s, t; IN >> s >> t;</span><br><span class="line"><span class="keyword">int</span> x = T[s].u, y = T[s].v, a = T[t].u, b = T[t].v;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> C(u, v, w) (((Dep[(u)]+Dep[(v)]-2*Dep[(w)])/2)-1)</span></span><br><span class="line"><span class="keyword">if</span>(q != tmp<span class="number">-1</span>) cout << <span class="string">"\n"</span>;</span><br><span class="line">cout << <span class="built_in">max</span>(<span class="built_in">max</span>(<span class="built_in">C</span>(x, a, <span class="built_in">Lca</span>(x, a)),</span><br><span class="line"> <span class="built_in">C</span>(x, b, <span class="built_in">Lca</span>(x, b)) ),</span><br><span class="line"> <span class="built_in">max</span>(<span class="built_in">C</span>(y, a, <span class="built_in">Lca</span>(y, a)),</span><br><span class="line"> <span class="built_in">C</span>(y, b, <span class="built_in">Lca</span>(y, b)) ) );</span><br><span class="line"><span class="meta">#<span class="meta-keyword">undef</span> C</span></span><br><span class="line">}</span><br><span class="line">IN >> n >> m;</span><br><span class="line">}<span class="keyword">while</span>(n + m);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Sk.<span class="built_in">push</span>(x);</span><br><span class="line">Fn[x] = Lw[x] = ++pos;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) </span><br><span class="line"><span class="keyword">if</span>(!Fn[v]) {</span><br><span class="line"><span class="built_in">Tarjan</span>(v), Lw[x] = <span class="built_in">min</span>(Lw[x], Lw[v]);</span><br><span class="line"><span class="keyword">if</span>(Lw[v] == Fn[x] && ++tot) {</span><br><span class="line"><span class="keyword">int</span> y = Sk.<span class="built_in">top</span>();</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">Sk.<span class="built_in">pop</span>(); </span><br><span class="line">S.<span class="built_in">Ad</span>(tot, y); S.<span class="built_in">Ad</span>(y, tot);</span><br><span class="line">} <span class="keyword">while</span>(y != v && (y = Sk.<span class="built_in">top</span>()));</span><br><span class="line">S.<span class="built_in">Ad</span>(x, tot); S.<span class="built_in">Ad</span>(tot, x);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> Lw[x] = <span class="built_in">min</span>(Lw[x], Fn[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> vlt)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i,v,S,x,I) <span class="keyword">if</span>(v != vlt)</span><br><span class="line">Dep[v] = Dep[x] + <span class="number">1</span>, F[v][<span class="number">0</span>] = x, <span class="built_in">Pre</span>(v, x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(e, <span class="number">1</span>, <span class="number">20</span>) <span class="built_in">IR</span>(i, <span class="number">1</span>, tot) </span><br><span class="line">F[i][e] = F[F[i][e<span class="number">-1</span>]][e<span class="number">-1</span>];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(Dep[x] < Dep[y]) <span class="built_in">swap</span>(x, y);</span><br><span class="line"><span class="built_in">SR</span>(i, <span class="number">20</span>, <span class="number">0</span>) <span class="keyword">if</span>(Dep[F[x][i]] >= Dep[y]) x = F[x][i];</span><br><span class="line"><span class="keyword">if</span>(x == y) <span class="keyword">return</span> x;</span><br><span class="line"></span><br><span class="line"><span class="built_in">SR</span>(i, <span class="number">20</span>, <span class="number">0</span>) <span class="keyword">if</span>(F[x][i] != F[y][i])</span><br><span class="line">x = F[x][i], y = F[y][i];</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> F[x][<span class="number">0</span>];</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> Tarjan </tag>
<tag> v-DCC </tag>
<tag> 圆方树 </tag>
<tag> 割点 </tag>
</tags>
</entry>
<entry>
<title>LG4320 道路相遇</title>
<link href="/2021/11/16/LG4320%E9%81%93%E8%B7%AF%E7%9B%B8%E9%81%87/"/>
<url>/2021/11/16/LG4320%E9%81%93%E8%B7%AF%E7%9B%B8%E9%81%87/</url>
<content type="html"><![CDATA[<h3 id="LG4320"><a href="#LG4320" class="headerlink" title="LG4320"></a><a href="https://www.luogu.com.cn/problem/P4320">LG4320</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><h4 id="建圆方树"><a href="#建圆方树" class="headerlink" title="建圆方树"></a>建圆方树</h4><h4 id="对于每一组点x-y-设z-lca-x-y"><a href="#对于每一组点x-y-设z-lca-x-y" class="headerlink" title="对于每一组点x, y, 设z = lca(x, y)"></a>对于每一组点x, y, 设z = lca(x, y)</h4><ul><li><h4 id="z为方点-Ans-frac-Dep-x-Dep-z-1-2-frac-Dep-y-Dep-z-1-2-frac-Dep-x-Dep-y-2-Dep-z-2-1"><a href="#z为方点-Ans-frac-Dep-x-Dep-z-1-2-frac-Dep-y-Dep-z-1-2-frac-Dep-x-Dep-y-2-Dep-z-2-1" class="headerlink" title="z为方点, $Ans = \frac{Dep_x - Dep_z - 1}{2} + \frac{Dep_y - Dep_z - 1}{2} = \frac{Dep_x + Dep_y - 2 Dep_z}{2} - 1$"></a>z为方点, $Ans = \frac{Dep_x - Dep_z - 1}{2} + \frac{Dep_y - Dep_z - 1}{2} = \frac{Dep_x + Dep_y - 2 Dep_z}{2} - 1$</h4></li><li><h4 id="z为圆点-Ans-frac-Dep-x-Dep-z-2-frac-Dep-y-Dep-z-2-1-frac-Dep-x-Dep-y-2-Dep-z-2-1"><a href="#z为圆点-Ans-frac-Dep-x-Dep-z-2-frac-Dep-y-Dep-z-2-1-frac-Dep-x-Dep-y-2-Dep-z-2-1" class="headerlink" title="z为圆点, $Ans = \frac{Dep_x - Dep_z}{2} + \frac{Dep_y - Dep_z}{2} - 1 = \frac{Dep_x + Dep_y - 2 Dep_z}{2} - 1$"></a>z为圆点, $Ans = \frac{Dep_x - Dep_z}{2} + \frac{Dep_y - Dep_z}{2} - 1 = \frac{Dep_x + Dep_y - 2 Dep_z}{2} - 1$</h4></li></ul><h4 id="综上-两种情况结果相同"><a href="#综上-两种情况结果相同" class="headerlink" title="综上, 两种情况结果相同"></a>综上, 两种情况结果相同</h4><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">5e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">1e6</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> E></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[E<<<span class="number">1</span>], To[E<<<span class="number">1</span>], I[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u]=cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_<<<span class="number">1</span>, E_> I, S;</span><br><span class="line">stack<<span class="keyword">int</span>> Sk;</span><br><span class="line"><span class="keyword">int</span> Fn[N_], Lw[N_], Dep[N_<<<span class="number">1</span>], F[N_<<<span class="number">1</span>][<span class="number">22</span>];</span><br><span class="line"><span class="keyword">int</span> n, m, pos, tot, rt, q;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> vlt)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line">rt = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">I.<span class="built_in">Cl</span>(); S.<span class="built_in">Cl</span>(); </span><br><span class="line"></span><br><span class="line"><span class="built_in">FL</span>(Fn, <span class="number">0</span>); <span class="built_in">FL</span>(Lw, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Dep, <span class="number">0</span>); <span class="built_in">FL</span>(F, <span class="number">0</span>); </span><br><span class="line"></span><br><span class="line">tot = n;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(Sk.<span class="built_in">size</span>()) Sk.<span class="built_in">pop</span>();</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v); I.<span class="built_in">Ad</span>(v, u);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">Dep[<span class="number">1</span>] = <span class="number">1</span>; </span><br><span class="line"><span class="built_in">Pre</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">Dp</span>();</span><br><span class="line"></span><br><span class="line">IN >> q;</span><br><span class="line"><span class="keyword">while</span>(q--) {</span><br><span class="line"><span class="keyword">int</span> s, t; IN >> s >> t;</span><br><span class="line"><span class="keyword">int</span> z = <span class="built_in">Lca</span>(s, t);</span><br><span class="line"></span><br><span class="line">cout << (Dep[s] + Dep[t] - <span class="number">2</span> * Dep[z]) / <span class="number">2</span> + <span class="number">1</span> << <span class="string">"\n"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Sk.<span class="built_in">push</span>(x);</span><br><span class="line">Fn[x] = Lw[x] = ++pos;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) </span><br><span class="line"><span class="keyword">if</span>(!Fn[v]) {</span><br><span class="line"><span class="built_in">Tarjan</span>(v), Lw[x] = <span class="built_in">min</span>(Lw[x], Lw[v]);</span><br><span class="line"><span class="keyword">if</span>(Lw[v] == Fn[x] && ++tot) {</span><br><span class="line"><span class="keyword">int</span> y = Sk.<span class="built_in">top</span>();</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">Sk.<span class="built_in">pop</span>(); </span><br><span class="line">S.<span class="built_in">Ad</span>(tot, y); S.<span class="built_in">Ad</span>(y, tot);</span><br><span class="line">} <span class="keyword">while</span>(y != v && (y = Sk.<span class="built_in">top</span>()));</span><br><span class="line">S.<span class="built_in">Ad</span>(x, tot); S.<span class="built_in">Ad</span>(tot, x);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> Lw[x] = <span class="built_in">min</span>(Lw[x], Fn[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> vlt)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i,v,S,x,I) <span class="keyword">if</span>(v != vlt)</span><br><span class="line">Dep[v] = Dep[x] + <span class="number">1</span>, F[v][<span class="number">0</span>] = x, <span class="built_in">Pre</span>(v, x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(e, <span class="number">1</span>, <span class="number">20</span>) <span class="built_in">IR</span>(i, <span class="number">1</span>, tot) </span><br><span class="line">F[i][e] = F[F[i][e<span class="number">-1</span>]][e<span class="number">-1</span>];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(Dep[x] < Dep[y]) <span class="built_in">swap</span>(x, y);</span><br><span class="line"><span class="built_in">SR</span>(i, <span class="number">20</span>, <span class="number">0</span>) <span class="keyword">if</span>(Dep[F[x][i]] >= Dep[y]) x = F[x][i];</span><br><span class="line"><span class="keyword">if</span>(x == y) <span class="keyword">return</span> x;</span><br><span class="line"></span><br><span class="line"><span class="built_in">SR</span>(i, <span class="number">20</span>, <span class="number">0</span>) <span class="keyword">if</span>(F[x][i] != F[y][i])</span><br><span class="line">x = F[x][i], y = F[y][i];</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> F[x][<span class="number">0</span>];</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> LCA </tag>
<tag> 图论 </tag>
<tag> Tarjan </tag>
<tag> 缩点 </tag>
<tag> v-DCC </tag>
</tags>
</entry>
<entry>
<title>AcWing395 Rudundan Paths</title>
<link href="/2021/11/15/AcWing395RedundantPaths/"/>
<url>/2021/11/15/AcWing395RedundantPaths/</url>
<content type="html"><![CDATA[<h3 id="AcWing395"><a href="#AcWing395" class="headerlink" title="AcWing395"></a><a href="https://www.acwing.com/problem/content/description/397/">AcWing395</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">5e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">1e4</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u]=cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_, E_> I;</span><br><span class="line"><span class="keyword">int</span> Fn[N_], Lw[N_], DccV[N_], In[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, pos, cnt;</span><br><span class="line"><span class="keyword">bool</span> Bg[E_<<<span class="number">1</span>], Vv[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> elt)</span> </span>{</span><br><span class="line">Fn[x] = Lw[x] = ++pos;</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(i != (elt^<span class="number">1</span>))</span><br><span class="line"><span class="keyword">if</span>(!Fn[v]) </span><br><span class="line"><span class="built_in">Tarjan</span>(v, i), Lw[x] = <span class="built_in">min</span>(Lw[x], Lw[v]), </span><br><span class="line">Bg[i] = Bg[i^<span class="number">1</span>] = (Bg[i] | Fn[x] < Lw[v]);</span><br><span class="line"><span class="keyword">else</span> </span><br><span class="line">Lw[x] = <span class="built_in">min</span>(Lw[x], Fn[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Fdcc</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Vv[x] = <span class="number">1</span>; DccV[x] = cnt;</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && !Bg[i]) <span class="built_in">Fdcc</span>(v);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v); I.<span class="built_in">Ad</span>(v, u);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Fn[i]) <span class="built_in">Tarjan</span>(i, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Vv[i]) ++cnt, <span class="built_in">Fdcc</span>(i);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">ER</span>(k,v,I,i,I) </span><br><span class="line"><span class="keyword">if</span>(Bg[k]) In[DccV[i]]++, In[DccV[v]]++;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tmp = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) tmp += In[i] == <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">cout << tmp / <span class="number">2</span> + tmp % <span class="number">2</span> << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 树 </tag>
<tag> 图论 </tag>
<tag> Tarjan </tag>
<tag> e-DCC </tag>
<tag> 缩点 </tag>
</tags>
</entry>
<entry>
<title>AcWing390 逃学的小孩</title>
<link href="/2021/11/15/AcWing390%E9%80%83%E5%AD%A6%E7%9A%84%E5%B0%8F%E5%AD%A9/"/>
<url>/2021/11/15/AcWing390%E9%80%83%E5%AD%A6%E7%9A%84%E5%B0%8F%E5%AD%A9/</url>
<content type="html"><![CDATA[<h3 id="AcWing390"><a href="#AcWing390" class="headerlink" title="AcWing390"></a><a href="https://www.acwing.com/problem/content/description/392/">AcWing390</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><h4 id="路径为-A-gt-B-min-C-gt-A-C-gt-B-可以证明-A-B一定是直径的端点"><a href="#路径为-A-gt-B-min-C-gt-A-C-gt-B-可以证明-A-B一定是直径的端点" class="headerlink" title="路径为 A->B + min(C->A, C->B), 可以证明, A, B一定是直径的端点"></a>路径为 A->B + min(C->A, C->B), 可以证明, A, B一定是直径的端点</h4><h4 id="枚举C"><a href="#枚举C" class="headerlink" title="枚举C"></a>枚举C</h4><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T W[M<<<span class="number">1</span>];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w)</span> </span>{ Nt[++cnt] = I[u]; W[cnt] = w; To[I[u]=cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x; <span class="keyword">long</span> <span class="keyword">long</span> y;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">long</span> <span class="keyword">long</span> y_ = <span class="number">0</span>) { x = x_; y = y_; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<<span class="keyword">long</span> <span class="keyword">long</span>, N_, N_> I;</span><br><span class="line">Node Pr[N_];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dia;</span><br><span class="line"><span class="keyword">int</span> n, m, pr, qr;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Wkp</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">long</span> <span class="keyword">long</span> dis)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(dis > dia) dia = dis, pr = x;</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v]=<span class="number">1</span>)) </span><br><span class="line"><span class="built_in">Wkp</span>(v, dis+I.W[i]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Wkq</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> vlt, <span class="keyword">long</span> <span class="keyword">long</span> wlt, <span class="keyword">long</span> <span class="keyword">long</span> dis)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(dis >= dia) dia = dis, qr = x;</span><br><span class="line">Pr[x] = <span class="built_in">Node</span>(vlt, wlt);</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v]=<span class="number">1</span>)) </span><br><span class="line"><span class="built_in">Wkq</span>(v, x, I.W[i], dis+I.W[i]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Wkx</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> tmp = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v]=<span class="number">1</span>))</span><br><span class="line">tmp = <span class="built_in">max</span>(tmp, <span class="built_in">Wkx</span>(v) + I.W[i]);</span><br><span class="line"><span class="keyword">return</span> tmp;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">int</span> u, v; <span class="keyword">long</span> <span class="keyword">long</span> w;</span><br><span class="line">IN >> u >> v >> w;</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v, w); I.<span class="built_in">Ad</span>(v, u, w);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Vv[<span class="number">1</span>] = <span class="number">1</span>; <span class="built_in">Wkp</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>);</span><br><span class="line">Vv[pr] = <span class="number">1</span>; <span class="built_in">Wkq</span>(pr, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> y = qr;</span><br><span class="line"><span class="keyword">do</span> </span><br><span class="line">Vv[y] = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span>(y != pr && (y = Pr[y].x));</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> x = qr; <span class="keyword">long</span> <span class="keyword">long</span> sum = <span class="number">0</span>, ans = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">do</span> </span><br><span class="line">ans = <span class="built_in">max</span>(ans, <span class="built_in">min</span>(sum, dia-sum) + <span class="built_in">Wkx</span>(x) + dia);</span><br><span class="line"><span class="keyword">while</span>(x != pr && (sum += Pr[x].y) && (x = Pr[x].x));</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 树 </tag>
<tag> 图论 </tag>
<tag> 树的直径 </tag>
<tag> dfs </tag>
</tags>
</entry>
<entry>
<title>AcWing384 升降梯上</title>
<link href="/2021/11/14/AcWing384%E5%8D%87%E9%99%8D%E6%A2%AF%E4%B8%8A/"/>
<url>/2021/11/14/AcWing384%E5%8D%87%E9%99%8D%E6%A2%AF%E4%B8%8A/</url>
<content type="html"><![CDATA[<h3 id="AcWing384"><a href="#AcWing384" class="headerlink" title="AcWing384"></a><a href="https://www.acwing.com/problem/content/description/386/">AcWing384</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><h4 id="设-F-x-v-表示到达第-x-层-操作杆位于-v-的最短路"><a href="#设-F-x-v-表示到达第-x-层-操作杆位于-v-的最短路" class="headerlink" title="设 $F_{x, v}$表示到达第$x$层, 操作杆位于$v$的最短路"></a>设 $F_{x, v}$表示到达第$x$层, 操作杆位于$v$的最短路</h4><h4 id="F-x-v-可以更新"><a href="#F-x-v-可以更新" class="headerlink" title="$F_{x, v}$可以更新"></a>$F_{x, v}$可以更新</h4><ul><li><h5 id="F-x-C-k-k-为-F-x-v-v-k-2-C-k-即移动操作杆"><a href="#F-x-C-k-k-为-F-x-v-v-k-2-C-k-即移动操作杆" class="headerlink" title="$F_{x+C_k, k}$ 为 $F_{x, v} + |v - k| + |2 * C_k|$, 即移动操作杆"></a>$F_{x+C_k, k}$ 为 $F_{x, v} + |v - k| + |2 * C_k|$, 即移动操作杆</h5></li><li><h5 id="F-x-nC-v-v-为-F-x-v-2n-C-v-即不移动操作杆"><a href="#F-x-nC-v-v-为-F-x-v-2n-C-v-即不移动操作杆" class="headerlink" title="$F_{x+nC_v, v}$ 为 $F_{x, v} + |2n * C_v|$, 即不移动操作杆"></a>$F_{x+nC_v, v}$ 为 $F_{x, v} + |2n * C_v|$, 即不移动操作杆</h5></li></ul><h4 id="不需要建图-直接Dij即可"><a href="#不需要建图-直接Dij即可" class="headerlink" title="不需要建图, 直接Dij即可"></a>不需要建图, 直接Dij即可</h4><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li><h4 id="C-i-0"><a href="#C-i-0" class="headerlink" title="$C_i = 0$"></a>$C_i = 0$</h4></li><li><h4 id="凑两条"><a href="#凑两条" class="headerlink" title="(凑两条"></a>(凑两条</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">20</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> F[N_][M_], C[M_];</span><br><span class="line"><span class="keyword">int</span> n, m, fg;</span><br><span class="line"><span class="keyword">bool</span> Vs[N_][M_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dij</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Abs</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="comment">//freopen("I:/Code/Data/WA.out", "w", stdout);</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) IN >> C[i], fg = !C[i] ? i : fg; </span><br><span class="line"></span><br><span class="line"><span class="built_in">Dij</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = INF;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) ans = <span class="built_in">min</span>(ans, F[n][i]);</span><br><span class="line"></span><br><span class="line">cout << (ans != INF ? ans : <span class="number">-1</span>) << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y, v;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>, <span class="keyword">int</span> v_ = <span class="number">0</span>) { x = x_; y = y_; v = v_; }</span><br><span class="line"><span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Node &x, <span class="keyword">const</span> Node &y) { <span class="keyword">return</span> x.v > y.v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dij</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">FL</span>(F, <span class="number">0x3f</span>); <span class="built_in">FL</span>(Vs, <span class="number">0</span>);</span><br><span class="line">priority_queue<Node> Q; Q.<span class="built_in">push</span>(<span class="built_in">Node</span>(<span class="number">1</span>, fg, <span class="number">0</span>));</span><br><span class="line">F[<span class="number">1</span>][fg] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) {</span><br><span class="line"><span class="keyword">int</span> x, y, v;</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">x = Q.<span class="built_in">top</span>().x, y = Q.<span class="built_in">top</span>().y, v = Q.<span class="built_in">top</span>().v,</span><br><span class="line">Q.<span class="built_in">pop</span>();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(Vs[x][y] && Q.<span class="built_in">size</span>());</span><br><span class="line"></span><br><span class="line">Vs[x][y] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> to = x + C[i];</span><br><span class="line"><span class="keyword">if</span>(to < <span class="number">1</span> || to > n || !C[i] || i == y) <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>( F[to][i] > F[x][y] + <span class="built_in">Abs</span>(i - y) + <span class="built_in">Abs</span>(<span class="number">2</span> * C[i]) ) </span><br><span class="line">F[to][i] = F[x][y] + <span class="built_in">Abs</span>(i - y) + <span class="built_in">Abs</span>(<span class="number">2</span> * C[i]),</span><br><span class="line">Q.<span class="built_in">push</span>(<span class="built_in">Node</span>(to, i, F[to][i]));</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> k = x, s = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(k > <span class="number">0</span> && k <= n && C[y]) {</span><br><span class="line">k = k + C[y]; s += <span class="built_in">Abs</span>(<span class="number">2</span> * C[y]);</span><br><span class="line"><span class="keyword">if</span>(k > <span class="number">0</span> && k <= n && F[k][y] > F[x][y] + s) </span><br><span class="line">F[k][y] = F[x][y] + s,</span><br><span class="line">Q.<span class="built_in">push</span>(<span class="built_in">Node</span>(k, y, F[k][y]));</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Abs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{ <span class="keyword">return</span> x < <span class="number">0</span> ? -x : x; }</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> dp </tag>
<tag> 图论 </tag>
<tag> 最短路 </tag>
<tag> 分层图 </tag>
<tag> Dijkstra </tag>
</tags>
</entry>
<entry>
<title>UOJ331 NOIP2017 逛公园</title>
<link href="/2021/11/14/UOJ331NOIP2017%E9%80%9B%E5%85%AC%E5%9B%AD/"/>
<url>/2021/11/14/UOJ331NOIP2017%E9%80%9B%E5%85%AC%E5%9B%AD/</url>
<content type="html"><![CDATA[<h3 id="UOJ331"><a href="#UOJ331" class="headerlink" title="UOJ331"></a><a href="https://uoj.ac/problem/331">UOJ331</a></h3><h3 id="做法"><a href="#做法" class="headerlink" title="做法:\"></a>做法:\</h3><h4 id="设"><a href="#设" class="headerlink" title="设"></a>设</h4><ul><li><h5 id="dis-x-为起点到-x-的最短路径"><a href="#dis-x-为起点到-x-的最短路径" class="headerlink" title="$dis_x$ 为起点到$x$ 的最短路径"></a>$dis_x$ 为起点到$x$ 的最短路径</h5></li><li><h5 id="状态-F-x-k-表示到节点-x-距离-dis-x-k-的方案数"><a href="#状态-F-x-k-表示到节点-x-距离-dis-x-k-的方案数" class="headerlink" title="状态 $F_{x, k}$ 表示到节点 $x$, 距离$dis_x + k$的方案数"></a>状态 $F_{x, k}$ 表示到节点 $x$, 距离$dis_x + k$的方案数</h5></li></ul><h4 id="则"><a href="#则" class="headerlink" title="则"></a>则</h4><ul><li><h5 id="F-x-k-sum-v-x-in-E-F-v-dis-x-k-dis-v-w-w-是边-v-x-的长度"><a href="#F-x-k-sum-v-x-in-E-F-v-dis-x-k-dis-v-w-w-是边-v-x-的长度" class="headerlink" title="$F_{x, k} = \sum_{(v, x)\in E}F[v][dis(x)+k-dis(v)-w]$, $w$是边$(v, x)$的长度"></a>$F_{x, k} = \sum_{(v, x)\in E}F[v][dis(x)+k-dis(v)-w]$, $w$是边$(v, x)$的长度</h5></li></ul><h4 id="建新图连反向边-记忆化搜索"><a href="#建新图连反向边-记忆化搜索" class="headerlink" title="建新图连反向边, 记忆化搜索"></a>建新图连反向边, 记忆化搜索</h4><h4 id="对于环的处理"><a href="#对于环的处理" class="headerlink" title="对于环的处理: \"></a>对于环的处理: \</h4><h5 id="一但路径上出现0环-答案无穷-也就是dp状态转移时出现环"><a href="#一但路径上出现0环-答案无穷-也就是dp状态转移时出现环" class="headerlink" title="一但路径上出现0环, 答案无穷, 也就是dp状态转移时出现环"></a>一但路径上出现0环, 答案无穷, 也就是dp状态转移时出现环</h5><h5 id="所以-在记忆化搜索的过程中-标记正在被处理的点"><a href="#所以-在记忆化搜索的过程中-标记正在被处理的点" class="headerlink" title="所以, 在记忆化搜索的过程中 标记正在被处理的点"></a>所以, 在记忆化搜索的过程中 标记正在被处理的点</h5><h5 id="一但递归过程中重复访问到一个被标记的点-即代表路径上有0环-直接退出输出-1"><a href="#一但递归过程中重复访问到一个被标记的点-即代表路径上有0环-直接退出输出-1" class="headerlink" title="一但递归过程中重复访问到一个被标记的点, 即代表路径上有0环, 直接退出输出$-1$"></a>一但递归过程中重复访问到一个被标记的点, 即代表路径上有0环, 直接退出输出$-1$</h5><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li><h4 id="F可能为0-记搜初值设为-1-搜到边界时特殊赋初值"><a href="#F可能为0-记搜初值设为-1-搜到边界时特殊赋初值" class="headerlink" title="F可能为0, 记搜初值设为-1, 搜到边界时特殊赋初值"></a>F可能为0, 记搜初值设为-1, 搜到边界时特殊赋初值</h4></li><li><h4 id="Spfa要上双端队列优化"><a href="#Spfa要上双端队列优化" class="headerlink" title="Spfa要上双端队列优化"></a>Spfa要上双端队列优化</h4></li><li><h4 id="luogu垃圾"><a href="#luogu垃圾" class="headerlink" title="luogu垃圾"></a>luogu垃圾</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> K_ = <span class="number">50</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N], R[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T W[M << <span class="number">1</span>];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); <span class="built_in">FL</span>(R, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AdI</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w)</span> </span>{ Nt[++cnt] = I[u]; W[cnt] = w; To[I[u]=cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AdT</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w)</span> </span>{ Nt[++cnt] = R[u]; W[cnt] = w; To[R[u]=cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); <span class="built_in">FL</span>(R, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<<span class="keyword">int</span>, N_, M_> I;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> Far[N_], F[N_][K_];</span><br><span class="line"><span class="keyword">int</span> n, m, t, s, f, k, P;</span><br><span class="line"><span class="keyword">bool</span> Vq[N_], Vs[N_][K_];</span><br><span class="line"><span class="keyword">bool</span> fg;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Wk</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> k)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> t;</span><br><span class="line"><span class="keyword">while</span>(t--) {</span><br><span class="line">I.<span class="built_in">Cl</span>(); <span class="built_in">FL</span>(F, <span class="number">-1</span>); <span class="built_in">FL</span>(Vs, <span class="number">0</span>); fg = <span class="number">0</span>;</span><br><span class="line">IN >> n >> m >> k >> P; </span><br><span class="line">s = <span class="number">1</span>, f = n;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v, w; IN >> u >> v >> w;</span><br><span class="line">I.<span class="built_in">AdI</span>(u, v, w); </span><br><span class="line">I.<span class="built_in">AdT</span>(v, u, w);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Spfa</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, k && !fg) </span><br><span class="line">ans += <span class="built_in">Wk</span>(f, i), ans %= P;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!fg) cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"><span class="keyword">else</span> cout << <span class="number">-1</span> << <span class="string">"\n"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Wk</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> k)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(fg) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Vs[x][k]) <span class="keyword">return</span> fg = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(~F[x][k]) <span class="keyword">return</span> F[x][k];</span><br><span class="line"></span><br><span class="line">F[x][k] = x==<span class="number">1</span> && !k;</span><br><span class="line">Vs[x][k] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,R) <span class="keyword">if</span>(Far[x]+k-I.W[i]-Far[v]>=<span class="number">0</span> && !fg) {</span><br><span class="line">F[x][k] += <span class="built_in">Wk</span>(v, Far[x]+k-I.W[i]-Far[v]), F[x][k] %= P;</span><br><span class="line"><span class="keyword">if</span>(fg) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line">Vs[x][k] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> F[x][k];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">FL</span>(Far, <span class="number">0x3f</span>); Far[s] = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">FL</span>(Vq, <span class="number">0</span>); Vq[s] = <span class="number">1</span>;</span><br><span class="line">deque<<span class="keyword">int</span>> Q; Q.<span class="built_in">push_back</span>(s);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(!Q.<span class="built_in">empty</span>()) {</span><br><span class="line"><span class="keyword">int</span> tp = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop_front</span>();</span><br><span class="line">Vq[tp] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,tp,I) </span><br><span class="line"><span class="keyword">if</span>(Far[v]>Far[tp]+I.W[i]) {</span><br><span class="line">Far[v] = Far[tp] + I.W[i];</span><br><span class="line"><span class="keyword">if</span>(!Vq[v] && (Vq[v] = <span class="number">1</span>)) </span><br><span class="line"><span class="keyword">if</span>(Q.<span class="built_in">size</span>() && Far[Q.<span class="built_in">front</span>()] >= Far[v]) Q.<span class="built_in">push_front</span>(v);</span><br><span class="line"><span class="keyword">else</span> Q.<span class="built_in">push_back</span>(v);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 计数 </tag>
<tag> SPFA </tag>
<tag> 最短路 </tag>
<tag> 次短路 </tag>
</tags>
</entry>
<entry>
<title>AcWing387 Arctic Network</title>
<link href="/2021/11/14/AcWing387ArcticNetwork/"/>
<url>/2021/11/14/AcWing387ArcticNetwork/</url>
<content type="html"><![CDATA[<h3 id="AcWing387"><a href="#AcWing387" class="headerlink" title="AcWing387"></a><a href="https://www.acwing.com/problem/content/description/389/">AcWing387</a></h3><h3 id="考虑对于一颗最小生成树-删掉最长的-s-1-条边-则形成-s-颗子树-其中每颗子树选一个点-就是最优情况"><a href="#考虑对于一颗最小生成树-删掉最长的-s-1-条边-则形成-s-颗子树-其中每颗子树选一个点-就是最优情况" class="headerlink" title="考虑对于一颗最小生成树, 删掉最长的$s-1$条边, 则形成$s$颗子树, 其中每颗子树选一个点, 就是最优情况"></a>考虑对于一颗最小生成树, 删掉最长的$s-1$条边, 则形成$s$颗子树, 其中每颗子树选一个点, 就是最优情况</h3><h3 id="Kruskal-添加的第-n-s-条边的长度就是答案"><a href="#Kruskal-添加的第-n-s-条边的长度就是答案" class="headerlink" title="Kruskal, 添加的第 $n-s$ 条边的长度就是答案"></a>Kruskal, 添加的第 $n-s$ 条边的长度就是答案</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">5e2</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Uaf</span> {</span></span><br><span class="line"><span class="keyword">int</span> Fa[N];</span><br><span class="line"><span class="built_in">Uaf</span>() { <span class="built_in">Ir</span>(i, <span class="number">1</span>, N) Fa[i] = i; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Fd</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{ <span class="keyword">return</span> Fa[x] = x == Fa[x] ? x : <span class="built_in">Fd</span>(Fa[x]); }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Un</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{ Fa[<span class="built_in">Fd</span>(x)] = <span class="built_in">Fd</span>(y); }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">Ir</span>(i, <span class="number">1</span>, N) Fa[i] = i; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y; </span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>) { x = x_; y = y_; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Adj</span> {</span></span><br><span class="line"><span class="keyword">int</span> u, v;</span><br><span class="line"><span class="keyword">double</span> w;</span><br><span class="line"><span class="built_in">Adj</span>(<span class="keyword">int</span> u_ = <span class="number">0</span>, <span class="keyword">int</span> v_ = <span class="number">0</span>, <span class="keyword">double</span> w_ = <span class="number">0</span>) { u = u_; v = v_; w = w_; }</span><br><span class="line"><span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Adj &x, <span class="keyword">const</span> Adj &y) { <span class="keyword">return</span> x.w < y.w; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Uaf<N_*N_> U;</span><br><span class="line">Node P[N_];</span><br><span class="line">Adj E[N_*N_], T[N_*N_];</span><br><span class="line"><span class="keyword">int</span> n, s, cnt, idx, t;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Fr</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> t;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(t--) {</span><br><span class="line"><span class="built_in">FL</span>(P, <span class="number">0</span>); <span class="built_in">FL</span>(Vv, <span class="number">0</span>); cnt = idx = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">FL</span>(T, <span class="number">0</span>); <span class="built_in">FL</span>(E, <span class="number">0</span>); U.<span class="built_in">Cl</span>();</span><br><span class="line"></span><br><span class="line">IN >> s >> n;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> P[i].x >> P[i].y;</span><br><span class="line"><span class="keyword">if</span>(s >= n) { <span class="built_in">printf</span>(<span class="string">"%.2lf\n"</span>, <span class="number">0.00</span>); <span class="keyword">continue</span>; }</span><br><span class="line"></span><br><span class="line">s = n - s;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) </span><br><span class="line">E[cnt++] = <span class="built_in">Adj</span>(i, v, <span class="built_in">Fr</span>(P[i].x, P[i].y, P[v].x, P[v].y) );</span><br><span class="line"></span><br><span class="line"><span class="built_in">sort</span>(E, E+cnt);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, cnt) {</span><br><span class="line"><span class="keyword">int</span> xFa = U.<span class="built_in">Fd</span>(E[i].u), yFa = U.<span class="built_in">Fd</span>(E[i].v);</span><br><span class="line"><span class="keyword">if</span>(xFa != yFa) {</span><br><span class="line">s--;U.<span class="built_in">Un</span>(xFa, yFa);</span><br><span class="line"><span class="keyword">if</span>(!s) { <span class="built_in">printf</span>(<span class="string">"%.2lf\n"</span>, E[i].w); <span class="keyword">break</span>; }</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Fr</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> <span class="built_in">sqrt</span>(<span class="built_in">pow</span>(x-a, <span class="number">2</span>) + <span class="built_in">pow</span>(y-b, <span class="number">2</span>));</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 最小生成树 </tag>
<tag> Kruskal </tag>
</tags>
</entry>
<entry>
<title>AcWing386 社交网络</title>
<link href="/2021/11/14/Acwing386%E7%A4%BE%E4%BA%A4%E7%BD%91%E7%BB%9C/"/>
<url>/2021/11/14/Acwing386%E7%A4%BE%E4%BA%A4%E7%BD%91%E7%BB%9C/</url>
<content type="html"><![CDATA[<h3 id="AcWing386"><a href="#AcWing386" class="headerlink" title="AcWing386"></a><a href="https://www.acwing.com/problem/content/description/388/">AcWing386</a></h3><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><h1 id="开long-long"><a href="#开long-long" class="headerlink" title="开long long"></a>开long long</h1><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e2</span> + <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> E[N_][N_], Cnt[N_][N_];</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"></span><br><span class="line"><span class="built_in">FL</span>(E, <span class="number">0x3f</span>);</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v, w; IN >> u >> v >> w;</span><br><span class="line">E[u][v] = E[v][u] = w;</span><br><span class="line">Cnt[u][v] = Cnt[v][u] = <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) E[i][i] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(k, <span class="number">1</span>, n) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">if</span>(k == i || k == v || i == v) <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> tmp = E[i][k] + E[k][v];</span><br><span class="line"><span class="keyword">if</span>(tmp < E[i][v]) E[i][v] = tmp, Cnt[i][v] = (Cnt[i][k] * Cnt[k][v]);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(tmp == E[i][v]) Cnt[i][v] += (Cnt[i][k] * Cnt[k][v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(k, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">double</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">if</span>(k == i || k == v || i == v) <span class="keyword">continue</span>;</span><br><span class="line">ans += (<span class="keyword">double</span>)(E[i][k] + E[k][v] == E[i][v]) * Cnt[i][k] * Cnt[k][v] / Cnt[i][v];</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%.3lf\n"</span>, ans);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 计数 </tag>
<tag> 全源最短路 </tag>
<tag> Floyd </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title></title>
<link href="/2021/11/13/Test/"/>
<url>/2021/11/13/Test/</url>
<content type="html"><![CDATA[<p>有关 逛公园(UOJ 331) 记忆化搜索, 应该是判0环有问题, UOJ额外数据TLE</p><p>做法:<br>设</p><ul><li>$dis_x$ 为起点到$x$ 的最短路径</li><li>状态 $F_{x, k}$ 表示到节点 $x$, 距离$dis_x + k$的方案数 </li></ul><p>则</p><ul><li>$F_{x, k} = \sum_{(v, x)\in E}F[v][dis(x)+k-dis(v)-w]$, $w$是边$(v, x)$的长度</li></ul><p>建新图连反向边, 记忆化搜索</p><p>对于环的处理: <br>我觉得, 一但路径上出现0环, 答案无穷, 也就是dp状态转移时出现环</p><p>那么, 我在记忆化搜索的过程中 标记正在被处理的点</p><p>一但递归过程中重复访问到一个被标记的点, 即代表路径上有0环, 直接退出输出$-1$</p><p>另外在 $k=0$时可能无法正确判环, 此时额外跑一遍$k=1$</p>]]></content>
</entry>
<entry>
<title>AcWing375 蚂蚁</title>
<link href="/2021/11/12/AcWing375%E8%9A%82%E8%9A%81/"/>
<url>/2021/11/12/AcWing375%E8%9A%82%E8%9A%81/</url>
<content type="html"><![CDATA[<h3 id="AcWing375"><a href="#AcWing375" class="headerlink" title="AcWing375"></a><a href="https://www.acwing.com/problem/content/description/377/">AcWing375</a></h3><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li><h4 id="实数比较"><a href="#实数比较" class="headerlink" title="实数比较"></a>实数比较</h4></li><li><h4 id="曼哈顿距离"><a href="#曼哈顿距离" class="headerlink" title="曼哈顿距离"></a>曼哈顿距离</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">4e2</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> EPS = <span class="number">1e-17</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], I[N], H[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="keyword">double</span> W[M << <span class="number">1</span>];</span><br><span class="line">T C[M << <span class="number">1</span>];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); <span class="built_in">FL</span>(H, <span class="number">0</span>); <span class="built_in">FL</span>(W, <span class="number">0</span>); <span class="built_in">FL</span>(C, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AdI</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, <span class="keyword">int</span> c, <span class="keyword">double</span> w)</span> </span>{</span><br><span class="line">Nt[++cnt] = I[u]; To[cnt] = v; C[cnt] = c; W[I[u] = cnt] = w; </span><br><span class="line">Nt[++cnt] = I[v]; To[cnt] = u; C[cnt] = <span class="number">0</span>; W[I[v] = cnt] = -w; </span><br><span class="line">}</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> {</span> <span class="keyword">int</span> x, y; } A[N_<<<span class="number">1</span>];</span><br><span class="line">Edge<<span class="keyword">int</span>, N_, N_*N_> I;</span><br><span class="line"><span class="keyword">double</span> Far[N_];</span><br><span class="line"><span class="keyword">int</span> n, s, t, ans;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_], Vq[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dis</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Spfa</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="comment">//freopen("I:/Code/Data/WA.out", "w", stdout);</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n<<<span class="number">1</span>) IN >> A[i].x >> A[i].y;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, n+<span class="number">1</span>, n<<<span class="number">1</span>) </span><br><span class="line">I.<span class="built_in">AdI</span>(i, v, <span class="number">1</span>, <span class="built_in">Dis</span>(i, v));</span><br><span class="line"></span><br><span class="line">s = <span class="number">0</span>, t = (n << <span class="number">1</span>) + <span class="number">1</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) I.<span class="built_in">AdI</span>(s, i, <span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">IR</span>(i, n+<span class="number">1</span>, n<<<span class="number">1</span>) I.<span class="built_in">AdI</span>(i, t, <span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="built_in">Spfa</span>()) </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>), <span class="built_in">Dic</span>(s, INF);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i,<span class="number">1</span>,n) <span class="built_in">ER</span>(v,k,I,i,I) <span class="keyword">if</span>(!I.C[v] && k != s) { cout << k-n << <span class="string">"\n"</span>; <span class="keyword">break</span>; }</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dis</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{ <span class="keyword">return</span> <span class="built_in">sqrt</span>( <span class="built_in">pow</span>(A[x].x - A[y].x, <span class="number">2</span>) + <span class="built_in">pow</span>(A[x].y- A[y].y, <span class="number">2</span>) ); }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Spfa</span><span class="params">()</span> </span>{</span><br><span class="line">queue<<span class="keyword">int</span>> Q; Q.<span class="built_in">push</span>(s);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N_) Far[i] = INF;</span><br><span class="line"></span><br><span class="line">Far[s] = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">FL</span>(Vq, <span class="number">0</span>); Vq[s] = <span class="number">1</span>; </span><br><span class="line">I.H[s] = I.I[s];</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(!Q.<span class="built_in">empty</span>()) {</span><br><span class="line"><span class="keyword">int</span> tp = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop</span>();</span><br><span class="line">Vq[tp] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,tp,I) {</span><br><span class="line"><span class="keyword">if</span>(Far[tp] + I.W[i] - Far[v] < EPS && <span class="built_in">abs</span>(Far[tp]+I.W[i]-Far[v]) > EPS && I.C[i]) {</span><br><span class="line">Far[v] = Far[tp] + I.W[i];</span><br><span class="line">I.H[v] = I.I[v];</span><br><span class="line"><span class="keyword">if</span>(!Vq[v]) Q.<span class="built_in">push</span>(v), Vq[v] = <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">abs</span>(Far[t]-INF) > EPS;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(x == t) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">int</span> res = fl; Vv[x] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,H) {</span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">abs</span>(Far[x]+I.W[i]-Far[v])<=EPS && I.C[i] && !Vv[v]) {</span><br><span class="line"><span class="keyword">int</span> k = <span class="built_in">Dic</span>(v, <span class="built_in">min</span>(fl, I.C[i]));</span><br><span class="line"></span><br><span class="line">I.H[x] = i; ans += k * I.W[i];</span><br><span class="line">I.C[i] -= k, I.C[i^<span class="number">1</span>] += k; res -= k;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!res) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">if</span>(!k) Far[v] = <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">Vv[x] = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">return</span> fl - res;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Spfa </tag>
<tag> 二分图 </tag>
<tag> 二分图匹配 </tag>
<tag> 网络流 </tag>
<tag> Dinic </tag>
<tag> 二分图带权匹配 </tag>
<tag> 费用流 </tag>
</tags>
</entry>
<entry>
<title>AcWing379 捉迷藏</title>
<link href="/2021/11/12/AcWing379%E6%8D%89%E8%BF%B7%E8%97%8F/"/>
<url>/2021/11/12/AcWing379%E6%8D%89%E8%BF%B7%E8%97%8F/</url>
<content type="html"><![CDATA[<h3 id="AcWing379"><a href="#AcWing379" class="headerlink" title="AcWing379"></a><a href="https://www.acwing.com/problem/content/381/">AcWing379</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e2</span> + <span class="number">2</span>; </span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">1e4</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[M<<<span class="number">1</span>]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_*N_, M_> I;</span><br><span class="line"><span class="keyword">int</span> Ada[N_*N_];</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_*N_], E[N_][N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">E[u][v] = <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) E[i][i] =<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(k, <span class="number">1</span>, n) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) </span><br><span class="line">E[i][v] |= (E[i][k] && E[k][v]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) </span><br><span class="line"><span class="keyword">if</span>(E[i][v] && i != v) I.<span class="built_in">Ad</span>(i, v+n); </span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>), ans += <span class="built_in">Aug</span>(i);</span><br><span class="line"></span><br><span class="line">cout << n - ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v]=<span class="number">1</span>) && (!Ada[v]||<span class="built_in">Aug</span>(Ada[v]))) </span><br><span class="line"><span class="keyword">return</span> Ada[v] = x;</span><br><span class="line"><span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 二分图 </tag>
<tag> 二分图匹配 </tag>
<tag> 最小路径点覆盖 </tag>
<tag> 增广路算法 </tag>
</tags>
</entry>
<entry>
<title>AcWing377</title>
<link href="/2021/11/12/AcWing377MuddyField/"/>
<url>/2021/11/12/AcWing377MuddyField/</url>
<content type="html"><![CDATA[<h3 id="AcWing377"><a href="#AcWing377" class="headerlink" title="AcWing377"></a><a href="https://www.acwing.com/problem/content/379/">AcWing377</a></h3><h3 id="每个连续行和连续列视为节点"><a href="#每个连续行和连续列视为节点" class="headerlink" title="每个连续行和连续列视为节点"></a>每个连续行和连续列视为节点</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">50</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M<<<span class="number">1</span>], To[M<<<span class="number">1</span>], I[N<<<span class="number">1</span>]; <span class="keyword">int</span> cnt = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_*N_, N_*N_> I;</span><br><span class="line"><span class="keyword">int</span> X[N_*N_], Y[N_*N_], Ada[N_*N_];</span><br><span class="line"><span class="keyword">int</span> n, m, xct, yct;</span><br><span class="line"><span class="keyword">bool</span> Mp[N_][N_], Vv[N_*N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">H</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) { </span><br><span class="line"><span class="built_in">IR</span>(v, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">char</span> ch = <span class="built_in">getchar</span>();</span><br><span class="line">Mp[i][v] = ch == <span class="string">'*'</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">getchar</span>();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, m) </span><br><span class="line"><span class="keyword">if</span>(Mp[i][v]) X[<span class="built_in">H</span>(i, v)] = Mp[i][v<span class="number">-1</span>] ? xct : ++xct;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(v, <span class="number">1</span>, m) <span class="built_in">IR</span>(i, <span class="number">1</span>, n)</span><br><span class="line"><span class="keyword">if</span>(Mp[i][v]) Y[<span class="built_in">H</span>(i, v)] = Mp[i<span class="number">-1</span>][v] ? yct : ++yct; </span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, m) </span><br><span class="line"><span class="keyword">if</span>(Mp[i][v]) I.<span class="built_in">Ad</span>(X[<span class="built_in">H</span>(i, v)], Y[<span class="built_in">H</span>(i, v)]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, xct) </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>), ans += <span class="built_in">Aug</span>(i);</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">H</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{ <span class="keyword">return</span> (x<span class="number">-1</span>)*m + y; }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v] = <span class="number">1</span>) && (!Ada[v] || <span class="built_in">Aug</span>(Ada[v]))) </span><br><span class="line"><span class="keyword">return</span> Ada[v] = x;</span><br><span class="line"><span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 二分图 </tag>
<tag> 二分图匹配 </tag>
<tag> 二分图最小点覆盖 </tag>
<tag> 增广路 </tag>
</tags>
</entry>
<entry>
<title>AcWing376 Machine Schedule</title>
<link href="/2021/11/12/AcWing376MachineSchedule/"/>
<url>/2021/11/12/AcWing376MachineSchedule/</url>
<content type="html"><![CDATA[<h3 id="AcWing376"><a href="#AcWing376" class="headerlink" title="AcWing376"></a><a href="https://www.acwing.com/problem/content/description/378/">AcWing376</a></h3><h3 id="注意AcWing题意和进阶指南不同"><a href="#注意AcWing题意和进阶指南不同" class="headerlink" title="注意AcWing题意和进阶指南不同"></a>注意AcWing题意和进阶指南不同</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q, k) for(int i = p.k[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e2</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">1e3</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], I[M << <span class="number">1</span>]; <span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ad</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = I[u]; To[I[u] = cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_, M_> I;</span><br><span class="line"><span class="keyword">int</span> Ada[M_];</span><br><span class="line"><span class="keyword">int</span> n, m, k;</span><br><span class="line"><span class="keyword">bool</span> Vv[M_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="keyword">while</span>(<span class="number">1</span>) {</span><br><span class="line">IN >> n; </span><br><span class="line"><span class="keyword">if</span>(!n) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">I.<span class="built_in">Cl</span>(); <span class="built_in">FL</span>(Ada, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">IN >> m >> k;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, k) { </span><br><span class="line"><span class="keyword">int</span> u, v, pos; IN >> pos >> u >> v; </span><br><span class="line"><span class="keyword">if</span>(!u || !v) <span class="keyword">continue</span>;</span><br><span class="line">I.<span class="built_in">Ad</span>(u, v+m+n); </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>), ans += <span class="built_in">Aug</span>(i);</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i,v,I,x,I) <span class="keyword">if</span>(!Vv[v] && (Vv[v] = <span class="number">1</span>) && (!Ada[v] || <span class="built_in">Aug</span>(Ada[v])) ) <span class="keyword">return</span> Ada[v] = x;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 二分图 </tag>
<tag> 二分图最小点覆盖 </tag>
<tag> 增广路 </tag>
<tag> 二分图最大匹配 </tag>
</tags>
</entry>
<entry>
<title>AcWing380 舞动的夜晚</title>
<link href="/2021/11/11/AcWing380%E8%88%9E%E5%8A%A8%E7%9A%84%E5%A4%9C%E6%99%9A/"/>
<url>/2021/11/11/AcWing380%E8%88%9E%E5%8A%A8%E7%9A%84%E5%A4%9C%E6%99%9A/</url>
<content type="html"><![CDATA[<h3 id="AcWing380"><a href="#AcWing380" class="headerlink" title="AcWing380"></a><a href="https://www.acwing.com/problem/content/382/">AcWing380</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码:"></a>代码:</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e4</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">1e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Pos[M << <span class="number">1</span>], Hd[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T Val[M << <span class="number">1</span>];</span><br><span class="line"><span class="keyword">int</span> I[N];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); <span class="built_in">FL</span>(Val, <span class="number">0</span>); <span class="built_in">FL</span>(I, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AdI</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w, <span class="keyword">int</span> pos = <span class="number">0</span>)</span> </span>{ Nt[++cnt] = I[u]; Val[cnt] = w; Pos[cnt] = pos; To[I[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N];</span><br><span class="line"><span class="keyword">int</span> tp, num;</span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); tp = num = <span class="number">0</span>; }</span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ num = --tp; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T& x)</span> </span>{ A[num = ++tp] = x; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(A, <span class="number">0</span>); tp = num = <span class="number">0</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Stack<<span class="keyword">int</span>, N_<<<span class="number">1</span> > Sk;</span><br><span class="line">Edge<<span class="keyword">int</span>, N_<<<span class="number">1</span>, E_<<<span class="number">1</span>> G;</span><br><span class="line"><span class="keyword">int</span> D[N_<<<span class="number">1</span>], Ms[N_<<<span class="number">1</span>], Fn[N_<<<span class="number">1</span>], Lw[N_<<<span class="number">1</span>];</span><br><span class="line"><span class="keyword">int</span> n, m, e, s, t, cnt, tot, pos;</span><br><span class="line"><span class="keyword">bool</span> Vk[N_<<<span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Hg</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m >> e;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, e) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">G.<span class="built_in">AdI</span>(u, v+n, <span class="number">1</span>, i); G.<span class="built_in">AdI</span>(v+n, u, <span class="number">0</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) G.<span class="built_in">AdI</span>(<span class="number">0</span>, i, <span class="number">1</span>), G.<span class="built_in">AdI</span>(i, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">t = n + m + <span class="number">1</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, n+<span class="number">1</span>, n+m) G.<span class="built_in">AdI</span>(i, t, <span class="number">1</span>), G.<span class="built_in">AdI</span>(t, i, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="built_in">Hg</span>()) </span><br><span class="line">tot += <span class="built_in">Dic</span>(s, INF);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, n+m+<span class="number">1</span>) G.Hd[i] = G.I[i]; </span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, n+m+<span class="number">1</span>) <span class="keyword">if</span>(!Ms[i]) </span><br><span class="line"><span class="built_in">Tarjan</span>(i), Sk.<span class="built_in">Cl</span>();</span><br><span class="line"></span><br><span class="line">priority_queue<<span class="keyword">int</span>> Q;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">ER</span>(v, k, G, i) </span><br><span class="line"><span class="keyword">if</span>(G.Val[v] && Ms[i] != Ms[k] && G.Pos[v]) </span><br><span class="line">Q.<span class="built_in">push</span>(-G.Pos[v]);</span><br><span class="line"></span><br><span class="line">cout << Q.<span class="built_in">size</span>() << <span class="string">"\n"</span>;</span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) { cout << -Q.<span class="built_in">top</span>() << <span class="string">" "</span>; Q.<span class="built_in">pop</span>(); }</span><br><span class="line">cout << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Hg</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">FL</span>(D, <span class="number">0</span>); D[s] = <span class="number">1</span>;</span><br><span class="line">queue<<span class="keyword">int</span>> Q; Q.<span class="built_in">push</span>(s);</span><br><span class="line">G.Hd[s] = G.I[s];</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) {</span><br><span class="line"><span class="keyword">int</span> x = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop</span>();</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = G.I[x], v = G.To[i]; i; i = G.Nt[i], v = G.To[i]) </span><br><span class="line"><span class="keyword">if</span>(G.Val[i] && !D[v]) {</span><br><span class="line">D[v] = D[x] + <span class="number">1</span>, Q.<span class="built_in">push</span>(v), G.Hd[v] = G.I[v];</span><br><span class="line"><span class="keyword">if</span>(v == t) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dic</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> fl)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(x == t) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">int</span> res = fl;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> &i = G.Hd[x], v = G.To[i]; i; i = G.Nt[i], v = G.To[i]) </span><br><span class="line"><span class="keyword">if</span>(G.Val[i] && D[v] == D[x] + <span class="number">1</span>) {</span><br><span class="line"><span class="keyword">int</span> k = <span class="built_in">Dic</span>(v, <span class="built_in">min</span>(fl, G.Val[i]));</span><br><span class="line">G.Val[i] -= k, G.Val[i^<span class="number">1</span>] += k; res -= k;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!res) <span class="keyword">return</span> fl;</span><br><span class="line"><span class="keyword">if</span>(!k) D[v] = <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> fl - res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Fn[x] = Lw[x] = ++pos;</span><br><span class="line">Sk.<span class="built_in">Ph</span>(x); Vk[x] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(G.Val[i])</span><br><span class="line"><span class="keyword">if</span>(!Fn[v]) <span class="built_in">Tarjan</span>(v), Lw[x] = <span class="built_in">min</span>(Lw[x], Lw[v]);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Vk[v]) Lw[x] = <span class="built_in">min</span>(Lw[x], Fn[v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Fn[x] == Lw[x] && ++cnt) {</span><br><span class="line"><span class="keyword">int</span> y;</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">y = Sk.<span class="built_in">Tp</span>(); Sk.<span class="built_in">Pp</span>(); Vk[y] = <span class="number">0</span>;</span><br><span class="line">Ms[y] = cnt;</span><br><span class="line">} <span class="keyword">while</span>(y != x);</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Tarjan </tag>
<tag> SCC </tag>
<tag> 网络流 </tag>
<tag> 最大流 </tag>
<tag> Dinic </tag>
<tag> 二分图最大匹配 </tag>
<tag> 二分图最大匹配的必须边和可行边 </tag>
</tags>
</entry>
<entry>
<title>CH6802</title>
<link href="/2021/11/10/CH6802%E8%BD%A6%E7%9A%84%E6%94%BE%E7%BD%AE/"/>
<url>/2021/11/10/CH6802%E8%BD%A6%E7%9A%84%E6%94%BE%E7%BD%AE/</url>
<content type="html"><![CDATA[<h3 id="CH6802"><a href="#CH6802" class="headerlink" title="CH6802"></a><a href="http://noi-test.zzstep.com/contest/0x60%E3%80%8C%E5%9B%BE%E8%AE%BA%E3%80%8D%E4%BE%8B%E9%A2%98/6802%20%E8%BB%8A%E7%9A%84%E6%94%BE%E7%BD%AE">CH6802</a></h3><h3 id="每行每列视为一个节点-行为左部-列为右部"><a href="#每行每列视为一个节点-行为左部-列为右部" class="headerlink" title="每行每列视为一个节点, 行为左部, 列为右部"></a>每行每列视为一个节点, 行为左部, 列为右部</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">5e2</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">5e2</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">5e4</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N]; <span class="keyword">int</span> cnt;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = Hd[u]; To[Hd[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_, E_> G;</span><br><span class="line"><span class="keyword">int</span> Ada[N_ + M_];</span><br><span class="line"><span class="keyword">int</span> n, m, e;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_ + M_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m >> e;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, e) { <span class="keyword">int</span> u, v; IN >> u >> v; G.<span class="built_in">Add</span>(u, v + n); }</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>), ans += <span class="built_in">Aug</span>(i);</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Aug</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(!Vv[v] && (Vv[v] = <span class="number">1</span>) && (!Ada[v] || <span class="built_in">Aug</span>(Ada[v]) ) ) <span class="keyword">return</span> Ada[v] = x;</span><br><span class="line"><span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 二分图 </tag>
<tag> 二分图最大匹配 </tag>
<tag> 增广路算法 </tag>
</tags>
</entry>
<entry>
<title>AcWing368 银河</title>
<link href="/2021/11/08/AcWing368%E9%93%B6%E6%B2%B3/"/>
<url>/2021/11/08/AcWing368%E9%93%B6%E6%B2%B3/</url>
<content type="html"><![CDATA[<h3 id="AcWing368"><a href="#AcWing368" class="headerlink" title="AcWing368"></a><a href="https://www.acwing.com/problem/content/370/">AcWing368</a></h3><h3 id="考虑每个SCC的边权和一定-0-否则无解"><a href="#考虑每个SCC的边权和一定-0-否则无解" class="headerlink" title="考虑每个SCC的边权和一定=0, 否则无解"></a>考虑每个SCC的边权和一定=0, 否则无解</h3><h3 id="Tarjan缩点-DAG上拓扑排序dp求最长路-然而缩点之后SPFA就可以卡过去"><a href="#Tarjan缩点-DAG上拓扑排序dp求最长路-然而缩点之后SPFA就可以卡过去" class="headerlink" title="Tarjan缩点, DAG上拓扑排序dp求最长路, 然而缩点之后SPFA就可以卡过去"></a>Tarjan缩点, DAG上拓扑排序dp求最长路, 然而缩点之后SPFA就可以卡过去</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码:"></a>代码:</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> int long long</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N]; <span class="keyword">int</span> tp, num;</span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); tp = num = <span class="number">0</span>; }</span><br><span class="line"></span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T &x)</span> </span>{ A[num = ++tp] = x; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ num = --tp; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T Val[M << <span class="number">1</span>];</span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w)</span> </span>{ Nt[++cnt] = Hd[u]; Val[cnt] = w; To[Hd[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>) { x = x_; y = y_; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Stack<<span class="keyword">int</span>, N_> Sk;</span><br><span class="line">Edge<<span class="keyword">int</span>, N_, M_> G, S;</span><br><span class="line">vector<<span class="keyword">int</span>> Scc[N_];</span><br><span class="line"><span class="keyword">int</span> Far[N_], Low[N_], Dfn[N_], Mscc[N_], Sscc[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, ns, pos, cnt;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_], Vsk[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">(<span class="keyword">const</span> Edge<T, N, M>& G, <span class="keyword">int</span> s)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">signed</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> opt, a, b; IN >> opt >> a >> b;</span><br><span class="line"><span class="built_in"><span class="keyword">switch</span></span>(opt) {</span><br><span class="line"><span class="keyword">case</span> <span class="number">1</span>: G.<span class="built_in">Add</span>(b, a ,<span class="number">0</span>); G.<span class="built_in">Add</span>(a, b, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">case</span> <span class="number">2</span>: G.<span class="built_in">Add</span>(a, b, <span class="number">1</span>); <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">case</span> <span class="number">3</span>: G.<span class="built_in">Add</span>(b, a, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">case</span> <span class="number">4</span>: G.<span class="built_in">Add</span>(b, a, <span class="number">1</span>); <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">case</span> <span class="number">5</span>: G.<span class="built_in">Add</span>(a, b, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) G.<span class="built_in">Add</span>(<span class="number">0</span>, i, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, n) <span class="built_in">ER</span>(v, k, G, i) </span><br><span class="line"><span class="keyword">if</span>(Mscc[i] != Mscc[k]) </span><br><span class="line">S.<span class="built_in">Add</span>(Mscc[i], Mscc[k], G.Val[v]);</span><br><span class="line"><span class="keyword">else</span> Sscc[Mscc[i]] += G.Val[v];</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) <span class="keyword">if</span>(Sscc[i] > <span class="number">0</span> && Scc[i].<span class="built_in">size</span>() > <span class="number">1</span>) { cout << <span class="string">"-1\n"</span>; <span class="keyword">return</span> <span class="number">0</span>; }</span><br><span class="line"></span><br><span class="line"><span class="built_in">Spfa</span>(S, Mscc[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) </span><br><span class="line">ans += Far[i] * Scc[i].<span class="built_in">size</span>();</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Low[x] = Dfn[x] = ++pos;</span><br><span class="line"></span><br><span class="line">Sk.<span class="built_in">Ph</span>(x); Vsk[x] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) </span><br><span class="line"><span class="keyword">if</span>(!Dfn[v]) <span class="built_in">Tarjan</span>(v), Low[x] = <span class="built_in">min</span>(Low[x], Low[v]);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Vsk[v]) Low[x] = <span class="built_in">min</span>(Low[x], Dfn[v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Dfn[x] == Low[x]) {</span><br><span class="line">cnt++;</span><br><span class="line"><span class="keyword">int</span> y;</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">y = Sk.<span class="built_in">Tp</span>(); Sk.<span class="built_in">Pp</span>();</span><br><span class="line">Scc[cnt].<span class="built_in">push_back</span>(y);</span><br><span class="line">Mscc[y] = cnt;Vsk[y] = <span class="number">0</span>;</span><br><span class="line">}<span class="keyword">while</span>(y != x);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">(<span class="keyword">const</span> Edge<T, N, M> &G, <span class="keyword">int</span> s)</span> </span>{</span><br><span class="line">queue<<span class="keyword">int</span>> Q; Q.<span class="built_in">push</span>(s); </span><br><span class="line"><span class="built_in">FL</span>(Vv, <span class="number">0</span>); Vv[s] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">FL</span>(Far, <span class="number">0xcf</span>); Far[s] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) {</span><br><span class="line"><span class="keyword">int</span> tp = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop</span>();</span><br><span class="line">Vv[tp] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, G, tp) <span class="keyword">if</span>(Far[v] < Far[tp] + G.Val[i]) {</span><br><span class="line"><span class="keyword">if</span>(!Vv[v]) Q.<span class="built_in">push</span>(v), Vv[v] = <span class="number">1</span>;</span><br><span class="line">Far[v] = Far[tp] + G.Val[i];</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Tarjan </tag>
<tag> 缩点 </tag>
<tag> SCC </tag>
<tag> 差分约束 </tag>
<tag> SPFA </tag>
<tag> 最长路 </tag>
<tag> 拓扑排序 </tag>
</tags>
</entry>
<entry>
<title>AcWing362 Intervals</title>
<link href="/2021/11/07/AcWing362Intervals/"/>
<url>/2021/11/07/AcWing362Intervals/</url>
<content type="html"><![CDATA[<h3 id="AcWing362"><a href="#AcWing362" class="headerlink" title="AcWing362"></a><a href="https://www.acwing.com/problem/content/description/364/">AcWing362</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">5e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">25e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N]; <span class="keyword">int</span> cnt;</span><br><span class="line">T Val[M << <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); <span class="built_in">FL</span>(Val, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, T w)</span> </span>{ Nt[++cnt] = Hd[u]; To[cnt] = v; Val[Hd[u] = cnt] = w; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<<span class="keyword">int</span>, N_, M_> G;</span><br><span class="line"><span class="keyword">int</span> Far[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, mx;</span><br><span class="line"><span class="keyword">bool</span> Vv[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">int</span> u, v, w; IN >> u >> v >> w;</span><br><span class="line">G.<span class="built_in">Add</span>(u, ++v, w);</span><br><span class="line">mx = <span class="built_in">max</span>(mx, <span class="built_in">max</span>(v+<span class="number">1</span>, u+<span class="number">1</span>));</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, mx) G.<span class="built_in">Add</span>(i, i+<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, mx) G.<span class="built_in">Add</span>(i, i<span class="number">-1</span>, <span class="number">-1</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Spfa</span>();</span><br><span class="line"></span><br><span class="line">cout << Far[mx] << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Spfa</span><span class="params">()</span> </span>{</span><br><span class="line">queue<<span class="keyword">int</span>> Q; Q.<span class="built_in">push</span>(<span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Far, <span class="number">0xcf</span>); Far[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(Q.<span class="built_in">size</span>()) {</span><br><span class="line"><span class="keyword">int</span> tp = Q.<span class="built_in">front</span>(); Q.<span class="built_in">pop</span>();</span><br><span class="line">Vv[tp] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, G, tp) </span><br><span class="line"><span class="keyword">if</span>(Far[tp] + G.Val[i] > Far[v]) {</span><br><span class="line">Far[v] = Far[tp] + G.Val[i];</span><br><span class="line"><span class="keyword">if</span>(!Vv[v]) Q.<span class="built_in">push</span>(v), Vv[v] = <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> Spfa </tag>
<tag> 差分约束 </tag>
</tags>
</entry>
<entry>
<title>AcWing367 学校网络</title>
<link href="/2021/11/07/AcWing367NetWorkofSchools/"/>
<url>/2021/11/07/AcWing367NetWorkofSchools/</url>
<content type="html"><![CDATA[<h3 id="AcWing367"><a href="#AcWing367" class="headerlink" title="AcWing367"></a><a href="https://www.acwing.com/problem/content/description/369/">AcWing367</a></h3><h3 id="设DAG中入度为零的点的数量为p-出度为0的点的数量为q-则至少添加max-p-q-条有向边可以使得整张图强连通"><a href="#设DAG中入度为零的点的数量为p-出度为0的点的数量为q-则至少添加max-p-q-条有向边可以使得整张图强连通" class="headerlink" title="设DAG中入度为零的点的数量为p, 出度为0的点的数量为q, 则至少添加max(p, q) 条有向边可以使得整张图强连通"></a>设DAG中入度为零的点的数量为p, 出度为0的点的数量为q, 则至少添加max(p, q) 条有向边可以使得整张图强连通</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">100</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = Hd[u]; To[Hd[u] = cnt] = v; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N]; <span class="keyword">int</span> num, tp;</span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); num = tp = <span class="number">0</span>; }</span><br><span class="line"></span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T &x)</span> </span>{ A[num = ++tp] = x; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ num = --tp; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(A, <span class="number">0</span>); num = tp = <span class="number">0</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Stack<<span class="keyword">int</span>, N_> Sk;</span><br><span class="line">Edge<N_, N_ * N_> G;</span><br><span class="line"><span class="keyword">int</span> Mc[N_], Low[N_], Dfn[N_], In[N_], Out[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, cnt, pos;</span><br><span class="line"><span class="keyword">bool</span> Vsk[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">int</span> v; IN >> v;</span><br><span class="line"><span class="keyword">while</span>(v) { G.<span class="built_in">Add</span>(i, v); IN >> v; }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Mc[i])</span><br><span class="line"><span class="built_in">Tarjan</span>(i), Sk.<span class="built_in">Cl</span>();</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">ER</span>(k, v, G, i) <span class="keyword">if</span>(Mc[i] != Mc[v]) </span><br><span class="line">In[Mc[v]]++, Out[Mc[i]]++;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(cnt == <span class="number">1</span>) { cout << <span class="string">"1\n0"</span>; <span class="keyword">return</span> <span class="number">0</span>; }</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>, tmp = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) ans += !In[i];</span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) tmp += !Out[i]; </span><br><span class="line">cout << <span class="built_in">max</span>(ans, tmp) << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Low[x] = Dfn[x] = ++pos;</span><br><span class="line">Sk.<span class="built_in">Ph</span>(x); Vsk[x] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) </span><br><span class="line"><span class="keyword">if</span>(!Dfn[v])<span class="built_in">Tarjan</span>(v), Low[x] = <span class="built_in">min</span>(Low[x], Low[v]);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Vsk[v]) Low[x] = <span class="built_in">min</span>(Low[x], Dfn[v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Low[x] == Dfn[x]) {</span><br><span class="line">cnt++;</span><br><span class="line"><span class="keyword">while</span>(Sk.<span class="built_in">Tp</span>() != x) </span><br><span class="line">Mc[Sk.<span class="built_in">Tp</span>()] = cnt, Vsk[Sk.<span class="built_in">Tp</span>()] = <span class="number">0</span>, Sk.<span class="built_in">Pp</span>();</span><br><span class="line">Mc[x] = cnt, Vsk[x] = <span class="number">0</span>, Sk.<span class="built_in">Pp</span>(); </span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Tarjan </tag>
<tag> 缩点 </tag>
<tag> SCC </tag>
</tags>
</entry>
<entry>
<title>Acwing366 Watch Cow</title>
<link href="/2021/11/07/AcWing366WatchCow/"/>
<url>/2021/11/07/AcWing366WatchCow/</url>
<content type="html"><![CDATA[<h3 id="AcWing366"><a href="#AcWing366" class="headerlink" title="AcWing366"></a><a href="https://www.acwing.com/problem/content/description/368/">AcWing366</a></h3><h3 id="根据邻接表的存储顺序-一条无向边的”回边”一定最先被放入一个点"><a href="#根据邻接表的存储顺序-一条无向边的”回边”一定最先被放入一个点" class="headerlink" title="根据邻接表的存储顺序, 一条无向边的”回边”一定最先被放入一个点"></a>根据邻接表的存储顺序, 一条无向边的”回边”一定最先被放入一个点</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码:"></a>代码:</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e4</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">5e4</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N]; <span class="keyword">int</span> tp, sum;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); tp = sum = <span class="number">0</span>; }</span><br><span class="line"></span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Num</span><span class="params">()</span> </span>{ <span class="keyword">return</span> sum; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T &x)</span> </span>{ A[sum = ++tp] = x; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ sum = --tp; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = Hd[u]; To[Hd[u] = cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_, M_> G;</span><br><span class="line">Stack<<span class="keyword">int</span>, M_ << <span class="number">1</span>> Sk;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Euler</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) { <span class="keyword">int</span> u, v; IN >> u >> v; G.<span class="built_in">Add</span>(u, v); G.<span class="built_in">Add</span>(v, u); }</span><br><span class="line"></span><br><span class="line"><span class="built_in">Euler</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">cout << <span class="number">1</span> << <span class="string">"\n"</span>;</span><br><span class="line"><span class="keyword">while</span>(Sk.<span class="built_in">Num</span>()) cout << Sk.<span class="built_in">Tp</span>() << <span class="string">"\n"</span>, Sk.<span class="built_in">Pp</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Euler</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = G.Hd[x], v = G.To[i]; i; i = G.Hd[x], v = G.To[i])</span><br><span class="line">G.Hd[x] = G.Nt[i],</span><br><span class="line"><span class="built_in">Euler</span>(v), Sk.<span class="built_in">Ph</span>(v);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 图论 </tag>
<tag> Tarjan </tag>
<tag> 欧拉路 </tag>
</tags>
</entry>
<entry>
<title>AcWing365圆桌骑士</title>
<link href="/2021/11/07/AcWing365KnightoftheRoundTable/"/>
<url>/2021/11/07/AcWing365KnightoftheRoundTable/</url>
<content type="html"><![CDATA[<h3 id="AcWing365"><a href="#AcWing365" class="headerlink" title="AcWing365"></a><a href="https://www.acwing.com/problem/content/367/">AcWing365</a></h3><h3 id="建补图-求v-DCC-对于每一个v-DCC-判断是否有奇环"><a href="#建补图-求v-DCC-对于每一个v-DCC-判断是否有奇环" class="headerlink" title="建补图, 求v-DCC, 对于每一个v-DCC, 判断是否有奇环"></a>建补图, 求v-DCC, 对于每一个v-DCC, 判断是否有奇环</h3><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li><h4 id="一个割点可能被包含于多个v-DCC-染色判奇环时注意清空数组"><a href="#一个割点可能被包含于多个v-DCC-染色判奇环时注意清空数组" class="headerlink" title="一个割点可能被包含于多个v-DCC, 染色判奇环时注意清空数组"></a>一个割点可能被包含于多个v-DCC, 染色判奇环时注意清空数组</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, v, k) for(int i = v; i <= k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, v, k) for(int i = v; i < k; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, v, k) for(int i = v; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, v, k) for(int i = v-1; i >= k; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">1e6</span> + <span class="number">2</span>; </span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stack</span> {</span></span><br><span class="line">T A[N];</span><br><span class="line"><span class="keyword">int</span> cnt, tp;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Stack</span>() { <span class="built_in">FL</span>(A, <span class="number">0</span>); cnt = <span class="number">0</span>; tp = <span class="number">1</span>; }</span><br><span class="line"></span><br><span class="line"><span class="function">T <span class="title">Tp</span><span class="params">()</span> </span>{ <span class="keyword">return</span> A[tp]; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Num</span><span class="params">()</span> </span>{ <span class="keyword">return</span> cnt; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pp</span><span class="params">()</span> </span>{ tp--; cnt--; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Ph</span><span class="params">(<span class="keyword">const</span> T &x)</span> </span>{ A[++tp] = x; cnt++; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(A, <span class="number">0</span>); cnt = <span class="number">0</span>; tp = <span class="number">0</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Edge</span>() { <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{ Nt[++cnt] = Hd[u]; To[Hd[u] = cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{ <span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>); cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Stack<<span class="keyword">int</span>, N_> Sk;</span><br><span class="line">Edge<N_, M_> G;</span><br><span class="line">vector<<span class="keyword">int</span>> Dcc[N_];</span><br><span class="line"><span class="keyword">int</span> Dfn[N_], Low[N_], Cl[N_];</span><br><span class="line"><span class="keyword">int</span> n, m, cnt;</span><br><span class="line"><span class="keyword">bool</span> Ve[N_][N_], Vv[N_], Vc[N_][N_], Vl[N_], Ct[N_], Fg[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y, <span class="keyword">int</span> rt)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> pos, <span class="keyword">bool</span> clo)</span></span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ </span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(n + m) {</span><br><span class="line"><span class="built_in">FL</span>(Dfn, <span class="number">0</span>); <span class="built_in">FL</span>(Low, <span class="number">0</span>); <span class="built_in">FL</span>(Ct, <span class="number">0</span>); <span class="built_in">FL</span>(Cl, <span class="number">0x3f</span>);</span><br><span class="line"><span class="built_in">FL</span>(Ve, <span class="number">0</span>); <span class="built_in">FL</span>(Vv, <span class="number">0</span>); <span class="built_in">FL</span>(Vc, <span class="number">0</span>); <span class="built_in">FL</span>(Vl, <span class="number">0</span>); <span class="built_in">FL</span>(Fg, <span class="number">0</span>);</span><br><span class="line">G.<span class="built_in">Cl</span>();</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) { <span class="keyword">int</span> u, v; IN >> u >> v; Ve[u][v] = <span class="number">1</span>; Ve[v][u] = <span class="number">1</span>; }</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, i+<span class="number">1</span>, n) <span class="keyword">if</span>(!Ve[i][v]) </span><br><span class="line">G.<span class="built_in">Add</span>(i, v), G.<span class="built_in">Add</span>(v, i);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Vv[i]) </span><br><span class="line"><span class="built_in">Tarjan</span>(i, <span class="number">1</span>, i), Sk.<span class="built_in">Cl</span>();</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> Debug</span></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) {</span><br><span class="line">cout << <span class="string">"\n=======================\n"</span>;</span><br><span class="line"><span class="built_in">Ir</span>(v, <span class="number">0</span>, Dcc[i].<span class="built_in">size</span>()) </span><br><span class="line">cout << Dcc[i][v] << <span class="string">" "</span>;</span><br><span class="line">cout << <span class="string">"\n=======================\n"</span>;</span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) </span><br><span class="line"><span class="built_in">Pre</span>(Dcc[i][<span class="number">0</span>], i, <span class="number">0</span>), <span class="built_in">FL</span>(Cl, <span class="number">0x3f</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, cnt) <span class="keyword">if</span>(Fg[i]) </span><br><span class="line"><span class="built_in">Ir</span>(v, <span class="number">0</span>, Dcc[i].<span class="built_in">size</span>()) Vl[Dcc[i][v]] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line">ans += !Vl[i];</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line">IN >> n >> m;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> pos, <span class="keyword">int</span> rt)</span> </span>{</span><br><span class="line">Dfn[x] = Low[x] = pos; </span><br><span class="line">Vv[x] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">Sk.<span class="built_in">Ph</span>(x);</span><br><span class="line"><span class="keyword">int</span> tot = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) </span><br><span class="line"><span class="keyword">if</span>(!Dfn[v]) {</span><br><span class="line"><span class="built_in">Tarjan</span>(v, pos+<span class="number">1</span>, rt);</span><br><span class="line">Low[x] = <span class="built_in">min</span>(Low[x], Low[v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Low[v] >= Dfn[x]) {</span><br><span class="line">Ct[x] = ++tot > <span class="number">1</span> || x != rt;</span><br><span class="line"></span><br><span class="line">cnt++;</span><br><span class="line"><span class="keyword">while</span>(Sk.<span class="built_in">Num</span>() && Sk.<span class="built_in">Tp</span>() != v) </span><br><span class="line">Vc[cnt][Sk.<span class="built_in">Tp</span>()] = <span class="number">1</span>, </span><br><span class="line">Dcc[cnt].<span class="built_in">push_back</span>(Sk.<span class="built_in">Tp</span>()), </span><br><span class="line">Sk.<span class="built_in">Pp</span>(); </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Sk.<span class="built_in">Num</span>()) </span><br><span class="line">Vc[cnt][Sk.<span class="built_in">Tp</span>()] = <span class="number">1</span>, </span><br><span class="line">Dcc[cnt].<span class="built_in">push_back</span>(Sk.<span class="built_in">Tp</span>()), </span><br><span class="line">Sk.<span class="built_in">Pp</span>();</span><br><span class="line"></span><br><span class="line">Vc[cnt][x] = <span class="number">1</span>; Dcc[cnt].<span class="built_in">push_back</span>(x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> Low[x] = <span class="built_in">min</span>(Low[x], Dfn[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> pos, <span class="keyword">bool</span> clo)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(Fg[pos]) <span class="keyword">return</span> ;</span><br><span class="line"></span><br><span class="line">Cl[x] = clo;</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(Vc[pos][v]) </span><br><span class="line"><span class="keyword">if</span>(Cl[v] == INF) <span class="built_in">Pre</span>(v, pos, clo^<span class="number">1</span>);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Cl[v] == clo) { Fg[pos] = <span class="number">1</span>; <span class="keyword">return</span> ; }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Tarjan </tag>
<tag> v-DCC </tag>
<tag> 奇环 </tag>
<tag> 补图 </tag>
</tags>
</entry>
<entry>
<title>AcWing364 网络</title>
<link href="/2021/11/06/AcWing366NetWork/"/>
<url>/2021/11/06/AcWing366NetWork/</url>
<content type="html"><![CDATA[<h3 id="AcWing365"><a href="#AcWing365" class="headerlink" title="AcWing365"></a><a href="https://www.acwing.com/problem/content/366/">AcWing365</a></h3><h3 id="缩点LCA-并查集优化"><a href="#缩点LCA-并查集优化" class="headerlink" title="缩点LCA, 并查集优化"></a>缩点LCA, 并查集优化</h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, p, q) for(int i = p.Hd[q], v = p.To[i]; i; i = p.Nt[i], v = p.To[i])</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> FL(x, y) memset(x, y, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">2e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">20</span> + <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Uaf</span> {</span></span><br><span class="line"><span class="keyword">int</span> Fa[N];</span><br><span class="line"><span class="built_in">Uaf</span>() {</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N) Fa[i] = i; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Fd</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> Fa[x] = x == Fa[x] ? x : <span class="built_in">Fd</span>(Fa[x]); }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Un</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line">Fa[<span class="built_in">Fd</span>(x)] = <span class="built_in">Fd</span>(y); }</span><br><span class="line"><span class="keyword">int</span>& <span class="keyword">operator</span>[](<span class="keyword">const</span> <span class="keyword">int</span> &x) {</span><br><span class="line"><span class="keyword">return</span> Fa[x]; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N, <span class="keyword">int</span> M></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Nt[M << <span class="number">1</span>], To[M << <span class="number">1</span>], Hd[N]; </span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"><span class="built_in">Edge</span>() {</span><br><span class="line"><span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>);</span><br><span class="line">cnt = <span class="number">1</span>; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{</span><br><span class="line">Nt[++cnt] = Hd[u]; To[Hd[u] = cnt] = v; }</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cl</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">FL</span>(Nt, <span class="number">0</span>); <span class="built_in">FL</span>(To, <span class="number">0</span>); <span class="built_in">FL</span>(Hd, <span class="number">0</span>);</span><br><span class="line">cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_, M_> G, S;</span><br><span class="line">Uaf<N_> U;</span><br><span class="line"><span class="keyword">int</span> Dfn[N_], Low[N_], Dcc[N_], F[N_][E_], Dep[N_];</span><br><span class="line"><span class="keyword">int</span> T, n, m, cnt, sum, q, idx;</span><br><span class="line"><span class="keyword">bool</span> Bg[M_ << <span class="number">1</span>], Vis[N_], Mark[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last, <span class="keyword">int</span> pos)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Fdcc</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Mk</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"></span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line">G.<span class="built_in">Cl</span>(); S.<span class="built_in">Cl</span>(); sum = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">FL</span>(Dfn, <span class="number">0</span>); <span class="built_in">FL</span>(Low, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Vis, <span class="number">0</span>); <span class="built_in">FL</span>(Bg, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">FL</span>(Mark, <span class="number">0</span>); cnt = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">G.<span class="built_in">Add</span>(u, v); G.<span class="built_in">Add</span>(v, u);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Vis[i]) cnt++, <span class="built_in">Fdcc</span>(i);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= G.cnt; i+=<span class="number">2</span>)</span><br><span class="line"><span class="keyword">if</span>(Bg[i]) {</span><br><span class="line"><span class="keyword">int</span> u = Dcc[G.To[i]], v = Dcc[G.To[i^<span class="number">1</span>]];</span><br><span class="line">S.<span class="built_in">Add</span>(u, v); S.<span class="built_in">Add</span>(v, u);</span><br><span class="line">sum++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Dep[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">Pre</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">Dp</span>();</span><br><span class="line"></span><br><span class="line">cout << <span class="string">"Case "</span> << ++idx << <span class="string">":"</span> << <span class="string">"\n"</span>;</span><br><span class="line">IN >> q;</span><br><span class="line"><span class="keyword">while</span>(q--) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line"><span class="keyword">if</span>(Dcc[u] == Dcc[v]) cout << sum << <span class="string">"\n"</span>;</span><br><span class="line"><span class="keyword">else</span> cout << (sum -= <span class="built_in">Mk</span>(Dcc[u], Dcc[v])) << <span class="string">"\n"</span>;</span><br><span class="line">}</span><br><span class="line">cout << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line">IN >> n >> m;</span><br><span class="line"></span><br><span class="line">} <span class="keyword">while</span>(n + m);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last, <span class="keyword">int</span> pos)</span> </span>{</span><br><span class="line">Low[x] = Dfn[x] = pos;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(i != (last ^ <span class="number">1</span>))</span><br><span class="line"><span class="keyword">if</span>(!Dfn[v]) </span><br><span class="line"><span class="built_in">Tarjan</span>(v, i, pos+<span class="number">1</span>), Low[x] = <span class="built_in">min</span>(Low[x], Low[v]),</span><br><span class="line">Bg[i] = Bg[i^<span class="number">1</span>] = (Bg[i] | (Dfn[x] < Low[v]));</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">Low[x] = <span class="built_in">min</span>(Low[x], Dfn[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Fdcc</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line">Vis[x] = <span class="number">1</span>; Dcc[x] = cnt;</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(!Bg[i] && !Vis[v]) <span class="built_in">Fdcc</span>(v);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Dp</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(e, <span class="number">1</span>, E_) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line">F[i][e] = F[F[i][e<span class="number">-1</span>]][e<span class="number">-1</span>];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Lca</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(Dep[x] < Dep[y]) <span class="built_in">swap</span>(x, y);</span><br><span class="line"><span class="built_in">SR</span>(e, E_<span class="number">-1</span>, <span class="number">0</span>) <span class="keyword">if</span>(Dep[F[x][e]] >= Dep[y]) x = F[x][e];</span><br><span class="line"><span class="keyword">if</span>(x == y) <span class="keyword">return</span> x;</span><br><span class="line"></span><br><span class="line"><span class="built_in">SR</span>(e, E_<span class="number">-1</span>, <span class="number">0</span>) <span class="keyword">if</span>(F[x][e] != F[y][e]) </span><br><span class="line">x = F[x][e], y = F[y][e];</span><br><span class="line"><span class="keyword">return</span> F[x][<span class="number">0</span>];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line">F[x][<span class="number">0</span>] = last;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, v, S, x) <span class="keyword">if</span>(v != last) </span><br><span class="line">Dep[v] = Dep[x] + <span class="number">1</span>, <span class="built_in">Pre</span>(v, x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Mk</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{</span><br><span class="line"><span class="keyword">int</span> res = <span class="number">0</span>, lca = <span class="built_in">Lca</span>(u, v);</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> Debug</span></span><br><span class="line">cout << <span class="string">"\n=======================\n"</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"u = %d, v = %d, lca = %d\n"</span>, u, v, lca);</span><br><span class="line">cout << <span class="string">"\n=======================\n"</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(u != lca) {</span><br><span class="line">res += !Mark[u]; Mark[u] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> fa = U.<span class="built_in">Fd</span>(u);</span><br><span class="line"><span class="keyword">if</span>(F[u][<span class="number">0</span>] == lca) <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">if</span>(fa == u) u = F[u][<span class="number">0</span>], U.<span class="built_in">Un</span>(fa, u);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Dep[fa] <= Dep[lca]) <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">else</span> u = fa;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(v != lca) {</span><br><span class="line">res += !Mark[v]; Mark[v] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> fa = U.<span class="built_in">Fd</span>(v);</span><br><span class="line"><span class="keyword">if</span>(F[v][<span class="number">0</span>] == lca) <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">if</span>(fa == v) v = F[v][<span class="number">0</span>], U.<span class="built_in">Un</span>(fa, v);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(Dep[fa] <= Dep[lca]) <span class="keyword">break</span>;</span><br><span class="line"><span class="keyword">else</span> v = fa;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> res;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> LCA </tag>
<tag> Tarjan </tag>
<tag> e-DCC </tag>
<tag> 缩点 </tag>
</tags>
</entry>
<entry>
<title></title>
<link href="/2021/11/04/Tem/"/>
<url>/2021/11/04/Tem/</url>
<content type="html"><![CDATA[<h3 id="lt-gt"><a href="#lt-gt" class="headerlink" title="<++>"></a><a href="++"><++></a></h3>]]></content>
<tags>
<tag> <++> </tag>
</tags>
</entry>
<entry>
<title>AcWing289 环路运输</title>
<link href="/2021/11/03/AcWing289_%E7%8E%AF%E8%B7%AF%E8%BF%90%E8%BE%93/"/>
<url>/2021/11/03/AcWing289_%E7%8E%AF%E8%B7%AF%E8%BF%90%E8%BE%93/</url>
<content type="html"><![CDATA[<h3 id="AcWing289"><a href="#AcWing289" class="headerlink" title="AcWing289"></a><a href="https://www.acwing.com/problem/content/291/">AcWing289</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e6</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> val, pos;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> val_ = <span class="number">0</span>, <span class="keyword">int</span> pos_ = <span class="number">0</span>) {</span><br><span class="line">val = val_, pos = pos_;</span><br><span class="line">}</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> A[N_];</span><br><span class="line"><span class="keyword">int</span> n, ans;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> A[i];</span><br><span class="line"></span><br><span class="line">deque<Node> Q;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> S Q.size()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> F Q.front()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> E Q.back()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PF Q.pop_front()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PE Q.pop_back()</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> I (i % n ? i % n : n)</span></span><br><span class="line"></span><br><span class="line">ans = -INF;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, <span class="number">2</span> * n) {</span><br><span class="line"><span class="keyword">while</span>(S && F.pos < i - n / <span class="number">2</span>) PF;</span><br><span class="line"></span><br><span class="line">ans = <span class="built_in">max</span>(ans, S ? F.val + A[I] + i : <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(S && E.val <= A[I] - i) PE;</span><br><span class="line"></span><br><span class="line">Q.<span class="built_in">push_back</span>(<span class="built_in">Node</span>(A[I] - i, i));</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> dp </tag>
<tag> 环形 </tag>
</tags>
</entry>
<entry>
<title>AcWing356 严格次小生成树</title>
<link href="/2021/11/02/AcWing356_%E4%B8%A5%E6%A0%BC%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/"/>
<url>/2021/11/02/AcWing356_%E4%B8%A5%E6%A0%BC%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/</url>
<content type="html"><![CDATA[<h3 id="AcWing356"><a href="#AcWing356" class="headerlink" title="AcWing356"></a><a href="https://www.acwing.com/problem/content/358/">AcWing356</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><h4 id="求出最小生成树-考虑将一条生成树上的边换成非树边"><a href="#求出最小生成树-考虑将一条生成树上的边换成非树边" class="headerlink" title="求出最小生成树, 考虑将一条生成树上的边换成非树边"></a>求出最小生成树, 考虑将一条生成树上的边换成非树边</h4><h4 id="一条非树边可以替换其组成的环上的任意边"><a href="#一条非树边可以替换其组成的环上的任意边" class="headerlink" title="一条非树边可以替换其组成的环上的任意边"></a>一条非树边可以替换其组成的环上的任意边</h4><h4 id="考虑更新环上的次大值和最大值"><a href="#考虑更新环上的次大值和最大值" class="headerlink" title="考虑更新环上的次大值和最大值"></a>考虑更新环上的次大值和最大值</h4><h4 id="树上倍增求两点到其LCA的最大值和次大值"><a href="#树上倍增求两点到其LCA的最大值和次大值" class="headerlink" title="树上倍增求两点到其LCA的最大值和次大值"></a>树上倍增求两点到其LCA的最大值和次大值</h4><ul><li><h5 id="P-x-i-x的2-i-级祖先"><a href="#P-x-i-x的2-i-级祖先" class="headerlink" title="P[x][i], x的2^i 级祖先"></a>P[x][i], x的2^i 级祖先</h5></li><li><h5 id="F-x-i-0-1-x到其2-i-级祖先的链上的最大值-次大值"><a href="#F-x-i-0-1-x到其2-i-级祖先的链上的最大值-次大值" class="headerlink" title="F[x][i][0/1] x到其2^i 级祖先的链上的最大值/次大值"></a>F[x][i][0/1] x到其2^i 级祖先的链上的最大值/次大值</h5></li></ul><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li>判断是否越界 (if P[x][i]])</li><li>INF 足够大</li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">3e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> E_ = <span class="number">20</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">long</span> <span class="keyword">long</span> INF = <span class="number">0x7f7f7f7f7f7f7f7f</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> next, to, val;</span><br><span class="line"><span class="built_in">Edge</span>(<span class="keyword">int</span> next_ = <span class="number">0</span>, <span class="keyword">int</span> to_ = <span class="number">0</span>, <span class="keyword">long</span> <span class="keyword">long</span> val_ = <span class="number">0</span>) {</span><br><span class="line">next = next_, to = to_, val = val_; }</span><br><span class="line">} E[M_ << <span class="number">1</span>];</span><br><span class="line"><span class="keyword">int</span> Head[N_];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> from, <span class="keyword">int</span> to, <span class="keyword">long</span> <span class="keyword">long</span> val)</span> </span>{</span><br><span class="line">E[++cnt] = <span class="built_in">Edge</span>(Head[from], to, val);</span><br><span class="line">Head[from] = cnt; </span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(i) E[i].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(i) E[i].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Adj</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> val;</span><br><span class="line"><span class="built_in">Adj</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>, <span class="keyword">long</span> <span class="keyword">long</span> val_ = <span class="number">0</span>) {</span><br><span class="line">x = x_, y = y_, val = val_; }</span><br><span class="line"><span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Adj &x, <span class="keyword">const</span> Adj &y) {</span><br><span class="line"><span class="keyword">return</span> x.val < y.val; }</span><br><span class="line">} Te[M_];</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">UnionAndFind</span> {</span></span><br><span class="line"><span class="keyword">int</span> Fa[N_];</span><br><span class="line"></span><br><span class="line"><span class="built_in">UnionAndFind</span>() {</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N_) Fa[i] = i; }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> Fa[x] = Fa[x] == x ? x : <span class="built_in">Find</span>(Fa[x]); }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">Ask</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> <span class="built_in">Find</span>(x) == <span class="built_in">Find</span>(y); }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Union</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line">Fa[<span class="built_in">Find</span>(x)] = <span class="built_in">Find</span>(y); }</span><br><span class="line">} Uf;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> F[N_][E_][<span class="number">2</span>];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> sum;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> Dep[N_], P[N_][E_];</span><br><span class="line"><span class="keyword">bool</span> Vis[M_];</span><br><span class="line"></span><br><span class="line"><span class="function">pair<<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">long</span> <span class="keyword">long</span>> <span class="title">Mx</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> y)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeP</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeF</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; <span class="keyword">long</span> <span class="keyword">long</span> w; </span><br><span class="line">IN >> u >> v >> w;</span><br><span class="line">Te[i] = <span class="built_in">Adj</span>(u, v, w);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">sort</span>(Te, Te + m);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, m) {</span><br><span class="line"><span class="keyword">int</span> x = Te[i].x, y = Te[i].y;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> val = Te[i].val;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(Uf.<span class="built_in">Ask</span>(x, y)) <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line">Uf.<span class="built_in">Union</span>(x, y);</span><br><span class="line"></span><br><span class="line">sum += val; Vis[i] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">Add</span>(x, y, val); <span class="built_in">Add</span>(y, x, val);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Pre</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">MakeP</span>();</span><br><span class="line"><span class="built_in">MakeF</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans = INF;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, m) <span class="keyword">if</span>(!Vis[i]) {</span><br><span class="line"><span class="keyword">int</span> x = Te[i].x, y = Te[i].y;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> val = Te[i].val;</span><br><span class="line">pair<<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">long</span> <span class="keyword">long</span>> p = <span class="built_in">Mx</span>(x, y);</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> mx = p.first, mxx = p.second;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(val > mx)ans = <span class="built_in">min</span>(ans, sum + val - mx);</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(val == mx && mxx != -INF) ans = <span class="built_in">min</span>(ans, sum + val - mxx);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">cout << ans;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line">Dep[<span class="built_in">T</span>(i)] = Dep[x] + <span class="number">1</span>,</span><br><span class="line">F[<span class="built_in">T</span>(i)][<span class="number">0</span>][<span class="number">0</span>] = <span class="built_in">V</span>(i), P[<span class="built_in">T</span>(i)][<span class="number">0</span>] = x,</span><br><span class="line"><span class="built_in">Pre</span>(<span class="built_in">T</span>(i), x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeP</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(e, <span class="number">1</span>, E_) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(P[i][e<span class="number">-1</span>])</span><br><span class="line">P[i][e] = P[P[i][e<span class="number">-1</span>]][e<span class="number">-1</span>];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeF</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">Ir</span>(e, <span class="number">1</span>, E_) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(P[i][e]) </span><br><span class="line">F[i][e][<span class="number">0</span>] = <span class="built_in">max</span>(F[i][e<span class="number">-1</span>][<span class="number">0</span>], F[P[i][e<span class="number">-1</span>]][e<span class="number">-1</span>][<span class="number">0</span>]),</span><br><span class="line">F[i][e][<span class="number">1</span>] = F[i][e<span class="number">-1</span>][<span class="number">0</span>] != F[P[i][e<span class="number">-1</span>]][e<span class="number">-1</span>][<span class="number">0</span>] ?</span><br><span class="line"><span class="built_in">min</span>(F[i][e<span class="number">-1</span>][<span class="number">0</span>], F[P[i][e<span class="number">-1</span>]][e<span class="number">-1</span>][<span class="number">0</span>]) : <span class="built_in">max</span>(F[i][e<span class="number">-1</span>][<span class="number">1</span>], F[P[i][e<span class="number">-1</span>]][e<span class="number">-1</span>][<span class="number">1</span>]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function">pair<<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">long</span> <span class="keyword">long</span>> <span class="title">Mx</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> y)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> mx = -INF, mxx = -INF;</span><br><span class="line"><span class="keyword">if</span>(Dep[x] < Dep[y]) <span class="built_in">swap</span>(x, y);</span><br><span class="line"></span><br><span class="line"><span class="built_in">SR</span>(e, E_ - <span class="number">1</span>, <span class="number">0</span>) <span class="keyword">if</span>(P[x][e] && Dep[P[x][e]] >= Dep[y]) {</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> u = F[x][e][<span class="number">0</span>], v = F[x][e][<span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(u > mx) mxx = mx, mx = u;</span><br><span class="line"><span class="keyword">if</span>(u > mxx && u != mx) mxx = u;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(v > mxx) mxx = v;</span><br><span class="line"></span><br><span class="line">x = P[x][e];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(x == y) <span class="keyword">return</span> <span class="built_in">make_pair</span>(mx, mxx);</span><br><span class="line"></span><br><span class="line"><span class="built_in">SR</span>(e, E_ - <span class="number">1</span>, <span class="number">0</span>) <span class="keyword">if</span>(P[x][e] != P[y][e]) {</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> T[<span class="number">4</span>] = { F[x][e][<span class="number">0</span>], F[y][e][<span class="number">0</span>], F[x][e][<span class="number">1</span>], F[y][e][<span class="number">1</span>] };</span><br><span class="line"><span class="built_in">sort</span>(T, T+<span class="number">4</span>); </span><br><span class="line"><span class="keyword">int</span> idx = <span class="built_in">unique</span>(T, T+<span class="number">4</span>) - T;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, idx) {</span><br><span class="line">mx = <span class="built_in">max</span>(mx, T[i]);</span><br><span class="line"><span class="keyword">if</span>(T[i] != mx) mxx = <span class="built_in">max</span>(mxx, T[i]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">x = P[x][e]; y = P[y][e];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> u = F[x][<span class="number">0</span>][<span class="number">0</span>], v = F[y][<span class="number">0</span>][<span class="number">0</span>];</span><br><span class="line">mx = <span class="built_in">max</span>(mx, <span class="built_in">max</span>(u, v));</span><br><span class="line"><span class="keyword">if</span>(u != mx) mxx = <span class="built_in">max</span>(mxx, u);</span><br><span class="line"><span class="keyword">if</span>(v != mx) mxx = <span class="built_in">max</span>(mxx, v);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">make_pair</span>(mx, mxx);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> LCA </tag>
<tag> 图论 </tag>
<tag> 最小生成树 </tag>
<tag> 树上倍增 </tag>
</tags>
</entry>
<entry>
<title>AcWing354 天天爱跑步</title>
<link href="/2021/11/02/AcWing354_%E5%A4%A9%E5%A4%A9%E7%88%B1%E8%B7%91%E6%AD%A5/"/>
<url>/2021/11/02/AcWing354_%E5%A4%A9%E5%A4%A9%E7%88%B1%E8%B7%91%E6%AD%A5/</url>
<content type="html"><![CDATA[<h3 id="AcWing354"><a href="#AcWing354" class="headerlink" title="AcWing354"></a><a href="https://www.acwing.com/problem/content/356/">AcWing354</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><h4 id="一条路径对一个点有贡献-当且仅当"><a href="#一条路径对一个点有贡献-当且仅当" class="headerlink" title="一条路径对一个点有贡献, 当且仅当:"></a>一条路径对一个点有贡献, 当且仅当:</h4><ol><li><h5 id="D-s-D-x-W-x-即-D-s-W-x-D-x"><a href="#D-s-D-x-W-x-即-D-s-W-x-D-x" class="headerlink" title="D[s] - D[x] = W[x], 即 D[s] = W[x] + D[x]"></a>D[s] - D[x] = W[x], 即 D[s] = W[x] + D[x]</h5></li><li><h5 id="D-s-D-x-2D-LCA-s-ed-W-x-即-D-s-2D-LCA-s-ed-W-x-D-x"><a href="#D-s-D-x-2D-LCA-s-ed-W-x-即-D-s-2D-LCA-s-ed-W-x-D-x" class="headerlink" title="D[s] + D[x] - 2D[LCA(s, ed)] = W[x], 即 D[s] - 2D[LCA(s, ed)] = W[x] - D[x]"></a>D[s] + D[x] - 2D[LCA(s, ed)] = W[x], 即 D[s] - 2D[LCA(s, ed)] = W[x] - D[x]</h5></li></ol><h4 id="问题转化为每条路径上的点加-D-s-D-s-2D-LCA-s-ed-每个点的贡献即为权值为-W-x-D-x-W-x-D-x-的数量"><a href="#问题转化为每条路径上的点加-D-s-D-s-2D-LCA-s-ed-每个点的贡献即为权值为-W-x-D-x-W-x-D-x-的数量" class="headerlink" title="问题转化为每条路径上的点加 D[s]/D[s]-2D[LCA(s, ed)], 每个点的贡献即为权值为 W[x] + D[x] / W[x] - D[x] 的数量"></a>问题转化为每条路径上的点加 D[s]/D[s]-2D[LCA(s, ed)], 每个点的贡献即为权值为 W[x] + D[x] / W[x] - D[x] 的数量</h4><h4 id="记录全局权值为x的个数为C-x-dfs处理-每次遇到一个点-使用临时变量cnt记录当前的C-W-x-C-W-x-D-x-递归处理-更新C的值-递归完后C-W-x-cnt-即为其子树中的答案"><a href="#记录全局权值为x的个数为C-x-dfs处理-每次遇到一个点-使用临时变量cnt记录当前的C-W-x-C-W-x-D-x-递归处理-更新C的值-递归完后C-W-x-cnt-即为其子树中的答案" class="headerlink" title="记录全局权值为x的个数为C[x], dfs处理, 每次遇到一个点, 使用临时变量cnt记录当前的C[W[x]] / C[W[x]-D[x]], 递归处理, 更新C的值, 递归完后C[W[x]] - cnt 即为其子树中的答案"></a>记录全局权值为x的个数为C[x], dfs处理, 每次遇到一个点, 使用临时变量cnt记录当前的C[W[x]] / C[W[x]-D[x]], 递归处理, 更新C的值, 递归完后C[W[x]] - cnt 即为其子树中的答案</h4><h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ul><li><h4 id="两种情况分别讨论-记录"><a href="#两种情况分别讨论-记录" class="headerlink" title="两种情况分别讨论, 记录"></a>两种情况分别讨论, 记录</h4></li><li><h4 id="第二种情况可能出现负数下标"><a href="#第二种情况可能出现负数下标" class="headerlink" title="第二种情况可能出现负数下标"></a>第二种情况可能出现负数下标</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">3e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">3e5</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> V_ = N_ + <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">UnionAndFind</span> {</span></span><br><span class="line"><span class="keyword">int</span> Fa[N_];</span><br><span class="line"><span class="built_in">UnionAndFind</span>() {</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N_) Fa[i] = i; }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> Fa[x] = Fa[x] == x ? x : <span class="built_in">Find</span>(Fa[x]); }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Union</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line">Fa[<span class="built_in">Find</span>(x)] = <span class="built_in">Find</span>(y); }</span><br><span class="line">} Uf;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> next, to;</span><br><span class="line"><span class="built_in">Edge</span>(<span class="keyword">int</span> next_ = <span class="number">0</span>, <span class="keyword">int</span> to_ = <span class="number">0</span>) {</span><br><span class="line">next = next_; to = to_;</span><br><span class="line">}</span><br><span class="line">} E[N_ << <span class="number">1</span>];</span><br><span class="line"><span class="keyword">int</span> Head[N_];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> from, <span class="keyword">int</span> to)</span> </span>{</span><br><span class="line">E[++cnt] = <span class="built_in">Edge</span>(Head[from], to);</span><br><span class="line">Head[from] = cnt;</span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(i) E[i].to</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y; </span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>) {</span><br><span class="line">x = x_; y = y_; }</span><br><span class="line">} M[M_];</span><br><span class="line"></span><br><span class="line">vector<<span class="keyword">int</span>> Ad_On[M_], Del_On[M_], Ad_Do[M_], Del_Do[M_], Q[N_], Id[N_];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> D[N_], W[N_], Lca[M_], Fa[N_], C_On[M_+V_], C_Do[M_ + V_], Ans[N_];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> n, m, ans;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeD</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Solve</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line"><span class="built_in">Add</span>(u, v); <span class="built_in">Add</span>(v, u);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> W[i];</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> s, t; IN >> s >> t;</span><br><span class="line">M[i] = <span class="built_in">Node</span>(s, t);</span><br><span class="line">Q[s].<span class="built_in">push_back</span>(t); Q[t].<span class="built_in">push_back</span>(s);</span><br><span class="line">Id[s].<span class="built_in">push_back</span>(i); Id[t].<span class="built_in">push_back</span>(i);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">MakeD</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> x, y, lca, fa;</span><br><span class="line">x = M[i].x, y = M[i].y, lca = Lca[i], fa = Fa[lca];</span><br><span class="line"></span><br><span class="line">Ad_On[x].<span class="built_in">push_back</span>(D[x] + V_), Del_On[fa].<span class="built_in">push_back</span>(D[x] + V_);</span><br><span class="line">Ad_Do[y].<span class="built_in">push_back</span>(D[x] - <span class="number">2</span> * D[lca] + V_), Del_Do[lca].<span class="built_in">push_back</span>(D[x] - <span class="number">2</span> * D[lca] + V_);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Solve</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) </span><br><span class="line">cout << Ans[i] << <span class="string">" "</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MakeD</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line">D[<span class="built_in">T</span>(i)] = D[x] + <span class="number">1</span>, Fa[<span class="built_in">T</span>(i)] = x, </span><br><span class="line"><span class="built_in">MakeD</span>(<span class="built_in">T</span>(i), x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Solve</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> fcnt = C_On[W[x] + D[x] + V_], scnt = C_Do[W[x] - D[x] + V_];</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Ad_On[x].<span class="built_in">size</span>()) C_On[Ad_On[x][i]]++;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Del_On[x].<span class="built_in">size</span>()) C_On[Del_On[x][i]]--;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Ad_Do[x].<span class="built_in">size</span>()) C_Do[Ad_Do[x][i]]++;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Del_Do[x].<span class="built_in">size</span>()) C_Do[Del_Do[x][i]]--;</span><br><span class="line"></span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line"><span class="built_in">Solve</span>(<span class="built_in">T</span>(i), x); </span><br><span class="line"></span><br><span class="line">Ans[x] = C_On[W[x] + D[x] + V_] - fcnt + </span><br><span class="line"> C_Do[W[x] - D[x] + V_] - scnt;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="built_in">T</span>(i), x);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Q[x].<span class="built_in">size</span>())</span><br><span class="line">Lca[Id[x][i]] = Uf.<span class="built_in">Find</span>(Q[x][i]);</span><br><span class="line"></span><br><span class="line">Uf.<span class="built_in">Union</span>(x, last ? last : x);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> LCA </tag>
<tag> 树 </tag>
<tag> 图论 </tag>
<tag> 树上差分 </tag>
</tags>
</entry>
<entry>
<title>AcWing352 暗之连锁</title>
<link href="/2021/11/02/AcWing352_%E6%9A%97%E4%B9%8B%E8%BF%9E%E9%94%81/"/>
<url>/2021/11/02/AcWing352_%E6%9A%97%E4%B9%8B%E8%BF%9E%E9%94%81/</url>
<content type="html"><![CDATA[<h3 id="AcWing352"><a href="#AcWing352" class="headerlink" title="AcWing352"></a><a href="https://www.acwing.com/problem/content/354/">AcWing352</a></h3><h3 id="附加边构成环-对于每一条主要边-设被环覆盖的次数为p-则其对答案的贡献为"><a href="#附加边构成环-对于每一条主要边-设被环覆盖的次数为p-则其对答案的贡献为" class="headerlink" title="附加边构成环, 对于每一条主要边, 设被环覆盖的次数为p, 则其对答案的贡献为"></a>附加边构成环, 对于每一条主要边, 设被环覆盖的次数为p, 则其对答案的贡献为</h3><ul><li><h4 id="p-0-gt-m"><a href="#p-0-gt-m" class="headerlink" title="p = 0 -> m"></a>p = 0 -> m</h4></li><li><h4 id="p-1-gt-1"><a href="#p-1-gt-1" class="headerlink" title="p = 1 -> 1"></a>p = 1 -> 1</h4></li><li><h4 id="p-gt-1-gt-0"><a href="#p-gt-1-gt-0" class="headerlink" title="p > 1 -> 0"></a>p > 1 -> 0</h4></li></ul><h3 id="差分"><a href="#差分" class="headerlink" title="差分:"></a>差分:</h3><h4 id="对于每一条附加边-x-y-令点权-W-x-1-W-y-1-W-LCA-x-y-2-则每个结点及其子树的权值和即为连接其父节点的边的被覆盖次数"><a href="#对于每一条附加边-x-y-令点权-W-x-1-W-y-1-W-LCA-x-y-2-则每个结点及其子树的权值和即为连接其父节点的边的被覆盖次数" class="headerlink" title="对于每一条附加边(x, y), 令点权 W[x]+1, W[y]+1, W[LCA(x, y)] - 2, 则每个结点及其子树的权值和即为连接其父节点的边的被覆盖次数"></a>对于每一条附加边(x, y), 令点权 W[x]+1, W[y]+1, W[LCA(x, y)] - 2, 则每个结点及其子树的权值和即为连接其父节点的边的被覆盖次数</h4><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M_ = <span class="number">2e5</span> + <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> next, to;</span><br><span class="line"><span class="built_in">Edge</span>(<span class="keyword">int</span> next_ = <span class="number">0</span>, <span class="keyword">int</span> to_ = <span class="number">0</span>) {</span><br><span class="line">next = next_; to = to_;</span><br><span class="line">}</span><br><span class="line">} E[N_ << <span class="number">1</span>];</span><br><span class="line"><span class="keyword">int</span> Head[N_];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> from, <span class="keyword">int</span> to)</span> </span>{</span><br><span class="line">E[++cnt] = <span class="built_in">Edge</span>(Head[from], to);</span><br><span class="line">Head[from] = cnt;</span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(i) E[i].to</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">UnionAndFind</span> {</span></span><br><span class="line"><span class="keyword">int</span> Fa[N_];</span><br><span class="line"><span class="built_in">UnionAndFind</span>() {</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, N_) Fa[i] = i; }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Union</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line">Fa[<span class="built_in">Find</span>(x)] = <span class="built_in">Find</span>(y); }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> Fa[x] = Fa[x] == x ? x : <span class="built_in">Find</span>(Fa[x]); }</span><br><span class="line">} Uf;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> {</span></span><br><span class="line"><span class="keyword">int</span> x, y;</span><br><span class="line"><span class="built_in">Node</span>(<span class="keyword">int</span> x_ = <span class="number">0</span>, <span class="keyword">int</span> y_ = <span class="number">0</span>) {</span><br><span class="line">x = x_; y = y_; }</span><br><span class="line">}T[M_];</span><br><span class="line"></span><br><span class="line">vector<<span class="keyword">int</span>> Q[N_], Id[N_];</span><br><span class="line"><span class="keyword">int</span> W[N_], Lca[M_];</span><br><span class="line"><span class="keyword">int</span> n, m, ans;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Solve</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">1</span>, n) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line"><span class="built_in">Add</span>(u, v); <span class="built_in">Add</span>(v, u);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> u, v; IN >> u >> v;</span><br><span class="line">T[i] = <span class="built_in">Node</span>(u, v);</span><br><span class="line">Q[u].<span class="built_in">push_back</span>(v); Q[v].<span class="built_in">push_back</span>(u);</span><br><span class="line">Id[u].<span class="built_in">push_back</span>(i); Id[v].<span class="built_in">push_back</span>(i);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, m) {</span><br><span class="line"><span class="keyword">int</span> x = T[i].x, y = T[i].y, lca = Lca[i];</span><br><span class="line">W[x]++, W[y]++, W[lca] -= <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Solve</span>(<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Tarjan</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line"><span class="built_in">Tarjan</span>(<span class="built_in">T</span>(i), x);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Ir</span>(i, <span class="number">0</span>, Q[x].<span class="built_in">size</span>()) </span><br><span class="line">Lca[Id[x][i]] = Uf.<span class="built_in">Find</span>(Q[x][i]);</span><br><span class="line"></span><br><span class="line">Uf.<span class="built_in">Union</span>(x, last != <span class="number">0</span> ? last : x);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Solve</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> last)</span> </span>{</span><br><span class="line"><span class="built_in">ER</span>(i, x) <span class="keyword">if</span>(<span class="built_in">T</span>(i) != last) </span><br><span class="line">W[x] += <span class="built_in">Solve</span>(<span class="built_in">T</span>(i), x);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(x == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!W[x]) ans += m;</span><br><span class="line"><span class="keyword">else</span> ans += W[x] == <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> W[x];</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> LCA </tag>
<tag> 树上差分 </tag>
<tag> 计数 </tag>
</tags>
</entry>
<entry>
<title>AcWing361 创世纪</title>
<link href="/2021/11/01/AcWing361_%E5%88%9B%E4%B8%96%E7%BA%AA/"/>
<url>/2021/11/01/AcWing361_%E5%88%9B%E4%B8%96%E7%BA%AA/</url>
<content type="html"><![CDATA[<h3 id="AcWing361"><a href="#AcWing361" class="headerlink" title="AcWing361"></a><a href="https://www.acwing.com/problem/content/description/361/">AcWing361</a></h3><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Ir(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Sr(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, v, x, y) for(int i = x.Head[y], v = x.T(i); i; i = x.Next[i], v = x.T(i))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ZR(x) memset(x, 0, sizeof x)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x =- x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N_ = <span class="number">1e6</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">int</span> N> </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span> {</span></span><br><span class="line"><span class="keyword">int</span> Next[N << <span class="number">1</span>], To[N << <span class="number">1</span>], Head[N];</span><br><span class="line"><span class="keyword">int</span> cnt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>{</span><br><span class="line">Next[++cnt] = Head[u]; To[cnt] = v; Head[u] = cnt; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">T</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">return</span> To[x]; }</span><br><span class="line"><span class="built_in">Edge</span>() {</span><br><span class="line"><span class="built_in">ZR</span>(Next); <span class="built_in">ZR</span>(To); <span class="built_in">ZR</span>(Head);</span><br><span class="line">cnt = <span class="number">1</span>; }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">Edge<N_> G;</span><br><span class="line"><span class="keyword">int</span> A[N_], F[N_][<span class="number">2</span>];</span><br><span class="line"><span class="keyword">int</span> n, rt, ans;</span><br><span class="line"><span class="keyword">bool</span> Vis[N_];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Dp</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">bool</span> opt)</span> </span>{</span><br><span class="line">F[x][<span class="number">0</span>] = <span class="number">0</span>; Vis[x] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tmp = INF, p, q;</span><br><span class="line"><span class="built_in">ER</span>(i, v, G, x) <span class="keyword">if</span>(v != rt) </span><br><span class="line">p = <span class="built_in">Dp</span>(v, opt), q = F[v][<span class="number">0</span>], </span><br><span class="line">tmp = <span class="built_in">min</span>(tmp, p - q), F[x][<span class="number">0</span>] += p;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(x != A[rt] || !opt)F[x][<span class="number">1</span>] = F[x][<span class="number">0</span>] - tmp + <span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span>F[x][<span class="number">1</span>] = F[x][<span class="number">0</span>] + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">max</span>(F[x][<span class="number">0</span>], F[x][<span class="number">1</span>]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Work</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"><span class="keyword">for</span>(rt = x; !Vis[A[rt]] && (Vis[rt] = <span class="number">1</span>); rt = A[rt]);</span><br><span class="line"><span class="keyword">int</span> ans = <span class="built_in">Dp</span>(rt, <span class="number">0</span>); <span class="built_in">Dp</span>(rt, <span class="number">1</span>);</span><br><span class="line"><span class="keyword">return</span> <span class="built_in">max</span>(ans, F[rt][<span class="number">0</span>]); </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:/Code/Data/WA.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> A[i], G.<span class="built_in">Add</span>(A[i], i);</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="keyword">if</span>(!Vis[i]) ans += <span class="built_in">Work</span>(i);</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> dp </tag>
<tag> 图论 </tag>
<tag> 基环树 </tag>
</tags>
</entry>
<entry>
<title>AcWing350 巡逻</title>
<link href="/2021/10/31/AcWing350_APIO2010_%E5%B7%A1%E9%80%BB/"/>
<url>/2021/10/31/AcWing350_APIO2010_%E5%B7%A1%E9%80%BB/</url>
<content type="html"><![CDATA[<h3 id="AcWing350"><a href="#AcWing350" class="headerlink" title="AcWing350"></a><a href="https://www.acwing.com/problem/content/352/">AcWing350</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><ul><li><h4 id="k-1-直径-ans-2-n-1-dia-1"><a href="#k-1-直径-ans-2-n-1-dia-1" class="headerlink" title="k = 1: 直径 ans = 2 * (n-1) - dia + 1"></a>k = 1: 直径 ans = 2 * (n-1) - dia + 1</h4></li><li><h4 id="k-2-新构成的环和前一个环重叠则损失-gt-直径上负-1权后再求一边直径-ans-2-n-1-dia-sdia-2"><a href="#k-2-新构成的环和前一个环重叠则损失-gt-直径上负-1权后再求一边直径-ans-2-n-1-dia-sdia-2" class="headerlink" title="k = 2: 新构成的环和前一个环重叠则损失->直径上负-1权后再求一边直径 ans = 2 * (n-1) - dia - sdia + 2"></a>k = 2: 新构成的环和前一个环重叠则损失->直径上负-1权后再求一边直径 ans = 2 * (n-1) - dia - sdia + 2</h4></li></ul><h3 id="Trick"><a href="#Trick" class="headerlink" title="Trick"></a>Trick</h3><ul><li><h4 id="记录每个点在邻接表中其父节点的位置"><a href="#记录每个点在邻接表中其父节点的位置" class="headerlink" title="记录每个点在邻接表中其父节点的位置"></a>记录每个点在邻接表中其父节点的位置</h4></li></ul>]]></content>
<tags>
<tag> dp </tag>
<tag> 图论 </tag>
<tag> 树的直径 </tag>
<tag> dfs </tag>
</tags>
</entry>
<entry>
<title>AcWing325 计算机</title>
<link href="/2021/10/31/AcWing325_%E8%AE%A1%E7%AE%97%E6%9C%BA/"/>
<url>/2021/10/31/AcWing325_%E8%AE%A1%E7%AE%97%E6%9C%BA/</url>
<content type="html"><![CDATA[<h3 id="AcWing325"><a href="#AcWing325" class="headerlink" title="AcWing325"></a><a href="https://www.acwing.com/problem/content/description/327/">AcWing325</a></h3><h3 id="F-x-0-1-和-Pos-x"><a href="#F-x-0-1-和-Pos-x" class="headerlink" title="F[x][0/1] 和 Pos[x]"></a>F[x][0/1] 和 Pos[x]</h3><h3 id="自下而上-F-x-0-1-以1为根时的值"><a href="#自下而上-F-x-0-1-以1为根时的值" class="headerlink" title="自下而上: F[x][0/1] 以1为根时的值"></a>自下而上: F[x][0/1] 以1为根时的值</h3><h3 id="自上而下-F-x-0-1-以x为根-最大和次大值"><a href="#自上而下-F-x-0-1-以x为根-最大和次大值" class="headerlink" title="自上而下: F[x][0/1] 以x为根, 最大和次大值"></a>自上而下: F[x][0/1] 以x为根, 最大和次大值</h3><h3 id="Pos-x-以x为根-x的最大值从哪一个子树更新"><a href="#Pos-x-以x为根-x的最大值从哪一个子树更新" class="headerlink" title="Pos[x] 以x为根, x的最大值从哪一个子树更新"></a>Pos[x] 以x为根, x的最大值从哪一个子树更新</h3><p>``cpp<br>#include<bits/stdc++.h><br>using namespace std;</p><p>#define IR(i, st, ed) for(int i = st; i <= ed; i++)<br>#define SR(i, st, ed) for(int i = st; i >= ed; i–)</p><p>struct FastIN<br>{<br> template<typename T><br> FastIN& operator>>(T& x)<br> {<br> char c, l;<br> for(c = 0; !isdigit(c); c = getchar() ) l = c;<br> for(x = 0; isdigit(c); c = getchar() ) x = x * 10 - ‘0’ + c;<br> if(l == ‘-‘) x = -x;<br> return *this;<br> }<br>} IN;<br>const int N_ = 1e4 + 2;</p><p>struct Edge {<br> int next, to, val; } E[N_ << 1];<br>int Head[N_];<br>int cnt;<br>void Add(int from, int to, int val) {<br> E[++cnt] = { Head[from], to, val };<br> Head[from] = cnt;<br>}<br>#define ER(i, x) for(int i = Head[x]; i; i = E[i].next)<br>#define V(i) E[i].val<br>#define T(i) E[i].to</p><p>vector<int> P[N_], Qp[N_], Qs[N_];<br>int Mk[N_], F[N_], Pos[N_];<br>int n;</p><p>int Pre(int x, int last);<br>void Wk(int x, int last);</p><p>int main() { /**/<br>#ifdef LOCAL<br> freopen(“I:/Code/Data/In.in”, “r”, stdin);<br> freopen(“I:/Code/Data/AC.out”, “w”, stdout);<br>#endif</p><pre><code>return 0;</code></pre><p>} /**/</p><p>int Pre(int x, int last) {<br> int mx = 0;<br> ER(i, x) {<br> if(T(i) == last) continue;</p><pre><code> P[x].push_back( Pre(T(i), x) + V(i) ); Pos[T(i)] = P[x].size() - 1; if(P[x].back() > mx) mx = P[x].back(), Mk[x] = T(i); Qp[x].push_back(mx);}Qs[x].resize(P[x].size());SR(i, P[x].size()-1, 0) Qs[x][i] = max(i < P[x].size()-1 ? Qs[x][i+1] : 0, P[x][i]);if(!P[x].size()) return 0;return mx;</code></pre><p>}</p><p>void Wk(int x, int last) {<br> ER(i, x) {<br> if(T(i) == last) continue;</p><pre><code> int p = Pos[T(i)]; F[T(i)] = Qp[x].size() ? max(p > 0 ? Qp[x][p-1] : 0, p < Qs[x].size() - 1 ? Qs[x][p+1] : 0) : 0 + V(i); printf("F[%d] = %d\n", T(i), F[T(i)]); /* printf("-------------------------\n"); printf("Qp[%d].size() = %d\n", x, Qp[x].size()); printf("Qs[%d].size() = %d\n", x, Qs[x].size()); printf("F[%d] = %d\n", T(i), F[T(i)]); printf("\n-------------------------\n"); */ if(Mk[x] != T(i)) F[T(i)] = max(F[T(i)], F[x] + V(i)); Wk(T(i), x);}</code></pre><p>}</p><pre><code></code></pre>]]></content>
<tags>
<tag> dp </tag>
<tag> 树形dp </tag>
<tag> 换根 </tag>
<tag> 二次扫描 </tag>
</tags>
</entry>
<entry>
<title>NOIP 2016 换教室</title>
<link href="/2021/10/29/LG1850_NOIP_2016_%E6%8F%90%E9%AB%98%E7%BB%84_%E6%8D%A2%E6%95%99%E5%AE%A4/"/>
<url>/2021/10/29/LG1850_NOIP_2016_%E6%8F%90%E9%AB%98%E7%BB%84_%E6%8D%A2%E6%95%99%E5%AE%A4/</url>
<content type="html"><![CDATA[<h3 id="LG1850"><a href="#LG1850" class="headerlink" title="LG1850"></a><a href="https://www.luogu.com.cn/problem/P1850">LG1850</a></h3><h3 id="状态-F-i-v-0-1-考虑到第i间教室-已经申请了v间-当前是否申请"><a href="#状态-F-i-v-0-1-考虑到第i间教室-已经申请了v间-当前是否申请" class="headerlink" title="状态: F[i][v][0/1] 考虑到第i间教室, 已经申请了v间, 当前是否申请"></a>状态: F[i][v][0/1] 考虑到第i间教室, 已经申请了v间, 当前是否申请</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IR(i, st, ed) for(int i = st; i <= ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SR(i, st, ed) for(int i = st; i >= ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ER(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c, l;</span><br><span class="line"><span class="keyword">for</span>(c = <span class="number">0</span>; !<span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) l = c;</span><br><span class="line"><span class="keyword">for</span>(x = <span class="number">0</span>; <span class="built_in">isdigit</span>(c); c = <span class="built_in">getchar</span>() ) x = x * <span class="number">10</span> - <span class="string">'0'</span> + c;</span><br><span class="line"><span class="keyword">if</span>(l == <span class="string">'-'</span>) x = -x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">} IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> _V = <span class="number">3e2</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> _N = <span class="number">2e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> _M = <span class="number">2e3</span> + <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> C[_N], D[_N], E[_N][_N]; </span><br><span class="line"><span class="keyword">int</span> n, m, v, e;</span><br><span class="line"><span class="keyword">double</span> K[_N], F[_N][_M][<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Floyd</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\AC.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m >> e >> v;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> C[i];</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) IN >> D[i];</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) cin >> K[i];</span><br><span class="line"></span><br><span class="line"><span class="built_in">memset</span>(E, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> E);</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, _V) E[i][i] = E[<span class="number">0</span>][i] = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, e) {</span><br><span class="line"><span class="keyword">int</span> u, v, w; IN >> u >> v >> w;</span><br><span class="line">E[u][v] = E[v][u] = <span class="built_in">min</span>(E[u][v], w);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">Floyd</span>();</span><br><span class="line"></span><br><span class="line"><span class="comment">//memset(F, 0xcf, sizeof F);</span></span><br><span class="line"><span class="comment">//cout << F[1][0][0] << "\n";</span></span><br><span class="line"><span class="comment">//memset(F, 0x3f, sizeof F);</span></span><br><span class="line"><span class="comment">//cout << F[1][0][0] << "\n";</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, _N<span class="number">-1</span>) <span class="built_in">IR</span>(v, <span class="number">0</span>, _M<span class="number">-1</span>) <span class="built_in">IR</span>(k, <span class="number">0</span>, <span class="number">1</span>)</span><br><span class="line">F[i][v][k] = INF;</span><br><span class="line"></span><br><span class="line">F[<span class="number">1</span>][<span class="number">0</span>][<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">F[<span class="number">1</span>][<span class="number">1</span>][<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">2</span>, n) <span class="built_in">IR</span>(v, <span class="number">0</span>, <span class="built_in">min</span>(i, m)) {</span><br><span class="line">F[i][v][<span class="number">0</span>] = <span class="built_in">min</span>(F[i<span class="number">-1</span>][v][<span class="number">0</span>] + E[C[i<span class="number">-1</span>]][C[i]], </span><br><span class="line"> F[i<span class="number">-1</span>][v][<span class="number">1</span>] + E[D[i<span class="number">-1</span>]][C[i]] * K[i<span class="number">-1</span>] + E[C[i<span class="number">-1</span>]][C[i]] * (<span class="number">1</span> - K[i<span class="number">-1</span>]));</span><br><span class="line"><span class="keyword">if</span>(v) {</span><br><span class="line">F[i][v][<span class="number">1</span>] = F[i][v<span class="number">-1</span>][<span class="number">0</span>] * (<span class="number">1</span> - K[i]) + </span><br><span class="line"> <span class="built_in">min</span>(F[i<span class="number">-1</span>][v<span class="number">-1</span>][<span class="number">0</span>] + E[C[i<span class="number">-1</span>]][D[i]], </span><br><span class="line"> F[i<span class="number">-1</span>][v<span class="number">-1</span>][<span class="number">1</span>] + E[D[i<span class="number">-1</span>]][D[i]] * K[i<span class="number">-1</span>] + E[C[i<span class="number">-1</span>]][D[i]] * (<span class="number">1</span>-K[i<span class="number">-1</span>]) );</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">printf("F[%d][%d][%d] = %lf\n", </span></span><br><span class="line"><span class="comment">i, v, 1, F[i][v][1]);</span></span><br><span class="line"><span class="comment">printf("F[%d][%d][%d] = %lf\n",</span></span><br><span class="line"><span class="comment">i, v, 0, F[i][v][0]);</span></span><br><span class="line"><span class="comment">printf("K[%d] = %d\n", i, K[i]);</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">0</span>, m) <span class="built_in">IR</span>(k, <span class="number">0</span>, <span class="number">1</span>)</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"F[%d][%d][%d] = %lf\n"</span>, i, v, k, F[i][v][k]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">double</span> ans = INF;</span><br><span class="line"><span class="built_in">IR</span>(i, <span class="number">0</span>, m) </span><br><span class="line">ans = <span class="built_in">min</span>(ans, </span><br><span class="line"> <span class="built_in">min</span>(F[n][i][<span class="number">0</span>], F[n][i][<span class="number">1</span>]) );</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%.2lf\n"</span>, ans);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Floyd</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">IR</span>(k, <span class="number">1</span>, n) <span class="built_in">IR</span>(i, <span class="number">1</span>, n) <span class="built_in">IR</span>(v, <span class="number">1</span>, n) </span><br><span class="line">E[i][v] = <span class="built_in">min</span>(E[i][v], E[i][k] + E[k][v]);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> dp </tag>
<tag> 期望 </tag>
</tags>
</entry>
<entry>
<title>CF559C</title>
<link href="/2021/10/27/CF559C_Gerald_and_Giant_Chess/"/>
<url>/2021/10/27/CF559C_Gerald_and_Giant_Chess/</url>
<content type="html"><![CDATA[<h3 id="CF559C"><a href="#CF559C" class="headerlink" title="CF559C"></a><a href="https://www.luogu.com.cn/problem/CF559C">CF559C</a></h3><h3 id="计数"><a href="#计数" class="headerlink" title="计数"></a>计数</h3>]]></content>
<tags>
<tag> dp </tag>
<tag> 计数 </tag>
</tags>
</entry>
<entry>
<title>单调队列多重背包</title>
<link href="/2021/10/26/%5B%E6%A8%A1%E6%9D%BF%5D%E5%8D%95%E8%B0%83%E9%98%9F%E5%88%97%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85/"/>
<url>/2021/10/26/%5B%E6%A8%A1%E6%9D%BF%5D%E5%8D%95%E8%B0%83%E9%98%9F%E5%88%97%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85/</url>
<content type="html"><![CDATA[<h3 id="AcWing6"><a href="#AcWing6" class="headerlink" title="AcWing6"></a><a href="https://www.acwing.com/problem/content/description/6/">AcWing6</a></h3><p>emmmm</p>]]></content>
<tags>
<tag> dp </tag>
<tag> 多重背包 </tag>
<tag> 单调队列 </tag>
</tags>
</entry>
<entry>
<title>UVA12983</title>
<link href="/2021/10/25/UVA12983_The_Battle_of_Chibi/"/>
<url>/2021/10/25/UVA12983_The_Battle_of_Chibi/</url>
<content type="html"><![CDATA[<h3 id="UVA12983"><a href="#UVA12983" class="headerlink" title="UVA12983"></a><a href="https://vjudge.net/problem/UVA-12983">UVA12983</a></h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><ul><li><h3 id="状态-F-i-v-以-A-v-结尾-长度为-i-的LIS种类数"><a href="#状态-F-i-v-以-A-v-结尾-长度为-i-的LIS种类数" class="headerlink" title="状态: $F[i][v]$ 以$A_v$结尾, 长度为$i$的LIS种类数"></a>状态: $F[i][v]$ 以$A_v$结尾, 长度为$i$的LIS种类数</h3></li><li><h3 id="转移-F-i-v-sum-limits-1-le-k-le-v-And-A-k-lt-A-v-F-i-1-k"><a href="#转移-F-i-v-sum-limits-1-le-k-le-v-And-A-k-lt-A-v-F-i-1-k" class="headerlink" title="转移: $F[i][v] = \sum\limits_{1\le k \le v \And A[k] < A[v]}F[i-1][k] $"></a>转移: $F[i][v] = \sum\limits_{1\le k \le v \And A[k] < A[v]}F[i-1][k] $</h3></li><li><h3 id="边界-F-1-i-1"><a href="#边界-F-1-i-1" class="headerlink" title="边界: $F[1][i] = 1$"></a>边界: $F[1][i] = 1$</h3></li><li><h3 id="外层枚举-i-内层枚举-v-树状数组维护离散化后每个数值对应答案的前缀和"><a href="#外层枚举-i-内层枚举-v-树状数组维护离散化后每个数值对应答案的前缀和" class="headerlink" title="外层枚举$i$, 内层枚举$v$, 树状数组维护离散化后每个数值对应答案的前缀和"></a>外层枚举$i$, 内层枚举$v$, 树状数组维护离散化后每个数值对应答案的前缀和</h3></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> _N = <span class="number">1e3</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> _M = <span class="number">1e3</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> P = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FenWick</span> {</span></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> Tr[_N];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Init</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Cg</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">long</span> <span class="keyword">long</span> val)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Ask</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span></span>; </span><br><span class="line">}B;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> F[_M][_N], A[_N], Q[_N], Pos[_N];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> t, n, m, idx, cnt;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\Out.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> t;</span><br><span class="line"><span class="keyword">while</span>(t--) {</span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) IN >> A[i], Q[i] = A[i];</span><br><span class="line"></span><br><span class="line"><span class="comment">//离散化</span></span><br><span class="line"><span class="built_in">sort</span>(Q, Q+n+<span class="number">1</span>);</span><br><span class="line">idx = <span class="built_in">unique</span>(Q+<span class="number">1</span>, Q+n+<span class="number">1</span>) - Q;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) Pos[i] = <span class="built_in">lower_bound</span>(Q+<span class="number">1</span>, Q+idx+<span class="number">1</span>, A[i]) - Q;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Dp</span></span><br><span class="line"><span class="built_in">memset</span>(F, <span class="number">0</span>, <span class="keyword">sizeof</span> F);</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) F[<span class="number">1</span>][i] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">2</span>, m+<span class="number">1</span>) {</span><br><span class="line">B.<span class="built_in">Init</span>();</span><br><span class="line"><span class="built_in">IOR</span>(v, <span class="number">1</span>, n+<span class="number">1</span>) {</span><br><span class="line">F[i][v] += B.<span class="built_in">Ask</span>(<span class="number">1</span>, Pos[v]<span class="number">-1</span>);</span><br><span class="line">F[i][v] %= P;</span><br><span class="line"></span><br><span class="line">B.<span class="built_in">Cg</span>(Pos[v], F[i<span class="number">-1</span>][v]);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) </span><br><span class="line">ans = (ans + F[m][i] % P) % P;</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"Case #%lld: %lld\n"</span>, ++cnt, ans);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FenWick::Init</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">memset</span>(Tr, <span class="number">0</span>, <span class="keyword">sizeof</span> Tr);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">FenWick::Cg</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">long</span> <span class="keyword">long</span> val)</span> </span>{</span><br><span class="line"><span class="keyword">while</span>(x <= n) </span><br><span class="line">Tr[x] = (Tr[x] + val % P) % P, x += x & -x;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">FenWick::Ask</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(l > r) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">l--; <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(l < r) res = (res % P + Tr[r]) % P, r -= r & -r;</span><br><span class="line"><span class="keyword">while</span>(l > r) res = (res % P - Tr[l]) % P, l -= l & -l;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> res % P;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> dp </tag>
<tag> 数据结构 </tag>
<tag> 树状数组 </tag>
</tags>
</entry>
<entry>
<title></title>
<link href="/2021/10/24/LG3959%5BNOIP2017%E6%8F%90%E9%AB%98%E7%BB%84%5D%E5%AE%9D%E8%97%8F/"/>
<url>/2021/10/24/LG3959%5BNOIP2017%E6%8F%90%E9%AB%98%E7%BB%84%5D%E5%AE%9D%E8%97%8F/</url>
<content type="html"><![CDATA[]]></content>
</entry>
<entry>
<title>POJ1742</title>
<link href="/2021/10/19/POJ1742/"/>
<url>/2021/10/19/POJ1742/</url>
<content type="html"><![CDATA[<h3 id="POJ1742"><a href="#POJ1742" class="headerlink" title="POJ1742"></a><a href="http://poj.org/problem?id=1742">POJ1742</a></h3><h3 id="Usd-j-表示考虑到第i个物品时拼出j需要的i的个数"><a href="#Usd-j-表示考虑到第i个物品时拼出j需要的i的个数" class="headerlink" title="Usd[j]表示考虑到第i个物品时拼出j需要的i的个数"></a>Usd[j]表示考虑到第i个物品时拼出j需要的i的个数</h3><h3 id="贪心-每次拼j时能用以前的拼出就用"><a href="#贪心-每次拼j时能用以前的拼出就用" class="headerlink" title="贪心, 每次拼j时能用以前的拼出就用"></a>贪心, 每次拼j时能用以前的拼出就用</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//#include<bits/stdc++.h></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">1e6</span> + <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> A[MAXN], B[MAXN], Use[MAXN], F[MAXN];</span><br><span class="line"><span class="keyword">int</span> T, n, m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\Out.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> m;</span><br><span class="line"><span class="keyword">while</span>(n + m) {</span><br><span class="line"><span class="built_in">memset</span>(F, <span class="number">0</span>, (m+<span class="number">10</span>) * <span class="built_in"><span class="keyword">sizeof</span></span>(<span class="keyword">int</span>));</span><br><span class="line">F[<span class="number">0</span>] = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) IN >> A[i];</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) IN >> B[i];</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) {</span><br><span class="line"><span class="built_in">memset</span>(Use, <span class="number">0</span>, (m+<span class="number">10</span>) * <span class="built_in"><span class="keyword">sizeof</span></span>(<span class="keyword">int</span>));</span><br><span class="line"><span class="built_in">IOR</span>(v, A[i], m+<span class="number">1</span>) </span><br><span class="line"><span class="keyword">if</span>(!F[v] && F[v-A[i]] && Use[v-A[i]] < B[i]) </span><br><span class="line">F[v] = <span class="literal">true</span>, Use[v] = Use[v-A[i]] + <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, m+<span class="number">1</span>) ans += F[i];</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line">IN >> n >> m;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Dp </tag>
<tag> 背包 </tag>
</tags>
</entry>
<entry>
<title>LCIS</title>
<link href="/2021/10/18/AcWing272/"/>
<url>/2021/10/18/AcWing272/</url>
<content type="html"><![CDATA[<h3 id="AcWing272"><a href="#AcWing272" class="headerlink" title="AcWing272"></a><a href="https://www.acwing.com/problem/content/274/">AcWing272</a></h3><h3 id="给定序列-A-B-长度-le-3e3-有负数-求LCIS"><a href="#给定序列-A-B-长度-le-3e3-有负数-求LCIS" class="headerlink" title="给定序列$A~B$, 长度$\le 3e3$, 有负数, 求LCIS"></a>给定序列$A~B$, 长度$\le 3e3$, 有负数, 求LCIS</h3><h3 id="解"><a href="#解" class="headerlink" title="解"></a>解</h3><ul><li><h4 id="状态-f-i-v-表示考虑-A-数组第-i-位-以-B-数组第-v-位结尾的LCIS长度"><a href="#状态-f-i-v-表示考虑-A-数组第-i-位-以-B-数组第-v-位结尾的LCIS长度" class="headerlink" title="状态$f_{i, v}$表示考虑$A$数组第$i$位, 以$B$数组第$v$位结尾的LCIS长度"></a>状态$f_{i, v}$表示考虑$A$数组第$i$位, 以$B$数组第$v$位结尾的LCIS长度</h4></li><li><h4 id="转移-f-i-v"><a href="#转移-f-i-v" class="headerlink" title="转移 $f_{i, v} =$"></a>转移 $f_{i, v} =$</h4><ul><li>$\qquad\qquad;; f_{i-1, v} \quad\quad a_i \neq b_i$</li><li>$\max\limits_{0 \leq k < j, a_i > b_k}{f_{i-1, k}} \qquad \text{else}$</li></ul></li></ul><h3 id="细节"><a href="#细节" class="headerlink" title="细节"></a>细节</h3><ul><li><h4 id="转移二中的max值可以在转移过程中维护-决策集合元素只增多不减少"><a href="#转移二中的max值可以在转移过程中维护-决策集合元素只增多不减少" class="headerlink" title="转移二中的max值可以在转移过程中维护(决策集合元素只增多不减少)"></a>转移二中的max值可以在转移过程中维护(决策集合元素只增多不减少)</h4></li><li><h4 id="注意具有负数-设置-a-i-b-i-0"><a href="#注意具有负数-设置-a-i-b-i-0" class="headerlink" title="注意具有负数, 设置$a_i = b_i = 0$"></a>注意具有负数, 设置$a_i = b_i = 0$</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">3e3</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> F[MAXN][MAXN], A[MAXN], B[MAXN];</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\Out.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) IN >> A[i];</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) IN >> B[i];</span><br><span class="line"></span><br><span class="line">A[<span class="number">0</span>] = B[<span class="number">0</span>] = -INF;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) {</span><br><span class="line"><span class="keyword">int</span> mx = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(v, <span class="number">1</span>, n+<span class="number">1</span>) {</span><br><span class="line">F[i][v] = (A[i] == B[v] ? </span><br><span class="line">mx+<span class="number">1</span> : F[i<span class="number">-1</span>][v]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(B[v] < A[i]) </span><br><span class="line">mx = <span class="built_in">max</span>(mx, F[i<span class="number">-1</span>][v]);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, n+<span class="number">1</span>) ans = <span class="built_in">max</span>(ans, F[n][i]);</span><br><span class="line"></span><br><span class="line">cout << ans;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Dp </tag>
<tag> LCS </tag>
<tag> LIS </tag>
</tags>
</entry>
<entry>
<title>AT_Tokiomarine2020_E</title>
<link href="/2021/10/17/AT_Tokiomarine2020_E/"/>
<url>/2021/10/17/AT_Tokiomarine2020_E/</url>
<content type="html"><![CDATA[<h3 id="AT-Tokiomarine2020-E"><a href="#AT-Tokiomarine2020-E" class="headerlink" title="AT_Tokiomarine2020_E"></a><a href="https://atcoder.jp/contests/tokiomarine2020/submissions/26617485">AT_Tokiomarine2020_E</a></h3><h3 id="题意"><a href="#题意" class="headerlink" title="题意"></a>题意</h3><h4 id="给定-N-个数-A-i-在其中选出-1-k-个满足选出的数按位与等于-S-按位或等于-T"><a href="#给定-N-个数-A-i-在其中选出-1-k-个满足选出的数按位与等于-S-按位或等于-T" class="headerlink" title="给定$N$个数$A_i$, 在其中选出$1-k$个满足选出的数按位与等于$S$, 按位或等于$T$"></a>给定$N$个数$A_i$, 在其中选出$1-k$个满足选出的数按位与等于$S$, 按位或等于$T$</h4><h4 id="0-lt-K-le-N-le-50-0-lt-A-i-S-T-le-2-18-A-i-neq-A-j"><a href="#0-lt-K-le-N-le-50-0-lt-A-i-S-T-le-2-18-A-i-neq-A-j" class="headerlink" title="$0 < K \le N \le 50, 0 < A_i, S, T \le 2^18, A_i \neq A_j$"></a>$0 < K \le N \le 50, 0 < A_i, S, T \le 2^18, A_i \neq A_j$</h4><h3 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h3><ol><li><h4 id="筛"><a href="#筛" class="headerlink" title="筛"></a>筛</h4><ul><li><h4 id="如果-S-T-T-neq-0-无解"><a href="#如果-S-T-T-neq-0-无解" class="headerlink" title="如果 $(S | T) ^ T \neq 0$ 无解"></a>如果 $(S | T) ^ T \neq 0$ 无解</h4></li><li><h4 id="对于每一个数-x-如果-x-T-neq-T-或-x-amp-S-neq-S-则一定不可能作为解的一部分"><a href="#对于每一个数-x-如果-x-T-neq-T-或-x-amp-S-neq-S-则一定不可能作为解的一部分" class="headerlink" title="对于每一个数$x$, 如果$x | T \neq T$或$x & S \neq S$, 则一定不可能作为解的一部分"></a>对于每一个数$x$, 如果$x | T \neq T$或$x & S \neq S$, 则一定不可能作为解的一部分</h4></li><li><h4 id="对于筛出的数-只需考虑-S-T-为-1-的位"><a href="#对于筛出的数-只需考虑-S-T-为-1-的位" class="headerlink" title="对于筛出的数, 只需考虑$S ^ T$为$1$的位"></a>对于筛出的数, 只需考虑$S ^ T$为$1$的位</h4></li></ul></li><li><h4 id="容斥"><a href="#容斥" class="headerlink" title="容斥"></a>容斥</h4><ul><li><h4 id="性质-具有全0或全1位"><a href="#性质-具有全0或全1位" class="headerlink" title="性质: 具有全0或全1位"></a>性质: 具有全0或全1位</h4></li><li><h4 id="目的-不具有性质的个数"><a href="#目的-不具有性质的个数" class="headerlink" title="目的: 不具有性质的个数"></a>目的: 不具有性质的个数</h4></li><li><h4 id="贡献-g-i-1-当且仅当-i-0-否则为-0-鬼知道为什么我的md用不了latex环境"><a href="#贡献-g-i-1-当且仅当-i-0-否则为-0-鬼知道为什么我的md用不了latex环境" class="headerlink" title="贡献: $g(i) = 1$ 当且仅当 $i = 0$, 否则为 $0$ (鬼知道为什么我的md用不了latex环境)"></a>贡献: $g(i) = 1$ 当且仅当 $i = 0$, 否则为 $0$ (鬼知道为什么我的md用不了latex环境)</h4></li><li><h4 id="容斥系数-考虑对于这个特殊的贡献-容斥系数-f-i-1-i"><a href="#容斥系数-考虑对于这个特殊的贡献-容斥系数-f-i-1-i" class="headerlink" title="容斥系数: 考虑对于这个特殊的贡献, 容斥系数$f(i) = (-1)^i$"></a>容斥系数: 考虑对于这个特殊的贡献, 容斥系数$f(i) = (-1)^i$</h4></li></ul></li><li><h4 id="求解"><a href="#求解" class="headerlink" title="求解"></a>求解</h4><ul><li><h4 id="枚举需要考虑的每一位-“全1或全0”-或-“不考虑”"><a href="#枚举需要考虑的每一位-“全1或全0”-或-“不考虑”" class="headerlink" title="枚举需要考虑的每一位 “全1或全0” 或 “不考虑”"></a>枚举需要考虑的每一位 “全1或全0” 或 “不考虑”</h4></li><li><h4 id="对于每次枚举出状态-将每一个数分类存储在桶中-下标-或者说分类依据-为上条前种状态中考虑的位置的对应二进制组成的数"><a href="#对于每次枚举出状态-将每一个数分类存储在桶中-下标-或者说分类依据-为上条前种状态中考虑的位置的对应二进制组成的数" class="headerlink" title="对于每次枚举出状态, 将每一个数分类存储在桶中, 下标,或者说分类依据,为上条前种状态中考虑的位置的对应二进制组成的数"></a>对于每次枚举出状态, 将每一个数分类存储在桶中, 下标,或者说分类依据,为上条前种状态中考虑的位置的对应二进制组成的数</h4></li><li><h4 id="对于每一类-都可以选择-1-k-个-容斥系数与枚举状态中考虑的数的个数-即状态变量-i-二进制中-1-的个数相关"><a href="#对于每一类-都可以选择-1-k-个-容斥系数与枚举状态中考虑的数的个数-即状态变量-i-二进制中-1-的个数相关" class="headerlink" title="对于每一类, 都可以选择$1-k$个, 容斥系数与枚举状态中考虑的数的个数, 即状态变量$i$二进制中$1$的个数相关"></a>对于每一类, 都可以选择$1-k$个, 容斥系数与枚举状态中考虑的数的个数, 即状态变量$i$二进制中$1$的个数相关</h4></li></ul></li></ol><h3 id="细节-技巧-注意"><a href="#细节-技巧-注意" class="headerlink" title="细节/技巧/注意"></a>细节/技巧/注意</h3><ul><li><h4 id="对于一个数x-遍历仅改变其二进制下原为-1-的位的所有情况"><a href="#对于一个数x-遍历仅改变其二进制下原为-1-的位的所有情况" class="headerlink" title="对于一个数x, 遍历仅改变其二进制下原为$1$的位的所有情况"></a>对于一个数x, 遍历仅改变其二进制下原为$1$的位的所有情况</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = x; i; i = (i - <span class="number">1</span>) & x) </span><br><span class="line">something</span><br></pre></td></tr></table></figure></li><li><h4 id="此题求解中对于考虑到的数-x-按状态-i-中考虑到的每一位分类"><a href="#此题求解中对于考虑到的数-x-按状态-i-中考虑到的每一位分类" class="headerlink" title="此题求解中对于考虑到的数$x$按状态$i$中考虑到的每一位分类"></a>此题求解中对于考虑到的数$x$按状态$i$中考虑到的每一位分类</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">A[i] & i</span><br></pre></td></tr></table></figure></li><li><h4 id="注意清空桶"><a href="#注意清空桶" class="headerlink" title="注意清空桶"></a>注意清空桶</h4></li></ul><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">60</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXV = <span class="number">1e6</span> + <span class="number">10</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> T[MAXN], A[MAXN], C[MAXN][MAXN];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> Bk[MAXV]; </span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> n, s, k, t, ans, cnt;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">()</span></span>;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\Out.out"</span>, <span class="string">"w"</span>, stdout);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> k >> s >> t;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) IN >> T[i];</span><br><span class="line"> </span><br><span class="line"><span class="built_in">Pre</span>();</span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span>((s | t) ^ t) <span class="keyword">return</span> cout << <span class="number">0</span>, <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) </span><br><span class="line"><span class="keyword">if</span>( (s & T[i]) == s && (t | T[i]) == t ) </span><br><span class="line">A[cnt++] = T[i];</span><br><span class="line"> </span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, k+<span class="number">1</span>) </span><br><span class="line">ans += C[cnt][i];</span><br><span class="line"> </span><br><span class="line"><span class="keyword">int</span> lm = s ^ t; </span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = lm; i; i = (i - <span class="number">1</span>) & lm) {</span><br><span class="line"><span class="built_in">IOR</span>(v, <span class="number">0</span>, cnt) Bk[A[v] & i]++;</span><br><span class="line"> </span><br><span class="line"><span class="built_in">IOR</span>(v, <span class="number">0</span>, cnt) {</span><br><span class="line"><span class="built_in">IOR</span>(l, <span class="number">1</span>, k+<span class="number">1</span>) </span><br><span class="line">ans += C[Bk[A[v] & i] ][l] * (__builtin_popcount(i) & <span class="number">1</span> ? <span class="number">-1</span> : <span class="number">1</span>);</span><br><span class="line">Bk[A[v] & i] = <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">cout << ans;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, MAXN) C[i][<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> </span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, MAXN) <span class="built_in">IOR</span>(v, <span class="number">1</span>, MAXN)</span><br><span class="line">C[i][v] = C[i<span class="number">-1</span>][v] + C[i<span class="number">-1</span>][v<span class="number">-1</span>];</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="做题中的一些问题"><a href="#做题中的一些问题" class="headerlink" title="做题中的一些问题"></a>做题中的一些问题</h3><ul><li><h3 id="《关于我因为把样例1看成样例2结果调了半个小时试了三种组合数求法这件事》"><a href="#《关于我因为把样例1看成样例2结果调了半个小时试了三种组合数求法这件事》" class="headerlink" title="《关于我因为把样例1看成样例2结果调了半个小时试了三种组合数求法这件事》"></a>《关于我因为把样例1看成样例2结果调了半个小时试了三种组合数求法这件事》</h3></li></ul>]]></content>
<tags>
<tag> 数学 </tag>
<tag> 容斥 </tag>
</tags>
</entry>
<entry>
<title>CF839D Winter is here</title>
<link href="/2021/10/16/CF839D_Winter_is_here/"/>
<url>/2021/10/16/CF839D_Winter_is_here/</url>
<content type="html"><![CDATA[<h3 id="CF839D"><a href="#CF839D" class="headerlink" title="CF839D"></a><a href="https://www.luogu.com.cn/problem/CF839D">CF839D</a></h3><h3 id="从大到小考虑每个数及其倍数的子序列长度和"><a href="#从大到小考虑每个数及其倍数的子序列长度和" class="headerlink" title="从大到小考虑每个数及其倍数的子序列长度和"></a>从大到小考虑每个数及其倍数的子序列长度和</h3><h3 id="过程中去重"><a href="#过程中去重" class="headerlink" title="过程中去重"></a>过程中去重</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXV = <span class="number">1e6</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> P = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> Len[MAXV], Sum[MAXV];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> n;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Qp</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> e)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="comment">//freopen("I:\\Code\\Out.out", "w", stdout);</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n; </span><br><span class="line"><span class="keyword">int</span> x;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, n) IN >> x, Sum[x]++; </span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> tmp = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">LOR</span>(i, MAXV<span class="number">-1</span>, <span class="number">1</span>) {</span><br><span class="line"><span class="comment">//if(!Sum[i]) continue;</span></span><br><span class="line">tmp = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> v = i; v < MAXV; v += i) {</span><br><span class="line"><span class="comment">//tmp = (tmp % P + Sum[v] % P) % P;</span></span><br><span class="line">tmp = (tmp + Sum[v]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!tmp) <span class="keyword">continue</span>;</span><br><span class="line">tmp = tmp * <span class="built_in">Qp</span>(<span class="number">2</span>, tmp - <span class="number">1</span>) % P;</span><br><span class="line">Len[i] = tmp;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> v = i+i; v < MAXV; v += i)</span><br><span class="line">Len[i] = (Len[i] - Len[v] + P) % P;</span><br><span class="line"></span><br><span class="line">ans = (ans + Len[i] * i % P) % P;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">cout << ans << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">long long Qp(long long x, long long e) {</span></span><br><span class="line"><span class="comment">long long res = 1;</span></span><br><span class="line"><span class="comment">x %= P;</span></span><br><span class="line"><span class="comment">while(e) {</span></span><br><span class="line"><span class="comment">if(e & 1) res = res * x % P;</span></span><br><span class="line"><span class="comment">x = x * x % P; e >>= 1;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">cerr << "Qp:" << res << "\n";</span></span><br><span class="line"><span class="comment">return res % P;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Qp</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> e)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">1</span>;</span><br><span class="line">x %= P;</span><br><span class="line"><span class="keyword">while</span>(e) {</span><br><span class="line"><span class="keyword">if</span>(e & <span class="number">1</span>) res = res * x % P;</span><br><span class="line">x = x * x % P; e >>= <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> res % P;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 数论 </tag>
<tag> gcd </tag>
</tags>
</entry>
<entry>
<title></title>
<link href="/2021/10/15/LG2480%5BSDOI2010%5D%E5%8F%A4%E4%BB%A3%E7%8C%AA%E6%96%87/"/>
<url>/2021/10/15/LG2480%5BSDOI2010%5D%E5%8F%A4%E4%BB%A3%E7%8C%AA%E6%96%87/</url>
<content type="html"><![CDATA[<h3 id="LG2480-SDOI2010-古代猪文"><a href="#LG2480-SDOI2010-古代猪文" class="headerlink" title="LG2480[SDOI2010]古代猪文"></a><a href="https://www.luogu.com.cn/problem/P2480">LG2480[SDOI2010]古代猪文</a></h3><h3 id="题意-给定-g-n-le-1e9-求"><a href="#题意-给定-g-n-le-1e9-求" class="headerlink" title="题意: 给定$g, n\le 1e9$求"></a>题意: 给定$g, n\le 1e9$求</h3><p>$$g^{\sum_{d|n}\tbinom{n}{d}}<del>mod</del>999911659$$</p><h3 id="解"><a href="#解" class="headerlink" title="解:"></a>解:</h3><h3 id="1-欧拉定理"><a href="#1-欧拉定理" class="headerlink" title="1.欧拉定理"></a>1.欧拉定理</h3><p>$$g^{\sum_{d|n}\tbinom{n}{d}<del>mod</del>999911658} $$</p><h3 id="2-CRT-amp-amp-2-3-4679-35617-999911658"><a href="#2-CRT-amp-amp-2-3-4679-35617-999911658" class="headerlink" title="2.CRT && 2 * 3 * 4679 * 35617 = 999911658"></a>2.CRT && 2 * 3 * 4679 * 35617 = 999911658</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$\begin{cases} </span><br><span class="line">x \equiv \sum_{d|n}\tbinom{n}{d}~mod~2</span><br><span class="line">x \equiv \sum_{d|n}\tbinom{n}{d}~mod~3</span><br><span class="line">x \equiv \sum_{d|n}\tbinom{n}{d}~mod~4679</span><br><span class="line">x \equiv \sum_{d|n}\tbinom{n}{d}~mod~35617</span><br><span class="line">\end{cases}$</span><br></pre></td></tr></table></figure><h3 id="左边式子用lucas处理"><a href="#左边式子用lucas处理" class="headerlink" title="左边式子用lucas处理"></a>左边式子用lucas处理</h3><h3 id="明天再学怎么在Markdown放latex公式"><a href="#明天再学怎么在Markdown放latex公式" class="headerlink" title="明天再学怎么在Markdown放latex公式"></a>明天再学怎么在Markdown放latex公式</h3><h3 id="ans-g-x-mod999911659"><a href="#ans-g-x-mod999911659" class="headerlink" title="$ans=g^{x}mod999911659$"></a>$ans<del>=</del>g^{x}<del>mod</del>999911659$</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> IOR(i, st, ed) for(int i = st; i < ed; i++)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LOR(i, st, ed) for(int i = st; i > ed; i--)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EOR(i, x) for(int i = Head[x]; i; i = E[i].next)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> T(x) E[x].to</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> V(x) E[x].val</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">FastIN</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">template</span><<span class="keyword">typename</span> T></span><br><span class="line">FastIN& <span class="keyword">operator</span>>>(T& x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">char</span> c,l;</span><br><span class="line"><span class="keyword">for</span>(c=<span class="number">0</span>;!<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())l=c;</span><br><span class="line"><span class="keyword">for</span>(x=<span class="number">0</span>;<span class="built_in">isdigit</span>(c);c=<span class="built_in">getchar</span>())x=x*<span class="number">10</span>-<span class="string">'0'</span>+c;</span><br><span class="line"><span class="keyword">if</span>(l==<span class="string">'-'</span>)x=-x;</span><br><span class="line"><span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">}</span><br><span class="line">}IN;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">4e4</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXM = <span class="number">4</span> + <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> Fac[MAXN], Ie[MAXN], D[MAXN];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> A[MAXM], N[MAXM], L[MAXM] = {<span class="number">2</span>, <span class="number">3</span>, <span class="number">4679</span>, <span class="number">35617</span>};</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> g, n, cnt;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Qp</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> e, <span class="keyword">long</span> <span class="keyword">long</span> p)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Lucas</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n, <span class="keyword">long</span> <span class="keyword">long</span> m, <span class="keyword">long</span> <span class="keyword">long</span> p)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Crt</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">long</span> <span class="keyword">long</span>* A, <span class="keyword">long</span> <span class="keyword">long</span> *N)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">C</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n, <span class="keyword">long</span> <span class="keyword">long</span> m, <span class="keyword">long</span> <span class="keyword">long</span> p)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> p)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Exgcd</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> a, <span class="keyword">long</span> <span class="keyword">long</span> b, <span class="keyword">long</span> <span class="keyword">long</span> &x, <span class="keyword">long</span> <span class="keyword">long</span> &y)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">/**/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> LOCAL</span></span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"I:\\Code\\In.in"</span>, <span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="comment">//freopen("I:\\Code\\Out.out", "w", stdout);</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">IN >> n >> g;</span><br><span class="line"><span class="keyword">if</span>(g % <span class="number">999911659</span> == <span class="number">0</span>) {</span><br><span class="line">cout << <span class="string">"0"</span> << <span class="string">"\n"</span>;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> lm = <span class="built_in">sqrt</span>(n);</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, lm+<span class="number">1</span>) <span class="keyword">if</span>(n % i == <span class="number">0</span>) </span><br><span class="line">D[cnt++] = i, D[cnt++] = n / i;</span><br><span class="line"><span class="keyword">if</span>(D[cnt<span class="number">-1</span>] == D[cnt<span class="number">-2</span>]) cnt--;</span><br><span class="line"></span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, <span class="number">4</span>) {</span><br><span class="line"><span class="built_in">Pre</span>(L[i]); N[i] = L[i];</span><br><span class="line"><span class="built_in">IOR</span>(v, <span class="number">0</span>, cnt) </span><br><span class="line">A[i] = (A[i] % L[i] + <span class="built_in">Lucas</span>(n, D[v], L[i]) ) % L[i];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> x = <span class="built_in">Crt</span>(<span class="number">4</span>, A, N);</span><br><span class="line"></span><br><span class="line">cout << <span class="built_in">Qp</span>(g, x, <span class="number">999911659</span>) << <span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">/**/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Pre</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> p)</span> </span>{</span><br><span class="line">Fac[<span class="number">0</span>] = Ie[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">1</span>, p+<span class="number">1</span>) </span><br><span class="line">Fac[i] = Fac[i<span class="number">-1</span>] * i % p;</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Qp</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> e, <span class="keyword">long</span> <span class="keyword">long</span> p)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">1</span>;</span><br><span class="line">x %= p;</span><br><span class="line"><span class="keyword">while</span>(e) {</span><br><span class="line"><span class="keyword">if</span>(e & <span class="number">1</span>) res = res * x % p;</span><br><span class="line">x = x * x % p; e >>= <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> res % p;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Lucas</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n, <span class="keyword">long</span> <span class="keyword">long</span> m, <span class="keyword">long</span> <span class="keyword">long</span> p)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(!m) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">return</span> <span class="built_in">C</span>(n % p, m % p, p) * <span class="built_in">Lucas</span>(n / p, m / p, p) % p;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">Crt</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">long</span> <span class="keyword">long</span>* A, <span class="keyword">long</span> <span class="keyword">long</span> *N)</span> </span>{</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> pn = <span class="number">1</span>, res = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, k) pn *= N[i];</span><br><span class="line"><span class="built_in">IOR</span>(i, <span class="number">0</span>, k) {</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> im, x, m = pn / N[i];</span><br><span class="line"><span class="built_in">Exgcd</span>(m, N[i], im, x);</span><br><span class="line">im = (im + N[i]) % N[i];</span><br><span class="line">im = im * pn / N[i];</span><br><span class="line">res = (res % pn + A[i] * im % pn) % pn;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Exgcd</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> a, <span class="keyword">long</span> <span class="keyword">long</span> b, <span class="keyword">long</span> <span class="keyword">long</span> &x, <span class="keyword">long</span> <span class="keyword">long</span> &y)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(!b) {x = <span class="number">1</span>, y = <span class="number">0</span>; <span class="keyword">return</span> ;}</span><br><span class="line"><span class="built_in">Exgcd</span>(b, a % b, y, x);</span><br><span class="line">y -= x * (a / b);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">C</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n, <span class="keyword">long</span> <span class="keyword">long</span> m, <span class="keyword">long</span> <span class="keyword">long</span> p)</span> </span>{</span><br><span class="line"><span class="keyword">if</span>(n < m) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"><span class="keyword">return</span> Fac[n] * ( <span class="built_in">Qp</span>(Fac[m], p<span class="number">-2</span>, p) * <span class="built_in">Qp</span>(Fac[n-m], p<span class="number">-2</span>, p) % p) % p;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>