This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void kmp(){ | |
int q = 0; // the position in P | |
int i = 0; // the position in T | |
while( i < n ){ // go through the entire array | |
if( P[q+1] == T[i+1] ){ // if a match continues, we increase the positions | |
q++; | |
i++; | |
if( q == m ){ // if q reached m, it means that we have a match | |
cout << "match from " << i-m+1 << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void computePi(){ | |
pi[1] = 0; | |
// compute pi[i] for all i | |
for( int i = 1; i <= m; ++i ){ | |
int w = pi[i]; | |
if ( P[i+1] == P[w+1] ) pi[i+1] = w + 1; // the "first" case | |
else { // the "second" case | |
while( w != 0 ){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<int> g[maxn]; | |
int id = 1; | |
int pre[maxn];// pre[v] is the label for the vertex v | |
int l[maxn];// l[v] is the lowest label what we can reach from the vertex v | |
void dfs(int v, int parent){ | |
pre[v] = id++; | |
l[v] = pre[v];// in the beginning this is the lowest label what we reach from here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<int> g[maxn]; | |
int pre[maxn]; | |
int id = 1; | |
void preorder(int v){ | |
pre[v] = id++; | |
for(int i = 0; i < g[v].size(); ++i){ | |
if( pre[g[v][i]] == 0 ) preorder(g[v][i]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Algorithm for finding the lowest common ancestor of two nodes in a tree | |
// with O(N log N) preprocessing and O(log N) query time | |
#include <bits/stdc++.h> | |
#define ll long long | |
#define FOR(i,n) for(int i=0;i<n;++i) | |
#define pb push_back | |
#define sz size | |
#define MAXN 100000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int number_of_components = 0 | |
for(int i=0;i<N;++i){ | |
if(explored[i]==false){ | |
dfs(i); | |
number_of_components++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn=100000; | |
vector<int> g[maxn]; | |
bool explored[maxn]; | |
void dfs(int v){ | |
explored[v]=true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stc++.h> | |
vector<int> g[MAXN];//Vectors storing our graph | |
int main(void){ | |
int n;//n - the number of vertexes in our graph | |
int m;//m - the number of edges in our graph | |
cin >> n >> m; | |
for(int i=0;i<m;++i){ | |
int a,b; |