-
Notifications
You must be signed in to change notification settings - Fork 6
/
MyTemplate.cpp
3210 lines (2747 loc) · 74 KB
/
MyTemplate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bits/stdc++.h>
#define P(X) cout<<"db "<<X<<endl;
#define P2(X,Y) cout<<"d2 "<<X<<" "<<Y<<endl;
#define P3(X,Y,Z) cout<<"d3 "<<X<<" "<<Y<<" "<<Z<<endl;
#define SQ(x) ((x) * (x))
#define ll long long
#define pii pair<int,int>
#define bchk(n,i) (bool)(n&(1<<i))
#define bon(n,i) (n|(1<<i))
#define boff(n,i) n=n&(~(1<<i))
#define distance(a,b) (sq(a.x-b.x) + sq(a.y-b.y))
#define MAX3(a,b,c) max(a,max(b,c))
#define MS(XX,YY) memset(XX,YY,sizeof(XX));
#define FastIO ios_base::sync_with_stdio(0);cin.tie(nullptr);
#define eps 10e-9
#define MX 1000005
using namespace std;
int m,n;
int main()
{
int i,j,test,cas=0;
int a,b;
freopen("test.txt","r",stdin);
scanf("%d",&test);
while(test--){
scanf("%",&);
printf("Case %d:\n",++cas,);
}
return 0;
}
/**
DATA STRUCTURE! DATA STRUCTURE! DATA STRUCTURE!
DATA STRUCTURE! DATA STRUCTURE! DATA STRUCTURE!
DATA STRUCTURE! DATA STRUCTURE! DATA STRUCTURE!
DATA STRUCTURE! DATA STRUCTURE! DATA STRUCTURE!
*/
/// *** Squre Root Decomposition [Sqrt(n)*Query]
/** 1 based indexing
* if index are 1 2 3 4 5 6 7 and block size is 3 then
* 1 2 is first block and 3 4 5 is in second block
* this will reduce coding complexity
*/
int ara[MX],block[sqqrt(MX)],rt,in=0;
void update(){
/// point update is simple
/// segment update is like query
}
void creat(int n){
in=0;
int mn = INT_MAX,i;
for(i=1;i<n;i++){
mn = min(ara[i],mn);
if(i%rt==0){
block[++in] = mn;
mn = INT_MAX;
}
}
block[++in] = mn;
}
int query(int l,int r){
int mn = ara[l];
while(l%rt!=0&&l<=r&&l!=1){
mn = min(ara[l],mn);
l++;
}
while(l+rt<=r){
l += rt;
mn = min(mn,block[l/rt]);
}
while(l<=r){
mn = min(ara[l],mn);
l++;
}
return mn;
}
/**
Name : Sparse table(RMQ)
Description : Find min/max
Time Complexity : Build O(nlogn) Query O(1)
*/
#include <bits/stdc++.h>
using namespace std;
//0 Indexed
#define MX 10000
int spt[MX][22];
int n,ar[MX]={ 7, 2, 3, 0, 5, 10, 3, 12, 18 };
void buildST()
{
for (int i = 0; i < n; i++) spt[i][0] = ar[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
spt[i][j] = min(spt[i + (1 << (j - 1))][j - 1] , spt[i][j - 1]);
}
}
}
int query(int l, int r)
{
if(l>r) return INT_MAX;
int j = (int)log2(r - l + 1);
///j = 31 - __builtin_clz(r - l+1);
return min (spt[l][j], spt[r - (1 << j) + 1][j]);
}
// Driver program
int main()
{
n = 9;
buildST();
cout << query(4, 7) << endl;
cout << query(7, 8) << endl;
return 0;
}
/// *** BIT O(Log(n)) space O(n)
/** 1 based index
* which functions has inverse function that can be solve bye BIT
* it works like consucative sums but in log(n)
*/
int n=SIZE; //of space;
void update(int idx,int val)//adding value val to idx index
{
while(idx<=n){
bitree[idx]+=val;
idx+=idx&(-idx); // Last set of digit
}
}
int query(int idx){// returns sum of [1,idx] index
int sum=0;
while(idx>0){
sum+=bitree[idx];
idx-=idx&(-idx);
}
return sum;
}
/**
Description : BIT range update range query
Time Complexity : all log(n)
*/
/// Remember to use 1 based indexing
//const int MX = 100005;
ll query(ll *bit, int indx)
{
ll sum = 0;
while (indx) {
sum += bit[indx];
indx -= (indx & -indx);
}
return sum;
}
void update(ll *bit, int indx, ll x)
{
while (indx < MX) {
bit[indx] += x;
indx += (indx & -indx);
}
}
ll B1[MX],B2[MX];//set 0
void Rupdate(int l, int r, ll v){
update(B1, l, v);
update(B1, r+1, -v);
update(B2, l, -((l-1)*v));
update(B2, r+1, r*v);
}
ll Rquery1(int p){
ll b1,b2;
b1 = query(B1, p);
b2 = query(B2, p);
return b1 * p + b2;
}
ll Requery(int l,int r){
return Rquery1(r)-Rquery1(l-1);
}
/// *** Segment Tree [log(total array size)*Query]
/** [ulow,uhigh] Query Range
* [low,high] total range of root
* [qlow,qhigh] Query Range
* Currrent position = pos
* 0 based Index And Root is also 0
*/
int ara[MX],seg[4*MX],lazy[4*MX];
void creat(int low,int high,int pos)
{
if(low==high){
seg[pos] = ara[low]; // reached leaf and update
return ;
}
int mid = (high+low)/2;
creat(low,mid,pos*2+1);
creat(mid+1,high,pos*2+2);
seg[pos] += seg[pos*2+1] + seg[pos*2+2];
}
void update(int low,int high,int ulow,int uhigh,int val,int pos)
{
if(low>high) return ;
if(lazy[pos]!=0){ /// is not propagated yet
seg[pos] += (high-low+1)*lazy[pos];
if(low!=high){ ///if not leaf node
lazy[pos*2+1] += lazy[pos];
lazy[pos*2+2] += lazy[pos];
}
lazy[pos] = 0;
}
if(ulow>high||uhigh<low) return; ///No overlap
if(ulow<=low&&uhigh>=high){ /// Total Overlap
seg[pos] += (high-low+1)*val;
if(low!=high){
lazy[pos*2+1] += val;
lazy[pos*2+2] += val;
}
return;
}
/// Partial overlap
int mid = (high+low)/2;
update(low,mid,ulow,uhigh,val,pos*2+1);
update(mid+1,high,ulow,uhigh,val,pos*2+2);
seg[pos] = seg[pos*2+1] + seg[pos*2+2]; /// Updating the intermediate node
}
int query(int low,int high,int qlow,int qhigh,int pos)
{
if(low>high) return 0;
if(lazy[pos]!=0){
seg[pos] += (high-low+1)*lazy[pos];
if(low!=high){
lazy[pos*2+1] += lazy[pos];
lazy[pos*2+2] += lazy[pos];
}
lazy[pos] = 0;
}
if(qlow>high||qhigh<low) return 0;
if(qlow<=low&&qhigh>=high)
return seg[pos];
int mid = (high+low)/2;
return query(low,mid,qlow,qhigh,pos*2+1) + query(mid+1,high,qlow,qhigh,pos*2+2);
}
/**
Description : Efficient and easy segment trees , Range [l, r)
Time Complexity : O(logn)
from: https://codeforces.com/blog/entry/18051
*/
const int N = 1e5; // limit for array size
int n; // array size
int tr[2 * N];
void build() { // build the tree
for (int i = n - 1; i > 0; --i) tr[i] = tr[i<<1] + tr[i<<1|1];
}
void modify(int p, int value) { // set value at position p
for (tr[p += n] = value; p > 1; p >>= 1) tr[p>>1] = tr[p] + tr[p^1];
}
int query(int l, int r) { // sum on interval [l, r)
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res += tr[l++];
if (r&1) res += tr[--r];
}
return res;
}
int main() {
//scanf("%d", &n);
int ar[]={1,3,4,5,4,5,6,4,6,4,5,6,6,5};
n=5;
for (int i = 0; i < n; ++i) {
tr[n+i]=ar[i];
}
build();
printf("%d\n", query(0, 4));//that means [0,3]
printf("%d\n", query(2, 3));
modify(0, 5);
printf("%d\n", query(0, 4));
return 0;
}
/**
* Name : Maximum subsegment sum
* Description: Segment Tree with custom merge function. .
* Usage: construct O(N), query O(lg(N)), update O(lg(N))
* Source: https://github.com/dragonslayerx
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 50100
#define INF -1000000000
struct node {
int sum;
int maxs, prefix, suffix;
node(){
sum = prefix = suffix = 0;
maxs = INF;
}
node(int sum, int maxs, int prefix, int suffix) {
setNode(sum, maxs, prefix, suffix);
}
void setNode(int sum, int maxs, int prefix, int suffix){
this->sum =sum;
this->maxs=maxs;
this->prefix=prefix;
this->suffix=suffix;
}
};
int a[MAX];
node st[4*MAX];
node merge(node left, node right){
node t;
t.prefix = max(left.prefix, left.sum+right.prefix);
t.suffix = max(right.suffix, right.sum+left.suffix);
t.sum = left.sum+right.sum;
t.maxs = left.maxs;
t.maxs = max(t.maxs, right.maxs);
t.maxs = max(t.maxs, left.suffix+right.prefix);
return t;
}
node construct(int n, int ll, int rl){
if (ll == rl) {
st[n].setNode(a[ll], a[ll], a[ll], a[ll]);
} else {
node left = construct(2*n+1, ll, (ll+rl)/2);
node right = construct(2*n+2, (ll+rl)/2+1, rl);
st[n] = merge(left, right);
}
return st[n];
}
node query(int n, int ll, int rl, int x, int y){
int mid = (ll+rl)/2;
if (x==ll && y==rl) return st[n];
else if (y <= mid) return query(2*n+1, ll, mid, x, y);
else if (x > mid) return query(2*n+2, mid+1, rl, x, y);
else {
node left = query(2*n+1, ll, (ll+rl)/2, x, mid);
node right = query(2*n+2, (ll+rl)/2+1, rl, mid+1, y);
return merge(left, right);
}
}
node update(int n, int ll, int rl, int p, int val){
if (p < ll || p > rl) return st[n];
if (p == ll && p == rl) {
st[n].setNode(val, val, val, val);
return st[n];
} else {
int mid = (ll+rl)/2;
node left = update(2*n+1, ll, (ll+rl)/2, p, val);
node right = update(2*n+2, (ll+rl)/2+1, rl, p, val);
st[n] = merge(left, right);
}
return st[n];
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", a+i);
construct(0, 0, n-1);
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
x--, y--;
printf("%d\n", query(0, 0, n-1, x, y).maxs);
}
}
© 2019 GitHub, Inc.
/**'
Name : Disjoint set union find
Description : Always check parent for size or anything
Complexity : O(n) ~ O(log n) ~ O(1)
*/
#define MX 10000
int rp[MX],sz[MX];
int parent(int n){
if(rp[n]==n)return n;
return rp[n]=parent(rp[n]);
}
void setUp(int a,int b){
a = parent(a);
b = parent(b);
if(a==b) return;
if(sz[a]<sz[b]){
rp[a] = rp[b];
sz[b] += sz[a];
}
else{
rp[b] = rp[a];
sz[a] += sz[b];
}
}
void init(){
for(int i=0;i<=MX;i++)
rp[i]=i,sz[i]=1;
}
/**
* Name : Dijoint Set with undo
* Description : DisjointSet (Makes a set of sets, merge sets, set membership, no. of sets, undo last operation,size of each component)
* Time Complexity : parent O(lg(N)), setUp O(lg(N)), undo O(1),
*/
#define MX 10000
int rp[MX],sz[MX];
int compo;
int pts[MX*2],in=0;
int parent(int n){
if(rp[n]==n)return n;
return rp[n]=parent(rp[n]);
}
// additionally storing parent which is connected to another parents
void setUp(int a,int b){
a = parent(a);
b = parent(b);
if(a==b){
pts[++in]=-1;
return;
}
if(sz[a]<sz[b]){
rp[a] = rp[b];
sz[b] += sz[a];
pts[++in]=a;
}
else{
rp[b] = rp[a];
sz[a] += sz[b];
pts[++in] = b;;
}
compo--;
}
void undo(){
if(!in) return;
int n = pts[in--];
if(n!=-1) {
sz[parent(rp[n])] -= sz[n];
rp[n]=n;
compo++;
}
}
void init(int n){
in=0;
for(int i=0;i<=MX;i++){
rp[i]=i;
sz[i]=1;
}
compo=n;
}
/**
Name : Trie with Dynamic memory
Time complexity : o((number of words)*(maximum lenght))
*/
/// Trie form shafaetsplanet
struct node {
bool endmark;
node* next[26 + 1];
node()
{
endmark = false;
for (int i = 0; i < 26; i++)
next[i] = NULL;
}
} * root;
void insert(char* str, int len)
{
node* curr = root;
for (int i = 0; i < len; i++) {
int id = str[i] - 'a';
if (curr->next[id] == NULL)
curr->next[id] = new node();
curr = curr->next[id];
}
curr->endmark = true;
}
bool search(char* str, int len)
{
node* curr = root;
for (int i = 0; i < len; i++) {
int id = str[i] - 'a';
if (curr->next[id] == NULL)
return false;
curr = curr->next[id];
}
return curr->endmark;
}
void del(node* cur)
{
for (int i = 0; i < 26; i++)
if (cur->next[i])
del(cur->next[i]);
delete (cur);
}
int main()
{
puts("ENTER NUMBER OF WORDS");
root = new node();
int num_word;
cin >> num_word;
for (int i = 1; i <= num_word; i++) {
char str[50];
scanf("%s", str);
insert(str, strlen(str));
}
puts("ENTER NUMBER OF QUERY";);
int query;
cin >> query;
for (int i = 1; i <= query; i++) {
char str[50];
scanf("%s", str);
if (search(str, strlen(str)))
puts("FOUND");
else
puts("NOT FOUND");
}
del(root);
return 0;
}
/// *** Trie[(number of words)*(maximum lenght)]
/**
Name : Trie with array
This trie is for All Uppercase & Lowercase letters
*/
int mp(char ch){
if(ch<95) return (int) (ch - 'A');
return (int) (ch - 'a' + 26);
}
struct node{
bool end;
int next[55];
int cnt; /// here cnt is counting how many element ends at this point
void set(){
cnt = 0;
end = false;
for(int i=0;i<=53;i++){
next[i] = 0;
}
}
}ara[MX];
int ptr;
void insert(char *st){
int i,in,x=0,y;
for(i=0;st[i];i++){
if(st[i]==' ') continue;
in = mp(st[i]);
/// If the chain is not exists
/// allocating memory by ptr++ that means new array element
if(!ara[x].next[in]){
///putting next arrays position at recent array's linked part
ara[x].next[in] = ptr++;
ara[ptr-1].set();
}
x = ara[x].next[in];
}
///marking last element as last
ara[x].end=1;
ara[x].cnt++;
}
int search(char *st){
int i,in,x=0,cn=0;
for(i=0;st[i];i++){
if(st[i]==' ')continue;
cn++;
in = mp(st[i]);
///if query element not exists then returning false
if(!ara[x].next[in]) return 0;
x = ara[x].next[in];
}
if(!cn) return 1;
/// returning how many elements like "st"
return ara[x].cnt;
}
/// *** Merge Sort Tree [nlog(n) + query*log(n)*log(n)]
int ara[MX];
vector<int>seg[MX*4];
/// 1 based index
int bns(int n,int val){
/// lower bound 1 2 2 2 3 4
/// then returns 2
int mid,i,j,low=0,high = seg[n].size()-1;
while((high-low)>4){
mid = (low+high)/2;
if(seg[n][mid]<=val) low = mid;
else high = mid - 1;
}
for(low;low<=high&&low<seg[n].size();low++){
if(seg[n][low]>val) break;
}
return seg[n].size()-low; /// numbers greater than value
}
void mergee(int x,int y,int z){ /// merging 2 vector x and y to Z in sorted order
int i,j,k,md,sz;
sz = seg[x].size() + seg[y].size();
for(i=0,j=0,k=0;k<sz;k++){
if(i>=seg[x].size()) seg[z].push_back(seg[y][j++]);
else if(j>=seg[y].size()) seg[z].push_back(seg[x][i++]);
else if(seg[x][i]<seg[y][j]) seg[z].push_back(seg[x][i++]);
else seg[z].push_back(seg[y][j++]);
}
}
/** [low,high] total range :: variable range
* [qlow,qhigh] query range
* pos = current position
*/
void creat(int low,int high,int pos){ /// creating merge sort tree
if(low==high){
seg[pos].push_back(ara[low]);
return ;
}
int mid = (low+high)/2;
creat(low,mid,pos*2);
creat(mid+1,high,pos*2+1);
mergee(pos*2,pos*2+1,pos);
/// merge with stl
/// merge(seg[pos*2].begin() , seg[pos*2].end(), seg[pos*2].begin(), seg[pos*2].end(),back_inserter(seg[pos]));
}
int query(int low,int high,int qlow,int qhigh,int pos,int val){
if(qlow>qhigh) return 0;
if(qlow>high||qhigh<low) return 0;
if(qlow<=low&&qhigh>=high){
return bns(pos,val);
}
int mid = (low + high)/2;
return query(low,mid,qlow,qhigh,pos*2,val) + query(mid+1,high,qlow,qhigh,pos*2+1,val);
}
/// *** For Rnage orders statistics (find k'th number in sorted segment)
vector<pii>input;
vector<int>seg[MX*4];
void creat(int low,int high,int pos){ /// creating merge sort tree
if(low==high){
seg[pos].push_back(input[low-1].second); /// in is 0 based
return ;
}
int mid = (low+high)/2;
creat(low,mid,pos*2);
creat(mid+1,high,pos*2+1);
mergee(pos*2,pos*2+1,pos);
}
/** calculating total number in left range lower than the given index
* if numbers are greater than equals to the searging value than look into left
* searhing on right sub array and substracting left sub arrys given up values
*/
int query(int low,int high,int qlow,int qhigh,int pos,int val)
{
if(low==high) return seg[pos][0];
int mid = (low+high)>>1,left=pos<<1;
int total = upper_bound(seg[left].begin(),seg[left].end(),qhigh) -
lower_bound(seg[left].begin(),seg[left].end(),qlow);
if(total>=val){
return query(low,mid,qlow,qhigh,pos*2,val);
}
else{
return query(mid+1,high,qlow,qhigh,pos*2+1,val-total);
}
}
sort(input.begin(),input.end());
/**
GRAPH! GRAPH! GRAPH! GRAPH! GRAPH! GRAPH!
GRAPH! GRAPH! GRAPH! GRAPH! GRAPH! GRAPH!
GRAPH! GRAPH! GRAPH! GRAPH! GRAPH! GRAPH!
GRAPH! GRAPH! GRAPH! GRAPH! GRAPH! GRAPH!
*/
/// *** BFS [ total(node + edge) ]
/// level by level travarse
vector <int> ed[MX];
int lev[MX],ms,par[MX];
bool vs[MX];
int bfs(int s){
int i,j,d,f,v;
queue <int> q;
q.push(s);
lev[s]=0;
vs[s]=1;
while(!q.empty()){
f=q.front();
q.pop();
for(i=0;i<ed[f].size();i++){
v=ed[f][i];
if(!vs[v]){
vs[v]=1;
q.push(v);
lev[v]=lev[f]+1;
}
}
}
return lev[destinition];
}
/// *** BFS in 2D Grid [edge + node]
int lev[MX][MX],m,n;
bool vs[MX][MX];
char st[MX][MX];
int dx[]={1,-1,0, 0, 1,1,};
int dy[]={0, 0,1,-1,-1, };
int bfs(int fx,int fy) /// starting position
{
int i,v,x,y,md=0;
queue <pii> q;
pii pr;
vs[fx][fy]=1;
lev[fx][fy]=0;
q.push(pii(fx,fy));
while(!q.empty()){
pr=q.front();
fx=pr.first;
fy=pr.second;
q.pop();
for(i=0;i<4;i++){
x=fx+dx[i];
y=fy+dy[i];
if(x<0||x>=n||y<0||y>=m)continue; /// for zero based index
if(!vs[x][y]&&st[x][y]!='#'){ /// # is blocked
q.push(pii(x,y));
vs[x][y]=1;
lev[x][y]=lev[fx][fy]+1;
if(st[x][y]=='d'){ /// d is destinition
md=max(md,lev[x][y]);
}
}
}
}
return md;/// max distance of d
}
/**
Description : dfs with coloring and visiting time
Complexity : O(V+E)
*/
vector<vector<int>> adj; // graph represented as an adjacency list
int n; // number of vertices
vector<int> color;
vector<int> time_in, time_out;
int dfs_timer = 0;
void dfs(int v) {
time_in[v] = dfs_timer++;
color[v] = 1;
for (int u : adj[v])
if (color[u] == 0)
dfs(u);
color[v] = 2;
time_out[v] = dfs_timer++;
}
/// *** Dijkstra O(edge * log (node))
struct node{
int id,cost;
node(){}
node(int nid,int ncost)
{
id=nid;
cost=ncost;
}
bool operator < (const node&x)const{
return cost>x.cost;
}
};
vector <int> ed[MX],ec[MX];
int ds[MX];
void dxt(int s){
priority_queue <node> q;
q.push(node(s,0));
ds[s]=0;
node fn;
int i,u,v;
while(!q.empty()){
fn=q.top();
q.pop();
u=fn.id;
if(fn.cost!=ds[u])continue;
for(i=0;i<ed[u].size();i++){
v=ed[u][i];
if(ds[v]>ds[u]+ec[u][i]){
ds[v]=ds[u]+ec[u][i];
q.push(node(v,ds[v]));
}
}
}
}
/**
Name : Warshal Algorithm
he key idea to notice here is that, we go through all
the nodes and for each node we try to make every path
better by going through that. Hence, Floyd Warshall can add vertex online.
n^2 loop can add this.
Time Complexity : O(n^3)
*/
int mtx[102][102],n;//intialize with inf;
int next[102][102];//for finding path only
void wrsl()
{
int i,j,k;
for(i=1;i<=n;i++){//for finding path only
for(j=1;j<=n;j++){
next[i][j]=j;
}
}
for(k=1;k<=n;k++){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(mtx[i][j]>mtx[i][k]+mtx[k][j]){
mtx[i][j]>mtx[i][k]+mtx[k][j];
next[i][j]=next[i][k];//for finding path only
}
}
}
}
}
//finding path using warshal, i to j
vector <int> path;
void findpath(int i,int j)
{
path.clear();
path.push_back(i);
while(i!=j){
i=next[i][j];
path.push_back(i);
}
}
/// **** Articulation points ****
/// O(E+V)
/// intiate root with starting position of tree
/// by checking marked points we will get articulation points
#define MX 10005
vector<int>ed[MX];
int vs[MX],dt[MX],points[MX],low[MX],pr[MX],discovery_time,root;
void articulation_point(int n){
int i,v,cn=0;
vs[n] = 1;
dt[n] = low[n] = ++discovery_time;
for(i=0;i<ed[n].size();i++){
v = ed[n][i];
if(pr[n]==v) continue; // won parent visit
// if backedge
if(vs[v]) low[n] = min(low[n],dt[v]);
else{
pr[v] = n;
articulation_point(v);
// storing min discovery discovery_time over all chaild
low[n] = min(low[n],low[v]);
if(dt[n]<=low[v]&&n!=root){
//then n is a articulation point
// v is a root of a devided subtree
points[n] = 1;
}
cn++;
}
}
//if root has more than one child then it is also a articulation_point
if(cn>1&&n==root) points[n]=1;
}
void free(){
for(int i=0;i<MX;i++){
ed[i].clear();
vs[i]=low[i]=dt[i]=points[i]=0;
pr[i]=-1;
}
discovery_time = 0;
}
/// ****Articulation Bridge ****
/// O(E+V)
/// bridges are stored on artqb vector
#define MX 10005
vector<int>ed[MX], artqb[MX];
int vs[MX],dt[MX],low[MX],pr[MX],discovery_time,root;
void articulation_bridge(int n){
int i,v;
vs[n] = 1;
dt[n] = low[n] = ++discovery_time;
for(i=0;i<ed[n].size();i++){
v = ed[n][i];
if(pr[n]==v) continue; // won parent visit