Stanford Python Codebook
Stanford Python Codebook
for (int &i = pt[u]; i < g[u].size(); ++i) { Edge &back = G[e.to][e.rev];
Edge &e = E[g[u][i]]; if (!ex[e.to] && f)
Edge &oe = E[g[u][i]^1]; hs[h[e.to]].push_back(e.to);
if (d[e.v] == d[e.u] + 1) { e.f -= f; ex[e.to] += f;
long long amt = e.cap - e.flow; back.f += f; ex[back.to] -= f;
if (flow != -1 && amt > flow) amt = flow; }
if (long long pushed = DFS(e.v, T, amt)) { vector<vector<int> > hs;
e.flow += pushed; vector<int> co;
oe.flow -= pushed; flow_t max_flow() {
return pushed; ex.assign(N, 0);
} h.assign(N, 0); hs.resize(2*N);
} co.assign(2*N, 0); cur.assign(N, 0);
} h[S] = N;
return 0; ex[T] = 1;
} co[0] = N-1;
long long MaxFlow(int S, int T) { for(auto &e:G[S]) add_flow(e, e.f);
long long total = 0; if(hs[0].size())
while (BFS(S, T)) { for (int hi = 0;hi>=0;) {
fill(pt.begin(), pt.end(), 0); int u = hs[hi].back();
while (long long flow = DFS(S, T)) hs[hi].pop_back();
total += flow; while (ex[u] > 0) { // discharge u
} if (cur[u] == G[u].size()) {
return total; h[u] = 1e9;
} for(unsigned int i=0;i<G[u].size();++i){
}; auto &e = G[u][i];
if (e.f && h[u] > h[e.to]+1){
h[u] = h[e.to]+1, cur[u] = i;
1.2 Min-cost Circulation }
}
// Runs in O(<max_flow > * log(V * max_edge_cost)) = O( if (++co[h[u]], !--co[hi] && hi < N)
V^3 * // log(V * C)) Really fast in // practice , 3e4 for(int i=0;i<N;++i)
edges are fine. Operates on integers , // costs are if (hi < h[i] && h[i] < N){
multiplied by N!! --co[h[i]];
template<typename flow_t = int, typename cost_t = int> h[i] = N + 1;
struct mcSFlow{ }
struct Edge{ hi = h[u];
cost_t c; } else if (G[u][cur[u]].f && h[u] == h[G[u][cur[
flow_t f; u]].to]+1)
int to, rev; add_flow(G[u][cur[u]], min(ex[u], G[u][cur[u
Edge(int _to, cost_t _c, flow_t _f, int _rev):c(_c), ]].f));
f(_f), to(_to), rev(_rev){} else ++cur[u];
}; }
static constexpr cost_t INFCOST = numeric_limits< while (hi>=0 && hs[hi].empty()) --hi;
cost_t>::max()/2; }
cost_t eps; return -ex[S];
int N, S, T; }
vector<vector<Edge> > G; void push(Edge &e, flow_t amt){
vector<unsigned int> isq, cur; if(e.f < amt) amt=e.f;
vector<flow_t> ex; e.f-=amt; ex[e.to]+=amt;
vector<cost_t> h; G[e.to][e.rev].f+=amt; ex[G[e.to][e.rev].to]-=amt;
mcSFlow(int _N, int _S, int _T):eps(0), N(_N), S(_S), }
T(_T), G(_N){} void relabel(int vertex){
void add_edge(int a, int b, cost_t cost, flow_t cap){ cost_t newHeight = -INFCOST;
assert(cap>=0); for(unsigned int i=0;i<G[vertex].size();++i){
assert(a>=0&&a<N&&b>=0&&b<N); Edge const&e = G[vertex][i];
if(a==b){assert(cost>=0); return;} if(e.f && newHeight < h[e.to]-e.c){
cost*=N; newHeight = h[e.to] - e.c;
eps = max(eps, abs(cost)); cur[vertex] = i;
G[a].emplace_back(b, cost, cap, G[b].size()); }
G[b].emplace_back(a, -cost, 0, G[a].size()-1); }
} h[vertex] = newHeight - eps;
void add_flow(Edge& e, flow_t f) {
3
IIIT-Delhi - Cogito Ergo Error
}
static constexpr int scale=2;
pair<flow_t, cost_t> minCostMaxFlow(){
1.3 Edmonds Max Matching
cost_t retCost = 0; #define MAXN 505
for(int i=0;i<N;++i) vector<int>g[MAXN];
for(Edge &e:G[i]) int pa[MAXN],match[MAXN],st[MAXN],S[MAXN],v[MAXN];
retCost += e.c*(e.f); int t,n;
//find max-flow inline int lca(int x,int y){
flow_t retFlow = max_flow(); for(++t;;swap(x,y)) if(x){
h.assign(N, 0); ex.assign(N, 0); if(v[x]==t)return x;
isq.assign(N, 0); cur.assign(N,0); v[x]=t, x=st[pa[match[x]]];
queue<int> q; }
for(;eps;eps>>=scale){ }
//refine #define qpush(x) q.push(x),S[x]=0
fill(cur.begin(), cur.end(), 0); void flower(int x,int y,int l,queue<int> &q){
for(int i=0;i<N;++i) while(st[x]!=l){
for(auto &e:G[i]) pa[x]=y, y=match[x];
if(h[i] + e.c - h[e.to] < 0 && e.f) push(e, e. if(S[y]==1) qpush(y);
f); st[x]=st[y]=l, x=pa[y];
for(int i=0;i<N;++i){ }
if(ex[i]>0){ }
q.push(i); bool bfs(int x){
isq[i]=1; for(int i=1;i<=n;++i)st[i]=i;
} memset(S+1,-1,sizeof(int)*n);
} queue<int>q;
// make flow feasible qpush(x);
while(!q.empty()){ while(q.size()){
int u=q.front();q.pop(); x=q.front(),q.pop();
isq[u]=0; for(size_t i=0;i<g[x].size();++i){
while(ex[u]>0){ int y=g[x][i];
if(cur[u] == G[u].size()) if(S[y]==-1){
relabel(u); pa[y]=x, S[y]=1;
for(unsigned int &i=cur[u], max_i = G[u].size if(!match[y]){
();i<max_i;++i){ for(int lst;x;y=lst,x=pa[y]){
Edge &e=G[u][i];
if(h[u] + e.c - h[e.to] < 0){ lst=match[x], match[x]=y, match[y]=x;
push(e, ex[u]); }
if(ex[e.to]>0 && isq[e.to]==0){ return 1;
q.push(e.to); }
isq[e.to]=1; qpush(match[y]);
} } else if(!S[y]&&st[y]!=st[x]){
if(ex[u]==0) break; int l=lca(y,x);
} flower(y,x,l,q); flower(x,y,l,q);
} }
} }
} }
if(eps>1 && eps>>scale==0){ return 0;
eps = 1<<scale; }
} int blossom(){
} int ans=0;
for(int i=0;i<N;++i){ for(int i=1;i<=n;++i)
for(Edge &e:G[i]){ if(!match[i]&&bfs(i))++ans;
retCost -= e.c*(e.f); return ans;
} }
} int main(){
return make_pair(retFlow, retCost/2/N); while(m--){
} g[x].push_back(y);
flow_t getFlow(Edge const &e){ g[y].push_back(x);
return G[e.to][e.rev].f; }
} printf("%d\n",blossom());
}; for(int i=1;i<=n;i++)printf("%d ",match[i]);
}
4
IIIT-Delhi - Cogito Ergo Error
2 Geometry
// determine if lines from a to b and c to d are
2.1 Miscellaneous geometry parallel or collinear
bool LinesParallel(PT a, PT b, PT c, PT d) {
double INF = 1e100,EPS = 1e-12; return fabs(cross(b-a, c-d)) < EPS;
}
struct PT {
double x, y; bool LinesCollinear(PT a, PT b, PT c, PT d) {
PT() {} return LinesParallel(a, b, c, d)
PT(double x, double y) : x(x), y(y) {} && fabs(cross(a-b, a-c)) < EPS
PT(const PT &p) : x(p.x), y(p.y) {} && fabs(cross(c-d, c-a)) < EPS;
PT operator + (const PT &p) const { return PT(x+p.x, }
y+p.y); } // determine if line segment from a to b intersects with
PT operator - (const PT &p) const { return PT(x-p.x, // line segment from c to d
y-p.y); } bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
PT operator * (double c) const { return PT(x*c, if (LinesCollinear(a, b, c, d)) {
y*c ); }
PT operator / (double c) const { return PT(x/c, if (dist2(a, c) < EPS || dist2(a, d) < EPS ||
y/c ); } dist2(b, c) < EPS || dist2(b, d) < EPS) return
}; true;
if (dot(c-a, c-b) > 0 && dot(d-a, d-b) > 0 && dot(c-
double dot(PT p, PT q) { return p.x*q.x+p.y*q.y; } b, d-b) > 0)
double dist2(PT p, PT q) { return dot(p-q,p-q); } return false;
double cross(PT p, PT q) { return p.x*q.y-p.y*q.x; } return true;
ostream &operator<<(ostream &os, const PT &p) { }
os << "(" << p.x << "," << p.y << ")"; if (cross(d-a, b-a) * cross(c-a, b-a) > 0) return
} false;
// rotate a point CCW or CW around the origin if (cross(a-c, d-c) * cross(b-c, d-c) > 0) return
PT RotateCCW90(PT p) { return PT(-p.y,p.x); } false;
PT RotateCW90(PT p) { return PT(p.y,-p.x); } return true;
PT RotateCCW(PT p, double t) { }
return PT(p.x*cos(t)-p.y*sin(t), p.x*sin(t)+p.y*cos(t) // compute intersection of line passing through a and b
); // with line passing through c and d, assuming that
} unique
// project point c onto line through a and b // intersection exists; for segment intersection, check
// assuming a != b if
PT ProjectPointLine(PT a, PT b, PT c) { // segments intersect first
return a + (b-a)*dot(c-a, b-a)/dot(b-a, b-a); PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
} b=b-a; d=c-d; c=c-a;
assert(dot(b, b) > EPS && dot(d, d) > EPS);
// project point c onto line segment through a and b return a + b*cross(c, d)/cross(b, d);
PT ProjectPointSegment(PT a, PT b, PT c) { }
double r = dot(b-a,b-a);
if (fabs(r) < EPS) return a; // compute center of circle given three points
r = dot(c-a, b-a)/r; PT ComputeCircleCenter(PT a, PT b, PT c) {
if (r < 0) return a; b=(a+b)/2;
if (r > 1) return b; c=(a+c)/2;
return a + (b-a)*r; return ComputeLineIntersection(b, b+RotateCW90(a-b), c
} , c+RotateCW90(a-c));
}
// compute distance from c to segment between a and b // determine if point is in a possibly non-convex
double DistancePointSegment(PT a, PT b, PT c) { polygon (by William
return sqrt(dist2(c, ProjectPointSegment(a, b, c))); // Randolph Franklin); returns 1 for strictly interior
} points, 0 for
// compute distance between point (x,y,z) and plane ax+ // strictly exterior points, and 0 or 1 for the
by+cz=d remaining points.
double DistancePointPlane(double x, double y, double z, // Note that it is possible to convert this into an *
double a, double b, double c, exact* test using
double d) // integer arithmetic by taking care of the division
{ appropriately
return fabs(a*x+b*y+c*z-d)/sqrt(a*a+b*b+c*c); // (making sure to deal with signs properly) and then by
} writing exact
6
IIIT-Delhi - Cogito Ergo Error
// tests for checking point on polygon boundary // counterclockwise fashion. Note that the centroid is
bool PointInPolygon(const vector<PT> &p, PT q) { often known as
bool c = 0; // the "center of gravity" or "center of mass".
for (int i = 0; i < p.size(); i++){ double ComputeSignedArea(const vector<PT> &p) {
int j = (i+1)%p.size(); double area = 0;
if ((p[i].y <= q.y && q.y < p[j].y || for(int i = 0; i < p.size(); i++) {
p[j].y <= q.y && q.y < p[i].y) && int j = (i+1) % p.size();
q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) area += p[i].x*p[j].y - p[j].x*p[i].y;
/ (p[j].y - p[i].y)) }
c = !c; return area / 2.0;
} }
return c; double ComputeArea(const vector<PT> &p) {
} return fabs(ComputeSignedArea(p));
// determine if point is on the boundary of a polygon }
bool PointOnPolygon(const vector<PT> &p, PT q) { PT ComputeCentroid(const vector<PT> &p) {
for (int i = 0; i < p.size(); i++) PT c(0,0);
if (dist2(ProjectPointSegment(p[i], p[(i+1)%p.size() double scale = 6.0 * ComputeSignedArea(p);
], q), q) < EPS) for (int i = 0; i < p.size(); i++){
return true;
return false; int j = (i+1) % p.size();
} c = c + (p[i]+p[j])*(p[i].x*p[j].y - p[j].x*p[i].y);
}
// compute intersection of line through points a and b return c / scale;
with }
// circle centered at c with radius r > 0
vector<PT> CircleLineIntersection(PT a, PT b, PT c, // tests whether or not a given polygon (in CW or CCW
double r) { order) is simple
vector<PT> ret; bool IsSimple(const vector<PT> &p) {
b = b-a; for (int i = 0; i < p.size(); i++) {
a = a-c; for (int k = i+1; k < p.size(); k++) {
double A = dot(b, b); int j = (i+1) % p.size();
double B = dot(a, b); int l = (k+1) % p.size();
double C = dot(a, a) - r*r; if (i == l || j == k) continue;
double D = B*B - A*C; if (SegmentsIntersect(p[i], p[j], p[k], p[l]))
if (D < -EPS) return ret; return false;
ret.push_back(c+a+b*(-B+sqrt(D+EPS))/A); }
if (D > EPS) }
ret.push_back(c+a+b*(-B-sqrt(D))/A); return true;
return ret; }
}
// compute intersection of circle centered at a with
radius r 2.2 3D geometry
// with circle centered at b with radius R
vector<PT> CircleCircleIntersection(PT a, PT b, double r public class Geom3D {
, double R) { // distance from point (x, y, z) to plane aX + bY + cZ
vector<PT> ret; + d = 0
double d = sqrt(dist2(a, b)); public static double ptPlaneDist(double x, double y,
if (d > r+R || d+min(r, R) < max(r, R)) return ret; double z,
double x = (d*d-R*R+r*r)/(2*d); double a, double b, double c, double d) {
double y = sqrt(r*r-x*x); return Math.abs(a*x + b*y + c*z + d) / Math.sqrt(a*a
PT v = (b-a)/d; + b*b + c*c);
ret.push_back(a+v*x + RotateCCW90(v)*y); }
if (y > 0) // distance between parallel planes aX + bY + cZ + d1
ret.push_back(a+v*x - RotateCCW90(v)*y); = 0 and
return ret; // aX + bY + cZ + d2 = 0
} public static double planePlaneDist(double a, double b
// This code computes the area or centroid of a ( , double c,
possibly nonconvex) double d1, double d2) {
// polygon, assuming that the coordinates are listed in return Math.abs(d1 - d2) / Math.sqrt(a*a + b*b + c*c
a clockwise or );
}
7
IIIT-Delhi - Cogito Ergo Error
// distance from point (px, py, pz) to line (x1, y1, return sqrt(pow(a.first-b.first,2)+pow(a.second-b.
z1)-(x2, y2, z2) second,2));
// (or ray, or segment; in the case of the ray, the }
endpoint is the vector<PT> convexhull(vector<PT> a){
// first point) vector<PT> hull;
public static final int LINE = 0; sort(a.begin(),a.end(),[](PT i, PT j){
public static final int SEGMENT = 1; if(i.second!=j.second)
public static final int RAY = 2; return i.second < j.second;
public static double ptLineDistSq(double x1, double y1 return i.first < j.first;
, double z1, });
double x2, double y2, double z2, double px, double for(int i=0;i<a.size();++i){
py, double pz, while(hull.size()>1 && cross(hull[hull.size()-2],
int type) { hull.back(),a[i])<=0)
double pd2 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1 hull.pop_back();
-z2)*(z1-z2); hull.push_back(a[i]);
}
double x, y, z; for(int i=a.size()-1, siz = hull.size();i--;){
if (pd2 == 0) { while(hull.size()>siz && cross(hull[hull.size()-2],
x = x1; hull.back(),a[i])<=0)
y = y1; hull.pop_back();
z = z1; hull.push_back(a[i]);
} else { }
double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1) + ( return hull;
pz-z1)*(z2-z1)) / pd2; }
x = x1 + u * (x2 - x1);
y = y1 + u * (y2 - y1);
z = z1 + u * (z2 - z1);
if (type != LINE && u < 0) {
2.4 Min Enclosing Circle
x = x1; // Minimum enclosing circle, Welzl’s algorithm
y = y1; // Expected linear time.
z = z1; // If there are any duplicate points in the input, be
} sure to remove them first.
if (type == SEGMENT && u > 1.0) { struct point {
x = x2; double x;
y = y2; double y;
z = z2; };
} struct circle {
} double x;
return (x-px)*(x-px) + (y-py)*(y-py) + (z-pz)*(z-pz) double y;
; double r;
} circle() {}
circle(double x, double y, double r): x(x), y(y)
public static double ptLineDist(double x1, double y1, , r(r) {}
double z1, };
double x2, double y2, double z2, double px, double circle b_md(vector<point> R) {
py, double pz, if (R.size() == 0) {
int type) { return circle(0, 0, -1);
return Math.sqrt(ptLineDistSq(x1, y1, z1, x2, y2, z2 } else if (R.size() == 1) {
, px, py, pz, type)); return circle(R[0].x, R[0].y, 0);
} } else if (R.size() == 2) {
} return circle((R[0].x+R[1].x)/2.0,
(R[0].y+R[1].y)/2.0,
hypot(R[0].x-R
2.3 Convex hull [1].x, R
[0].y-R[1].
typedef pair<long long, long long> PT; y)/2.0);
inline long long cross(PT o, PT a, PT b){ } else {
PT OA = {a.first-o.first,a.second-o.second}; double D = (R[0].x - R[2].x)*(R[1].y - R
PT OB = {b.first-o.first,b.second-o.second}; [2].y) - (R[1].x - R[2].x)*(R[0].y -
return OA.first*OB.second - OA.second*OB.first; R[2].y);
} double p0 = (((R[0].x - R[2].x)*(R[0].x
inline long double dist(PT a, PT b){ + R[2].x) + (R[0].y - R[2].y)*(R[0].y
8
IIIT-Delhi - Cogito Ergo Error
cnt.push_back(0); for(;--i;F[i]=F[i]==F[i-1]&&G[i]==G[i-1]&&S[i-1]<n-L
last = 0; );
for(p=B[*S]=0;++i<n;B[S[i]]=p=F[i]?p:i);
for(int i=0;i<s.size();i++) { for(fill_n(G,n,0);i--;F[i]=S[i]<L?-1:B[S[i]-L]);
// construct r for(iota(C,C+n,0);++i<n;~F[i]?G[i]=C[F[i]]++:0);
edges.push_back(map<char,int>()); for(copy_n(S,n,F);i--;F[i]<L?0:S[G[i]]=F[i]-L);
length.push_back(i+1); }
link.push_back(0); vector<int>res(S,S+n);
cnt.push_back(1); delete[] M;
int r = edges.size() - 1; return res;
// add edges to r and find p with link to q }
int p = last; vector<int> kasai(string &s, vector<int> &sa){
while(p >= 0 && !edges[p].count(s[i])){ int n = s.size();
edges[p][s[i]] = r; vector<int> lcp(n),inv(n);
p = link[p]; for(int i=0;i<n;++i) inv[sa[i]] = i;
} for(int i=0,k=0;i<n;++i){
if(p != -1) { if(k<0) k = 0;
int q = edges[p][s[i]]; if(inv[i]==n-1){ k=0; continue; }
if(length[p] + 1 == length[q]) { for(int j=sa[inv[i]+1];max(i,j)+k<n&&s[i+k]==s[j+k
// we do not have to split q, just set the ];++k);
correct suffix link lcp[inv[i]] = k--;
link[r] = q; }
} else { return lcp;
// we have to split, add q’ }
edges.push_back(edges[q]); // copy edges of q
length.push_back(length[p] + 1);
link.push_back(link[q]); // copy parent of q 5.3 Z Algorithm
cnt.push_back(0);
int qq = edges.size()-1; vector<int> compute_Z(string s) {
// add qq as the new parent of q and r int n = s.length();
link[q] = link[r] = qq; cnt[r] = 1; vector<int> z(n, 0);
// move short classes pointing to q to point z[0] = n;
to q’ for (int i = 1, l = 0, r = 0; i < n; ++i) {
while(p >= 0 && edges[p][s[i]] == q) { if (r >= i) {
edges[p][s[i]] = qq; z[i] = min(z[i - l], r - i + 1);
p = link[p]; }
} while (i+z[i] < n and s[i+z[i]] == s[z[i]]) {
} ++z[i];
} }
last = r; if (i + z[i] - 1 > r) {
} r = i + z[i] - 1;
l = i;
vector<int> ind(length.size()); }
iota(ind.begin(), ind.end(), 0); }
sort(ind.begin(), ind.end(), [&](int i, int j){ return z;
return length[i] > length[j]; }
});
for(auto i:ind) if(link[i] >= 0)
cnt[link[i]] += cnt[i]; 5.4 KMP
} vector<int> find_prefix(const vector<int> &P){
}; int M = P.size();
vector<int> pi(M);
5.2 Suffix array /* pi[i] <- largest prefix P[0..pi[i]] which is a
suffix of P[0..i]
vector<int> suffix_array(string &A){ * (but not equal to it) */
int n=A.size(),i=n, *M=new int[5*n]; pi[0] = -1;
int *B=M,*C=M+n,*F=M+2*n,*G=M+3*n,*S=M+4*n; for (int i = 1, k = -1; i < M; ++i) {
for(;i--;S[i]=n-i-1) B[i]=A[i]; while(k > -1 && P[k + 1] != P[i])
stable_sort(S,S+n,[&](int i,int j){return A[i]<A[j];}) k = pi[k];
; if (P[k + 1] == P[i]) ++k;
for(int L=1,p;L<n;L*=2){ pi[i] = k;
for(;++i<n;F[i]=B[S[i]],G[i]=B[S[i]+L/2]); }
16
IIIT-Delhi - Cogito Ergo Error
} return true;
bool solve() { }
val.assign(2*n, 0); valid.assign(1, 0);
for(int i=0; i<val.size(); i+=2) { public:
if(!valid[abs(val[i])]) { // Add the or-clause: (a or b)
valid.push_back(1); void add_or(int a, int b) {
if(!dfs(i)) { add_edge(-a,b);
valid.back()=0; add_edge(-b,a);
valid.push_back(1); }
if(!dfs(i+1)) return false; // Add the implication: a -> b
} void add_implication(int a, int b) {
} add_or(-a, b);
} }
return true;
} // Add condition: x is true
}; void add_true(int x) {
// Taken from https://github.com/dacin21/ add_or(x,x);
dacin21_codebook/blob/master/dfs_stuff/2sat.cpp }
// 2-sat in linear time via backtracking. // At most one with linear number of clauses
class Two_Sat { template<typename T>
int N; // number of variables void add_at_most_one(T vars) {
vector<int> val; // assignment of x is at val[2x] if(vars.begin() == vars.end()) return;
and -x at val[2x+1] int last = *vars.begin();
vector<char> valid; // changes made at time i are int cur = 0;
kept iff valid[i] for(int const&e:vars){
vector<vector<int> > G; // graph of implications G[x if(e == last) continue;
][i] = y means (x -> y) if(cur == 0) cur = e;
Two_Sat(int N_) : N(N_) { // create a formula over N else {
variables (numbered 1 to N) add_or(-cur, -e);
G.resize(2*N); int new_cur = add_variable();
} cur = add_implication(cur, new_cur);
add_implication(e, new_cur);
int add_variable() { cur = new_cur;
G.emplace_back(); }
G.emplace_back(); }
return N++; if(cur != 0){
} add_or(-cur, -last);
private: }
// converts a signed variable index to its position }
in val[] and G[] bool solve() {
int to_ind(int x) { val.assign(2*n, 0);
return 2*(abs(x)-1) + (x<0); valid.assign(1, 0);
} for(int i=0; i<val.size(); i+=2) {
// Add a directed edge to the graph. if(!valid[abs(val[i])]) {
// You most likely do not want to call this yourself valid.push_back(1);
! if(!dfs(i)) {
void add_edge(int a, int b) { valid.back()=0;
G[to_ind(a)].push_back(to_ind(b)); valid.push_back(1);
} if(!dfs(i+1)) return false;
}
int time() { }
return valid.size()-1; }
} return true;
}
bool dfs(int x) { };
if(valid[abs(val[x])]) return val[x]>0;
val[x] = time();
val[x^1] = -time();
for(int e:G[x])
7.2 Merge Insertion
if(!dfs(e)) // Sorting in O(n^2) time with near-optimal number of
return false; comparisons
21
IIIT-Delhi - Cogito Ergo Error