forked from VidishJoshi/CompetitiveProgramming-Material
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aac3a1
commit 67d1676
Showing
47 changed files
with
4,684 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <bits/stdtr1c++.h> | ||
|
||
#define MAX 200010 | ||
#define clr(ar) memset(ar, 0, sizeof(ar)) | ||
#define read() freopen("lol.txt", "r", stdin) | ||
#define dbg(x) cout << #x << " = " << x << endl | ||
#define neg(x) ((x) <= n ? (x + n) : (x - n)) | ||
|
||
using namespace std; | ||
|
||
int n; | ||
bool visited[MAX], val[MAX]; | ||
int l, cmp, ar[MAX], num[MAX]; | ||
vector <int> adj[MAX], rev[MAX]; | ||
|
||
void topsort(int i){ | ||
visited[i] = true; | ||
int j, x, len = adj[i].size(); | ||
|
||
for (j = 0; j < len; j++){ | ||
x = adj[i][j]; | ||
if (!visited[x]) topsort(x); | ||
} | ||
ar[l++] = i; | ||
} | ||
|
||
void dfs(int i){ | ||
num[i] = cmp; | ||
visited[i] = true; | ||
int j, x, len = rev[i].size(); | ||
|
||
for (j = 0; j < len; j++){ | ||
x = rev[i][j]; | ||
if (!visited[x]) dfs(x); | ||
} | ||
} | ||
|
||
void SCC(){ | ||
int i, j, x; | ||
l = 0, cmp = 0; | ||
|
||
clr(visited); | ||
for (i = 0; i < n; i++){ | ||
if (!visited[i]) topsort(i); | ||
} | ||
|
||
clr(visited); | ||
for (i = l - 1; i >= 0; i--){ | ||
x = ar[i]; | ||
if (!visited[x]){ | ||
cmp++; | ||
dfs(x); | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
int T = 0, t, q, a, b; | ||
|
||
scanf("%d", &t); | ||
while (t--){ | ||
clr(adj), clr(rev); | ||
scanf("%d %d", &n, &q); | ||
|
||
while (q--){ | ||
scanf("%d %d", &a, &b); | ||
adj[a].push_back(b); | ||
rev[b].push_back(a); | ||
} | ||
|
||
SCC(); | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <bits/stdtr1c++.h> | ||
|
||
#define MAX 100010 | ||
#define clr(ar) memset(ar, 0, sizeof(ar)) | ||
#define read() freopen("lol.txt", "r", stdin) | ||
#define dbg(x) cout << #x << " = " << x << endl | ||
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a)) | ||
|
||
using namespace std; | ||
|
||
struct Point{ | ||
long long x, y; | ||
|
||
Point(){ | ||
} | ||
|
||
Point(long long xi, long long yi){ | ||
x = xi, y = yi; | ||
} | ||
}; | ||
|
||
struct Segment{ | ||
struct Point P1, P2; | ||
|
||
Segment(){ | ||
} | ||
|
||
Segment(struct Point P1i, struct Point P2i){ | ||
P1 = P1i, P2 = P2i; | ||
} | ||
}; | ||
|
||
/// Returns 0 if ABC is collinear, positive if ABC is a left turn, negative if ABC is a right turn | ||
long long ccw(struct Point A, struct Point B, struct Point C){ | ||
return ((B.x - A.x) * (C.y - A.y)) - ((C.x - A.x) * (B.y - A.y)); | ||
} | ||
|
||
/// Returns the shortest distance from Segment S to Point P | ||
double dis(struct Segment S, struct Point P){ | ||
double p, xx, yy; | ||
long long x = P.x, y = P.y, x1 = S.P1.x, y1 = S.P1.y, x2 = S.P2.x, y2 = S.P2.y; | ||
long long a = x - x1, b = y - y1, c = x2 - x1, d = y2 - y1, dot = (a * c) + (b * d), len = (c * c) + (d * d); | ||
|
||
if ((dot < 0) || (x1 == x2 && y1 == y2)) xx = x1, yy = y1; | ||
else if (dot > len) xx = x2, yy = y2; | ||
else p = (1.0 * dot) / len, xx = x1 + (p * c), yy = y1 + (p * d); | ||
xx = -xx + x, yy = -yy + y; | ||
return sqrt((xx * xx) + (yy * yy)); | ||
} | ||
|
||
/// Returns true if Point P lies on the Segment (both end-points inclusive) | ||
bool PointOnSeg(struct Segment S, struct Point P){ | ||
long long x = P.x, y = P.y, x1 = S.P1.x, y1 = S.P1.y, x2 = S.P2.x, y2 = S.P2.y; | ||
long long a = x - x1, b = y - y1, c = x2 - x1, d = y2 - y1, dot = (a * c) + (b * d), len = (c * c) + (d * d); | ||
|
||
if (x1 == x2 && y1 == y2) return (x1 == x && y1 == y); | ||
if (dot < 0 || dot > len) return false; | ||
return ((((x1 * len) + (dot * c)) == (x * len)) && (((y1 * len) + (dot * d)) == (y * len))); | ||
} | ||
|
||
int main(){ | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
Min Query (Point Update): | ||
|
||
int st[4*N]; | ||
|
||
void build(int node, int L, int R) | ||
{ | ||
if(L==R) | ||
{ | ||
st[node]=1e9; | ||
return; | ||
} | ||
int M=(L+R)/2; | ||
build(node*2, L, M); | ||
build(node*2+1, M+1, R); | ||
st[node]=min(st[node*2], st[node*2+1]); | ||
} | ||
|
||
int query(int node, int L, int R, int i, int j) | ||
{ | ||
if(j<L || i>R) | ||
return 1e9; | ||
if(i<=L && R<=j) | ||
return st[node]; | ||
int M=(L+R)/2; | ||
int left=query(node*2, L, M, i, j); | ||
int right=query(node*2 + 1, M+1, R, i, j); | ||
return min(left, right); | ||
} | ||
|
||
void update(int node, int L, int R, int pos, int val) | ||
{ | ||
if(L==R) | ||
{ | ||
st[node]=val; | ||
return; | ||
} | ||
int M=(L+R)/2; | ||
if(pos<=M) | ||
update(node*2, L, M, pos, val); | ||
else | ||
update(node*2 + 1, M+1, R, pos, val); | ||
st[node]=min(st[node*2], st[node*2 + 1]); | ||
} | ||
|
||
//Problem 1 (Max Query - Point Update with Coordinate Compression): http://codeforces.com/gym/100733/problem/F | ||
//Solution 1: http://codeforces.com/gym/100733/submission/41643795 | ||
|
||
//Problem 2 (Min Query - Offline processing): https://codeforces.com/problemset/problem/522/D | ||
//Solution 2: https://codeforces.com/contest/522/submission/45493164 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
#define MAX 50010 | ||
#define clr(ar) memset(ar, 0, sizeof(ar)) | ||
#define read() freopen("lol.txt", "r", stdin) | ||
|
||
int n, q; | ||
int tree[MAX << 2], lazy[MAX << 2]; | ||
|
||
void propagate(int idx, int a, int b){ | ||
if (lazy[idx]){ | ||
int p = (idx << 1), q = p + 1; | ||
int c = (a + b) >> 1, d = c + 1; | ||
|
||
tree[idx] += (lazy[idx] * (b - a + 1)); | ||
if (a != b){ | ||
lazy[p] += lazy[idx]; | ||
lazy[q] += lazy[idx]; | ||
} | ||
lazy[idx] = 0; | ||
} | ||
} | ||
|
||
void update(int* tree, int* lazy, int idx, int a, int b, int l, int r, int x){ | ||
int p = (idx << 1), q = p + 1; | ||
int c = (a + b) >> 1, d = c + 1; | ||
|
||
if (a == l && b == r) lazy[idx] += x; | ||
propagate(idx, a, b); | ||
if (a == l && b == r) return; | ||
|
||
if (r <= c){ | ||
propagate(q, d, b); | ||
update(tree, lazy, p, a, c, l, r, x); | ||
} | ||
else if (l >= d){ | ||
propagate(p, a, c); | ||
update(tree, lazy, q, d, b, l, r, x); | ||
} | ||
else{ | ||
update(tree, lazy, p, a, c, l, c, x); | ||
update(tree, lazy, q, d, b, d, r, x); | ||
} | ||
|
||
tree[idx] = tree[p] + tree[q]; | ||
} | ||
|
||
int query(int* tree, int* lazy, int idx, int a, int b, int l, int r){ | ||
int p = (idx << 1), q = p + 1; | ||
int c = (a + b) >> 1, d = c + 1; | ||
|
||
propagate(idx, a, b); | ||
if (a == l && b == r) return tree[idx]; | ||
if (r <= c) return query(tree, lazy, p, a, c, l, r); | ||
else if (l >= d) return query(tree, lazy, q, d, b, l, r); | ||
else{ | ||
int x = query(tree, lazy, p, a, c, l, c); | ||
int y = query(tree, lazy, q, d, b, d, r); | ||
return (x + y); | ||
} | ||
} | ||
|
||
void update_range(int l, int r, int v){ | ||
update(tree, lazy, 1, 1, n, l, r, v); | ||
} | ||
|
||
int query_range(int l, int r){ | ||
return query(tree, lazy, 1, 1, n, l, r); | ||
} | ||
|
||
int main(){ | ||
} |
Oops, something went wrong.