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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <utility> #ifdef XLor #define dbg(args...) do {cout << #args << " -> "; err(args);} while (0) #else #define dbg(...) #endif void err() {std::cout << std::endl;} template<typename T, typename...Args> void err(T a, Args...args){std::cout << a << ' '; err(args...);} #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 200000 + 5;
int pre[maxn]; int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); } void join(int x, int y) { x = find(x); y = find(y); if (x == y) return ; pre[x] = y; }
vector<int> edge[maxn]; int n, m, q;
int cnt, bel[maxn]; // important! namespace Tarjan { int tot, dfn[maxn], low[maxn], st[maxn], top, vis[maxn]; void clear(int n) { tot = top = cnt = 0; for (int i = 1; i <= n; i++) { edge[i].clear(); dfn[i] = vis[i] = bel[i] = 0; } } void dfs(int u, int f) { dfn[u] = low[u] = ++tot; st[++top] = u; vis[u] = 1; int k = 0; for (int& v: edge[u]) { if (v == f && !k) { k++; continue; } if (!dfn[v]) { dfs(v, u); low[u] = min(low[u], low[v]); } else if (vis[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { cnt++; int t = 0; do { t = st[top--]; bel[t] = cnt; vis[t] = 0; } while (t != u); } } void scc(int n, vector<int> * g) { for (int i = 1; i <= n; i++) if (!dfn[i]) Tarjan::dfs(i, 0); for (int i = 1; i <= n; i++) g[i].clear(); for (int i = 1; i <= n; i++) { int u = bel[i]; for (int& x: edge[i]) { int v = bel[x]; if (u != v) { g[u].push_back(v); } } } } }
namespace Tree { vector<int> block[maxn]; int tin[maxn], tout[maxn], vis[maxn], tot; int dp[maxn][22], dep[maxn]; void dfs(int u, int fa){ tin[u] = ++tot; vis[u] = 1; dp[u][0] = fa; dep[u] = dep[fa] + 1; for (int& v: block[u]) { if (v == fa) continue; dfs(v, u); } tout[u] = tot; } int qlca(int x, int y){ if (dep[x] < dep[y]) swap(x, y); int tmp = dep[x] - dep[y]; for (int i = 0; tmp; i++, tmp >>= 1) if (tmp & 1) x = dp[x][i]; if (x == y) return x; for (int i = 20; i >= 0; i--){ if (dp[x][i] != dp[y][i]){ x = dp[x][i]; y = dp[y][i]; } } return dp[x][0]; } void init() { for (int i = 0; i <= cnt; i++) { vis[i] = 0; ms(dp[i], 0); } dep[0] = tot = 0; for (int i = 1; i <= cnt; i++) if (!vis[i]) { dfs(i, 0); } for (int j = 1; j < 20; j++) for (int i = 1; i <= cnt; i++) dp[i][j] = dp[dp[i][j - 1]][j - 1]; } int check(int p, int u) { return tin[p] <= tin[u] && tout[p] >= tout[u]; } }
int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d%d", &n, &m, &q); Tarjan::clear(n); for (int i = 1; i <= n; i++) { pre[i] = i; } for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); edge[u].push_back(v); edge[v].push_back(u); join(u, v); } Tarjan::scc(n, Tree::block); Tree::init(); while (q--) { int u, v, w; scanf("%d%d%d", &u, &v, &w); if (find(u) != find(v) || find(u) != find(w)) { puts("No"); continue; } u = bel[u]; v = bel[v]; w = bel[w]; int g = Tree::qlca(v, w); if (Tree::check(g, u)) { if (Tree::check(u, v) || Tree::check(u, w)) puts("Yes"); else puts("No"); } else puts("No"); } } return 0; }
|