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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <iostream> #include <vector> #define maxn 100005 using namespace std; void tarjan(int u); void merge(int u, int v); int find(int u); void add_query(int x, int y, int num); struct query { int to, num; query(int _to, int _num) { this->to = _to; this->num = _num; } }; vector<query> Gq[maxn]; vector<int> G[maxn]; int Q[5 * maxn];
int deep[maxn]; int k[maxn]; int fa[maxn]; bool vis[maxn]; int main() { cin.tie(0); cout.tie(0); cin.sync_with_stdio(0); int n, u, v; cin >> n; for(int i = 1; i < n; i++) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); fa[i] = i; } fa[n] = n; int q, x, y, a, b; cin >> q; for(int i = 0; i < q; i++) { cin >> x >> y >> a >> b >> (k[i]); add_query(a, b, 5 * i); add_query(x, a, 5 * i + 1); add_query(y, b, 5 * i + 2); add_query(x, b, 5 * i + 3); add_query(y, a, 5 * i + 4); } tarjan(1); bool ok; for(int i = 0; i < q; i++) { ok = false; int ab = Q[5 * i], xa = Q[5 * i + 1], yb = Q[5 * i + 2], xb = Q[5 * i + 3], ya = Q[5 * i + 4]; if(((k[i] + ab) & 1) == 0 && k[i] >= ab) { ok = true; } else if(((k[i] + xa + yb + 1) & 1) == 0 && k[i] >= xa + yb + 1) { ok = true; } else if(((k[i] + xb + ya + 1) & 1) == 0 && k[i] >= xb + ya + 1) { ok = true; } cout << (ok ? "YES" : "NO") << endl; } return 0; } void tarjan(int u) { vis[u] = true; for(int i = 0; i < G[u].size(); i++) { if(vis[G[u][i]]) continue; deep[G[u][i]] = deep[u] + 1; tarjan(G[u][i]); merge(G[u][i], u); } for(int i = 0; i < Gq[u].size(); i++) { if(!vis[Gq[u][i].to]) continue; Q[Gq[u][i].num] = deep[u] + deep[Gq[u][i].to] - 2 * deep[ find(Gq[u][i].to) ]; } }
void add_query(int x, int y, int num) { Gq[x].push_back(query(y, num)); Gq[y].push_back(query(x, num)); }
void merge(int u, int v) { fa[find(u)] = find(v); } int find(int u) { return (fa[u] == u ? u : fa[u] = find(fa[u])); }
|