fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "searoutes"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 2e5 + 5;
  38. int numNode, numEdge, numQuery;
  39. vector<ii> G[N];
  40. set<int> adj[N];
  41. stack<int> st;
  42. int num[N], low[N], timer, scc[N], numScc, vis[N];
  43.  
  44. void dfs(int u, int old_id, int T) {
  45. vis[u] = T;
  46. num[u] = low[u] = ++timer;
  47. st.push(u);
  48. for(ii x : G[u]) {
  49. int v, i; tie(v, i) = x;
  50. if (i == old_id) continue;
  51. if (num[v]) minimize(low[u], num[v]);
  52. else {
  53. dfs(v, i, T);
  54. minimize(low[u], low[v]);
  55. }
  56. }
  57.  
  58. if (num[u] == low[u]) {
  59. numScc++;
  60. int v;
  61. do {
  62. v = st.top(); st.pop();
  63. scc[v] = numScc;
  64. } while(v != u);
  65. }
  66. }
  67.  
  68. bool answer;
  69. struct IT {
  70. int sz;
  71. vector<int> node, lazy;
  72. IT(int n = 0): sz(n), node(4 * n + 5, 0), lazy(4 * n + 5, 0) {}
  73.  
  74. int combine(int x, int y) {
  75. if (max(x, y) == inf) return inf;
  76. if (x == y) return x;
  77. if (x * y == 0) return (!x ? y : x);
  78. return inf;
  79. }
  80.  
  81. void pushDown(int id) {
  82. if (lazy[id]) {
  83. node[id << 1] = combine(node[id << 1], lazy[id]);
  84. node[id << 1 | 1] = combine(node[id << 1 | 1], lazy[id]);
  85. lazy[id << 1] = combine(lazy[id << 1], lazy[id]);
  86. lazy[id << 1 | 1] = combine(lazy[id << 1 | 1], lazy[id]);
  87. lazy[id] = 0;
  88. }
  89. }
  90.  
  91. void update(int id, int l, int r, int u, int v, int k) {
  92. if (l > v || r < u) return;
  93. if (u <= l && r <= v) {
  94. node[id] = combine(node[id], k);
  95. lazy[id] = combine(lazy[id], k);
  96. return;
  97. }
  98. pushDown(id);
  99. int mid = (l + r) >> 1;
  100. update(id << 1, l, mid, u, v, k);
  101. update(id << 1 | 1, mid + 1, r, u, v, k);
  102. node[id] = combine(node[id << 1], node[id << 1 | 1]);
  103. }
  104.  
  105. int getNode(int id, int l, int r, int u, int v) {
  106. if (l > v || r < u) return 0;
  107. if (u <= l && r <= v) return node[id];
  108. pushDown(id);
  109. int mid = (l + r) >> 1;
  110. return combine(getNode(id << 1, l, mid, u, v), getNode(id << 1 | 1, mid + 1, r, u, v));
  111. }
  112. } it[N];
  113.  
  114. int head[N], par[N][20], h[N], len[N], pos[N], sz[N];
  115.  
  116. void hld(int u, int p, bool create_chain, bool is_head) {
  117. if (!create_chain || p == -1) {
  118. sz[u] = 1;
  119. for(int v : adj[u]) if (v != p) {
  120. par[v][0] = u;
  121. h[v] = h[u] + 1;
  122. hld(v, u, false, false);
  123. sz[u] += sz[v];
  124. }
  125. }
  126. if (create_chain == false) return;
  127. head[u] = (is_head ? u : head[p]);
  128. pos[u] = ++len[head[u]];
  129. int hvy = 0;
  130. for(int v : adj[u]) if (v != p && sz[v] > sz[hvy])
  131. hvy = v;
  132. for(int v : adj[u]) if (v != p)
  133. hld(v, u, true, (v != hvy));
  134. }
  135.  
  136. int LCA(int u, int v) {
  137. if (h[u] < h[v]) swap(u, v);
  138. int s = h[u] - h[v];
  139. RED(j, 20) if (BIT(s, j))
  140. u = par[u][j];
  141. if (u == v) return u;
  142. RED(j, 20) if (par[u][j] != par[v][j]) {
  143. u = par[u][j];
  144. v = par[v][j];
  145. }
  146. return par[u][0];
  147. }
  148.  
  149. void modify(int u, int lca, int sign) {
  150. while(head[u] != head[lca]) {
  151. if (answer == false) return;
  152. it[head[u]].update(1, 1, len[head[u]], 1, pos[u], sign);
  153. u = par[head[u]][0];
  154. }
  155.  
  156. if (u != lca) {
  157. // cout << "CUR : " << u << ' ' << lca << ' ' << sign << ' ' << pos[lca] + 1 << ' ' << pos[u] << ' ' << head[u] << '\n';
  158. it[head[u]].update(1, 1, len[head[u]], pos[lca] + 1, pos[u], sign);
  159. }
  160. }
  161.  
  162. void check(int u, int lca) {
  163. while(head[u] != head[lca]) {
  164. if (answer == false) return;
  165. if (it[head[u]].getNode(1, 1, len[head[u]], 1, pos[u]) == inf)
  166. answer = false;
  167. u = par[head[u]][0];
  168. }
  169. if (u != lca && it[head[u]].getNode(1, 1, len[head[u]], pos[lca] + 1, pos[u]) == inf)
  170. answer = false;
  171. }
  172.  
  173. void init(void) {
  174. cin >> numNode >> numEdge >> numQuery;
  175. FOR(i, 1, numEdge) {
  176. int u, v;
  177. cin >> u >> v;
  178. G[u].pb(mp(v, i));
  179. G[v].pb(mp(u, i));
  180. }
  181.  
  182. int cnt = 0;
  183. FOR(i, 1, numNode) if (!num[i])
  184. dfs(i, -1, ++cnt);
  185. }
  186.  
  187. void process(void) {
  188. FOR(i, 1, numNode) for(ii v : G[i])
  189. if (scc[i] != scc[v.fi])
  190. adj[scc[i]].insert(scc[v.fi]);
  191. FOR(i, 1, numScc) if (!pos[i])
  192. hld(i, -1, true, true);
  193. FOR(j, 1, 19) FOR(i, 1, numScc)
  194. par[i][j] = par[par[i][j - 1]][j - 1];
  195.  
  196. FOR(i, 1, numScc) if (i == head[i])
  197. it[i] = IT(len[i]);
  198. answer = true;
  199. // cout << "DEBUG SCC : "; FOR(i, 1, numNode) cout << scc[i] << ' '; cout << '\n';
  200.  
  201. FOR(i, 1, numQuery) {
  202. int u, v;
  203. cin >> u >> v;
  204. if (vis[u] != vis[v]) answer = false;
  205. if (!answer) continue;
  206. u = scc[u]; v = scc[v];
  207. if (u == v) continue;
  208. int p = LCA(u, v);
  209. // cout << "UPDATE : " << u << ' ' << p << ' ' << -1 << '\n';
  210. // cout << "UPDATE : " << v << ' ' << p << ' ' << +1 << '\n';
  211. modify(u, p, -1);
  212. modify(v, p, +1);
  213. check(u, p);
  214. check(v, p);
  215. }
  216. cout << (answer ? "Yes\n" : "No\n");
  217. }
  218.  
  219. void reset() {
  220. FOR(i, 1, numNode) {
  221. G[i].clear();
  222. adj[i].clear();
  223. num[i] = low[i] = 0;
  224. sz[i] = pos[i] = head[i] = len[i] = h[i] = vis[i] = 0;
  225. it[i].node.clear();
  226. it[i].lazy.clear();
  227. }
  228. numScc = timer = 0;
  229. st = stack<int>();
  230. }
  231.  
  232. int main() {
  233. ios_base::sync_with_stdio(0);
  234. cin.tie(0); cout.tie(0);
  235. if (fopen(task".inp", "r")) {
  236. freopen(task".inp", "r", stdin);
  237. freopen(task".out", "w", stdout);
  238. }
  239. int tc = 1;
  240. cin >> tc;
  241. while(tc--) {
  242. init();
  243. process();
  244. reset();
  245. }
  246. return 0;
  247. }
  248.  
  249.  
Success #stdin #stdout 0.05s 45836KB
stdin
Standard input is empty
stdout
Yes