Beruflich Dokumente
Kultur Dokumente
void solve() {
vector<int> d (n, INF);
d[v] = 0; // It can be any source vertex
vector<int> p (n, -1); // parents
int x;
for (int i=0; i<n; ++i) { // looping for all vertices
x = -1;
for (int j=0; j<m; ++j) // looping al edges
if (d[e[j].a] < INF) // done for handling negative wieghts
if (d[e[j].b] > d[e[j].a] + e[j].cost) { // Relaxation condition
d[e[j].b] = max (-INF, d[e[j].a] + e[j].cost);
p[e[j].b] = e[j].a;
x = e[j].b;
}
}
if (x == -1)
cout << "No negative cycle from " << v;
else {
int y = x;
for (int i=0; i<n; ++i)
y = p[y];
vector<int> path;
for (int cur=y; ; cur=p[cur]) {
path.push_back (cur);
if (cur == y && path.size() > 1) break;
}
reverse (path.begin(), path.end());
}
int main ( ) {
scanf("%lld%lld",&N,&Edges) ; // v is the number of vertices and e is number of edges .
FOR( i , Edges ) {
Int u , v ,c ;
scanf("%lld%lld%lld",&u,&v,&c) ;
u-- ; v-- ;
if( v!= u ) { E[u][v] = 1 ; // IF graph is undirected.
E[v][u] = 1 ;
C[u][v] += c ;
C[v][u] += c ;
}
}
Int s , t ;
s = 0;
t = N-1;
cout<<Edmond_Karp( s, t ) ;
}
Extended Euclid
Writing the code for extended euclidean algorithm , can be used to find mod inverse when a and MOD
are coprime that is gcd = 1, x = inverse
extend_euclid ( ) {
int x = 1 ;
int xlast = 0 ;
int y = 0 ;
int ylast = 1 ;
while ( a ) {
q = b/a ;
r = b%a ;
m = xlast - q*x ;
n = ylast - q*y ;
x=m;
y=n;
a=r;
b=a;
}
}
string longestPalindrome(string s) {
string T = preProcess(s);
int n = T.length();
int *P = new int[n];
int C = 0, R = 0;
for (int i = 1; i < n-1; i++) {
int i_mirror = 2*C-i; // equals to i' = C - (i-C)
P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0;
while (T[i + 1 + P[i]] == T[i - 1 - P[i]])
P[i]++;
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
int maxLen = 0;
int centerIndex = 0;
for (int i = 1; i < n-1; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
}
delete[] P;
return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
}
Sparse Table
void process2(int M[MAXN][LOGMAXN], int A[MAXN], int N) {
int i, j;
for (i = 0; i < N; i++)
M[i][0] = i;
for (j = 1; 1 << j <= N; j++)
for (i = 0; i + (1 << j) - 1 < N; i++)
if (A[M[i][j - 1]] < A[M[i + (1 << (j - 1))][j - 1]])
M[i][j] = M[i][j - 1];
else
M[i][j] = M[i + (1 << (j - 1))][j - 1];
}
int RMQ( int i , int j ) {
int k = log2( j - i + 1 ) ;
if( A[M[i][k]] > A[M[ j - (1<<k) + 1 ][k]] )
return M[i][k] ;
else return M[ j - (1<<k) + 1 ][k] ;
}
Articulation Point
void dfs (int v, int p = -1) {
used[v] = true;
tin[v] = fup[v] = timer++;
int children = 0;
for (size_t i=0; i<g[v].size(); ++i) {
int to = g[v][i];
if (to == p) continue;
if (used[to])
fup[v] = min (fup[v], tin[to]);
else {
dfs (to, v);
fup[v] = min (fup[v], fup[to]);
if (fup[to] >= tin[v] && p != -1)
IS_CUTPOINT(v);
++children;
}
}
if (p == -1 && children > 1)
IS_CUTPOINT(v);
}
int main() {
timer = 0;
dfs (0)
}
Bridge
vector<int> g[MAXN];
bool used[MAXN];
int timer, tin[MAXN], fup[MAXN];
void find_bridges() {
timer = 0;
for (int i=0; i<n; ++i)
if (!used[i])
dfs (i);
}
int main(void)
{
int n[] = { 3, 5, 7 };
int a[] = { 2, 3, 2 };
printf("%d\n", chinese_remainder(n, a, sizeof(n)/sizeof(n[0])));
return 0;
}
General solution = ans + mult(n[0]:n[n – 1])k for every non-negative integer k
FOR(i, 1, n)
{
s.insert(a[i]);
it = s.upper_bound(a[i]);
if(it != s.end())
s.erase(it);
}
Topological Sort
Do a reverse DFS tree with postorder traversal!!!!!!!
Simple SCC
Do a TopoSort and a DFS with Toposort Order (don’t forget to compute the component each traversal)
Bipartite Matching
Just Color the node with Red-Blue, without 2 adjacent node with same color
Intesection Point
Minimum Enclosing Circle (EASY maranatha Qualification)
#include <cstdio>
#include <cmath>
int n;
double x[1005], y[1005], X, Y, d, e;
double dist(double a, double b) {
return a*a + b*b;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &x[i], &y[i]);
X += x[i]; Y += y[i];
}
X /= n; Y /= n;
double P = 0.1;
for (int i = 0; i < 30000; i++) {
int f = 0;
d = dist(X - x[0], Y - y[0]);
for (int j = 1; j < n; j++) {
e = dist(X - x[j], Y - y[j]);
if (d < e) { d = e; f = j; }
}
X += (x[f] - X)*P;
Y += (y[f] - Y)*P;
P *= 0.999;
}
printf("%.3lf %.3lf\n%.3lf", X, Y, sqrt(d));
}
Some geometric fact
Easy Josephus Problem from Quora (BEWARE OF OVERFLOW)
return maxRect;
}
Maximum binary sub matrix
Consider every row as a histogram
Binomial Coefficient
int binomialCoeff(int n, int k)
{
int res = 1;
// Since C(n, k) = C(n, n-k)
if ( k > n - k )
k = n - k;
return res;
}
Reserver for Bit magic
A |= (1 << i) (set ith bit),
A &= ~(1 << i) (unset ith bit),
A & (1 << i) (check if the ith bit is set),
A ^= (1 << i) (toggle the ith bit),
T = A & (-A) (LSB),
T = v && (v & (v – 1)) (true for 2^k),
A |= B (merge 2 bits),
__builtin_popcount(A) (count set bits on A),
__builtin_ctz(A) (count trailing zero on A),
__builtin_clz(A) (count leading zero on A)
MSB(long N)
{
//changing all right side bits to 1.
N = N| (N>>1);
N = N| (N>>2);
N = N| (N>>4);
N = N| (N>>8);
… 16 for 32 bit and 32 for 64 bit
return (N+1)>>1;
TIPS!!!
Dont panic!
Don’t ever rely on FAST-IO!!!
Avoid cin use scanf instead
If Kruskal got TLE, use Union by rank, if still TLE, try Prim
If int is enough, use it. Long long can cause TLE
Extra care is always needed when we deal with precision problems and special cases in geometry. There
are many nice little tricks to deal with these, which are beyond the scope of this cheat sheet.