diff --git a/Articulation_Bridges.cpp b/Articulation_Bridges.cpp new file mode 100644 index 0000000..6fee151 --- /dev/null +++ b/Articulation_Bridges.cpp @@ -0,0 +1,61 @@ +#include +#define turbo() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define ll long long +#define mod (1000000007) +#define inf ((ll)1e18) +#define eps (1e-9) +#define ff first +#define ss second +#define pb push_back +#define endl "\n" +using namespace std; + +vector> adj; +vector dfs_low , dfs_num , dfs_parent; +vector > res; +int dfscnt = 1; +void dfs(int u) +{ + dfs_low[u] = dfs_num[u] = dfscnt++; + for(int x : adj[u]) + { + if(dfs_num[x] == 0) + { + dfs_parent[x] = u; + dfs(x); + if(dfs_low[x] > dfs_num[u]) + res.push_back({u , x}); + dfs_low[u] = min(dfs_low[u] , dfs_low[x]); + } + else if(x != dfs_parent[u]) + dfs_low[u] = min(dfs_low[u] , dfs_num[x]); + + } +} + +int main() +{ + int n = 10 ; // number of vertices; + // cin>>n; + int m = 15 ; // number of edges; + // cin>>m; + adj.resize(n + 1); + dfs_low.resize(n + 1); + dfs_num.resize(n + 1); + dfs_parent.resize(n + 1); + // store the adj list int adj; + for(int i = 0 ; i < m ; i++ ) + { + int x , y; + cin>>x>>y; + adj[x].pb(y); + adj[y].pb(x); + } + dfs(1); + cout<<"Bridges:\n"; + for(auto x : res) + { + cout< table; - - Gauss() - { - table = vector (bits, 0); - } - - Gauss(int _bits) - { - bits = _bits; - table = vector (bits, 0); - } - - int size() - { - int ans = 0; - for(int i = 0; i < bits; i++) - { - if(table[i]) - ans++; - } - return ans; - } - - bool can(int x) - { - for(int i = bits - 1; i >= 0; i--) - x = min(x, x ^ table[i]); - return x == 0; - } - - void add(int x) - { - for(int i = bits - 1; i >= 0 && x; i--) - { - if(table[i] == 0) - { - table[i] = x; - x = 0; - } - else - x = min(x, x ^ table[i]); - } - } - - int getBest() - { - int x = 0; - for(int i = bits - 1; i >= 0; i--) - x = max(x, x ^ table[i]); - return x; - } - - void merge(Gauss &other) - { - for(int i = bits - 1; i >= 0; i--) - add(other.table[i]); - } -}; - -//Logic: https://math.stackexchange.com/questions/48682/maximization-with-xor-operator -//Source: https://codeforces.com/profile/tfg - -//Problem 1: http://codeforces.com/contest/959/problem/F -//Solution 1: https://codeforces.com/contest/959/submission/50314871 - -//Problem 2: https://codeforces.com/contest/1101/problem/G -//Solution 2: https://codeforces.com/contest/1101/submission/50315103 - -//Problem 3: https://atcoder.jp/contests/abc141/tasks/abc141_f -//Solution 3: https://atcoder.jp/contests/abc141/submissions/15096631 - -//Problem 4: https://codeforces.com/contest/1336/problem/E1 (MITM) -//Solution 4: https://codeforces.com/contest/1336/submission/76891926 +struct Gauss +{ + int bits = 60; + vector table; + + Gauss() + { + table = vector (bits, 0); + } + + Gauss(int _bits) + { + bits = _bits; + table = vector (bits, 0); + } + + int size() + { + int ans = 0; + for(int i = 0; i < bits; i++) + { + if(table[i]) + ans++; + } + return ans; + } + + bool can(int x) + { + for(int i = bits - 1; i >= 0; i--) + x = min(x, x ^ table[i]); + return x == 0; + } + + void add(int x) + { + for(int i = bits - 1; i >= 0 && x; i--) + { + if(table[i] == 0) + { + table[i] = x; + x = 0; + } + else + x = min(x, x ^ table[i]); + } + } + + int getBest() + { + int x = 0; + for(int i = bits - 1; i >= 0; i--) + x = max(x, x ^ table[i]); + return x; + } + + void merge(Gauss &other) + { + for(int i = bits - 1; i >= 0; i--) + add(other.table[i]); + } +}; + +//Logic: https://math.stackexchange.com/questions/48682/maximization-with-xor-operator +//Source: https://codeforces.com/profile/tfg + +//Problem 1: http://codeforces.com/contest/959/problem/F +//Solution 1: https://codeforces.com/contest/959/submission/50314871 + +//Problem 2: https://codeforces.com/contest/1101/problem/G +//Solution 2: https://codeforces.com/contest/1101/submission/50315103 + +//Problem 3: https://atcoder.jp/contests/abc141/tasks/abc141_f +//Solution 3: https://atcoder.jp/contests/abc141/submissions/15096631 + +//Problem 4: https://codeforces.com/contest/1336/problem/E1 (MITM) +//Solution 4: https://codeforces.com/contest/1336/submission/76891926 diff --git a/Sweep Line: Intersecting Line Segments.cpp b/Sweep Line: Intersecting Line Segments.cpp deleted file mode 100644 index 0856c3b..0000000 --- a/Sweep Line: Intersecting Line Segments.cpp +++ /dev/null @@ -1,224 +0,0 @@ -/*Returns pair of indices of any two intersecting line segments if there exist intersecting line segments, -and (-1, -1) if no line segments intersect.*/ - -const double EPS = 1e-15; - -struct Point -{ - double x, y; - - Point() - { - x = 0, y = 0; - } - - Point(double _x, double _y) - { - x = _x, y = _y; - } - - bool between(Point &P, Point &Q) - { - bool checkX = x < max(P.x, Q.x) + EPS && x + EPS > min(P.x, Q.x); - bool checkY = y < max(P.y, Q.y) + EPS && y + EPS > min(P.y, Q.y); - return checkX && checkY; - } - - Point operator+ (const Point& P) const - { - return Point(x + P.x, y + P.y); - } - - Point operator- (const Point& P) const - { - return Point(x - P.x, y - P.y); - } - - bool operator< (Point &o) - { - if(abs(x - o.x) > EPS) - return x < o.x; - return y < o.y; - } -}; - -struct Line -{ - double a, b, c; - - Line(Point P, Point Q) - { - if(abs(P.x - Q.x) < EPS) - { - a = 1; - b = 0; - c = -P.x; - } - else - { - a = (P.y - Q.y) / (Q.x - P.x); - b = 1.0; - c = -(a * P.x + P.y); - } - } - - bool parallel(Line l) - { - return abs(a - l.a) < EPS && abs(b - l.b) < EPS; - } - - Point intersect(Line l) - { - if(parallel(l)) - return Point(); - double x = (b * l.c - c * l.b) / (a * l.b - b * l.a); - double y; - if(abs(b) < EPS) - y = -l.a * x - l.c; - else - y = -a * x - c; - } -}; - - -struct Segment -{ - Point P, Q; - int idx; - - Segment() - { - idx = -1; - } - - Segment(double x1, double y1, double x2, double y2, int _idx) - { - P = Point(x1, y1); - Q = Point(x2, y2); - idx = _idx; - } - - Segment(Point _P, Point _Q, int _idx) - { - P = _P, Q = _Q, idx = _idx; - } - - Point intersect(Segment ls) - { - Line l1 = Line(P, Q); - Line l2 = Line(ls.P, ls.Q); - if(l1.parallel(l2)) - return Point(); - Point c = l1.intersect(l2); - return c.between(P, Q) && c.between(ls.P, ls.Q) ? c : Point(); - } - - double get_y(double &x) const - { - if(abs(P.x - Q.x) EPS) - return x < e.x; - if(type != e.type) - return type > e.type; - return idx < e.idx; - } -}; - -int vec(const Point &a, const Point &b, const Point &c) -{ - double s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); - if(abs(s) < EPS) - return 0; - return s > 0 ? 1 : -1; -} - -bool intersect1D(double l1, double r1, double l2, double r2) -{ - if(l1 > r1) - swap(l1, r1); - if(l2 > r2) - swap(l2, r2); - return max(l1, l2) <= min(r1, r2) + EPS; -} - -bool intersect(const Segment &a, const Segment &b) -{ - return intersect1D(a.P.x, a.Q.x, b.P.x, b.Q.x) && - intersect1D(a.P.y, a.Q.y, b.P.y, b.Q.y) && - vec(a.P, a.Q, b.P) * vec(a.P, a.Q, b.Q) <= 0 && - vec(b.P, b.Q, a.P) * vec(b.P, b.Q, a.Q) <=0; -} - -bool operator<(const Segment& a, const Segment& b) -{ - double x = max(min(a.P.x, a.Q.x), min(b.P.x, b.Q.x)); - return a.get_y(x) < b.get_y(x) - EPS; -} - -set s; -vector::iterator > where; - -set::iterator prev(set::iterator it) -{ - return it == s.begin() ? s.end() : --it; -} - -set::iterator next(set::iterator it) -{ - return ++it; -} - -pair solve(const vector &v) -{ - int n = v.size(); - set events; - for(int i=0;i::iterator nxt = s.lower_bound(v[idx]), prv = prev(nxt); - if(nxt != s.end() && intersect(*nxt, v[idx])) - return make_pair(nxt->idx, v[idx].idx); - if(prv != s.end() && intersect(*prv, v[idx])) - return make_pair(prv->idx, v[idx].idx); - where[idx] = s.insert(nxt, v[idx]); - } - else - { - set::iterator nxt = next(where[idx]), prv = prev(where[idx]); - if(nxt != s.end() && prv != s.end() && intersect(*nxt, *prv)) - return make_pair(prv->idx, nxt->idx); - s.erase(where[idx]); - } - } - return make_pair(-1, -1); -} - -//Problem 1: http://acm.timus.ru/problem.aspx?space=1&num=1469 -//Solution 1: http://p.ip.fi/hRY9