1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> #define MAX_N 100000 #define mid ((s+t)>>1) using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, T; bool ans[MAX_N+5]; int fa[MAX_N+5], fc[MAX_N+5], w[MAX_N+5]; struct edge {int u, v, l, r;} ; int getf(int x) {return fa[x] == x ? x : getf(fa[x]);} int getc(int x) {return fa[x] == x ? 0 : getc(fa[x])^fc[x];} void merge(int u, int v, int c, stack <int> &sta) { if (w[u] < w[v]) swap(u, v); sta.push(v), fa[v] = u, fc[v] = c, w[u] += w[v]; } void restore(stack <int> &sta) { for (int u; !sta.empty(); sta.pop()) u = sta.top(), w[fa[u]] -= w[u], fa[u] = u, fc[u] = 0; } void bi_solve(int s, int t, vector <edge> E) { vector <edge> El, Er; stack <int> sta; for (int i = 0; i < (int)E.size(); i++) if (s >= E[i].l && t <= E[i].r) { int u = getf(E[i].u), v = getf(E[i].v); int c = getc(E[i].u)^getc(E[i].v)^1; if (u^v) merge(u, v, c, sta); else if (c) { for (int j = s; j <= t; j++) ans[j] = false; restore(sta); return; } } else { if (E[i].l <= mid) El.push_back(E[i]); if (E[i].r >= mid+1) Er.push_back(E[i]); } if (s < t) bi_solve(s, mid, El), bi_solve(mid+1, t, Er); restore(sta); } int main() { read(n), read(m), read(T); vector <edge> E; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1, u, v, l, r; i <= m; i++) read(u), read(v), read(l), read(r), E.push_back((edge){u, v, l+1, r}); for (int i = 1; i <= T; i++) ans[i] = true; bi_solve(1, T, E); for (int i = 1; i <= T; i++) puts(ans[i] ? "Yes" : "No"); return 0; }
|