Skip to content

Commit

Permalink
new file added on art. bridge/point
Browse files Browse the repository at this point in the history
  • Loading branch information
Asad committed Jun 29, 2023
1 parent 6c5f57c commit 0840a23
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 11 deletions.
29 changes: 18 additions & 11 deletions geometry/convex-hull.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
struct pt {
struct PT {
double x, y;

PT() {}
PT(double _x, double _y){
x = _x;
y = _y;
}
};

int orientation(pt a, pt b, pt c) {
int orientation(PT a, PT b, PT c) {
double v = a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y);
if (v < 0) return -1; // clockwise
if (v > 0) return +1; // counter-clockwise
return 0;
}

bool cw(pt a, pt b, pt c, bool include_collinear) {
bool cw(PT a, PT b, PT c, bool include_collinear) {
int o = orientation(a, b, c);
return o < 0 || (include_collinear && o == 0);
}
bool ccw(pt a, pt b, pt c, bool include_collinear) {
bool ccw(PT a, PT b, PT c, bool include_collinear) {
int o = orientation(a, b, c);
return o > 0 || (include_collinear && o == 0);
}

void convex_hull(vector<pt>& ara, bool include_collinear = false) {

// Be cautious about 'include_collinear
void convex_hull(vector<PT>& ara, bool include_collinear = false) {
if (ara.size() == 1)
return;

sort(ara.begin(), ara.end(), [](pt a, pt b) {
sort(ara.begin(), ara.end(), [](PT a, PT b) {
return make_pair(a.x, a.y) < make_pair(b.x, b.y);
});
pt p1 = ara[0], p2 = ara.back();
vector<pt> up, down;
PT p1 = ara[0], p2 = ara.back();
vector<PT> up, down;
up.push_back(p1);
down.push_back(p1);
for (int i = 1; i < (int)ara.size(); i++) {
if (i == ara.size() - 1 || cw(p1, ara[i], p2, include_collinear)) {
if (i == (int)ara.size() - 1 || cw(p1, ara[i], p2, include_collinear)) {
while (up.size() >= 2 && !cw(up[up.size()-2], up[up.size()-1], ara[i], include_collinear))
up.pop_back();
up.push_back(ara[i]);
}
if (i == ara.size() - 1 || ccw(p1, ara[i], p2, include_collinear)) {
if (i == (int)ara.size() - 1 || ccw(p1, ara[i], p2, include_collinear)) {
while (down.size() >= 2 && !ccw(down[down.size()-2], down[down.size()-1], ara[i], include_collinear))
down.pop_back();
down.push_back(ara[i]);
Expand Down
37 changes: 37 additions & 0 deletions graph/articulation-bridges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const int N = 2e5;
int tin[N+5], low[N+5];
bool vis[N+5];
vector<int> edge[N+5];
vector<pair<int, int> > art_bridge;

int timer = 0;
void dfs(int u, int p = -1){
tin[u] = low[u] = timer++;
vis[u] = 1;

for(int to : edge[u]){
if(vis[to]){
low[u] = min(low[u], tin[to]);
}
else{
dfs(to, u);

low[u] = min(low[u], low[to]);
if(low[to] > tin[u]){
art_bridge.push_back({u, to});
}
}
}
}
void cut_bridge(int n)
{
timer = 0;
art_bridge.clear();
for(int K = 1; K <= n; K++) vis[K] = 0;

for(int K = 1; K <= n; K++){
if(!vis[K]){
dfs(K);
}
}
}
77 changes: 77 additions & 0 deletions graph/articulation-point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const int N = 2e5;
int tin[N+5], low[N+5];
bool vis[N+5];
vector<int> art_point, edge[N+5];

int timer = 0;
void dfs(int u, int p = -1){
tin[u] = low[u] = timer++;
vis[u] = 1;

int child = 0;
for(int to : edge[u]){
if(to == p) continue;

if(vis[to]){
low[u] = min(low[u], tin[to]);
}
else{
dfs(to, u);

low[u] = min(low[u], low[to]);
if(low[to] >= tin[u] && p != -1){
art_point.push_back(u);
}
++child;
}
}
if(p == -1 && child >= 2){
art_point.push_back(u);
}
}
void cut_point(int n)
{
timer = 0;
art_point.clear();
for(int K = 1; K <= n; K++) vis[K] = 0;

for(int K = 1; K <= n; K++){
if(!vis[K]){
dfs(K);
}
}
}


































0 comments on commit 0840a23

Please sign in to comment.