fork download
  1. // version1
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int n, m, q;
  7.  
  8. int p1, p2, p3, p4;
  9. vector < pair <int,int> > graph[1000007];
  10.  
  11. int l;
  12. int pre[1000007];
  13. int low[1000007];
  14.  
  15. vector <int> vec;
  16.  
  17. int bic[1000007];
  18.  
  19. int b;
  20. vector <int> bgraph[1000007];
  21. int bpre[1000007];
  22. int bpost[1000007];
  23.  
  24. int c;
  25. int conn[1000007];
  26. int dis[1000007];
  27.  
  28. vector <int> jump[1000007];
  29.  
  30. vector <int> subset;
  31. vector < pair <int,int> > edges;
  32.  
  33. int trans[1000007];
  34. vector <int> sta;
  35.  
  36. vector < pair <int,int> > ngraph[1000007];
  37. int bic2[1000007];
  38.  
  39. int res;
  40.  
  41. void dfs1(int v, int p)
  42. {
  43. l++;
  44. pre[v]=l;
  45. low[v]=l;
  46. sta.push_back(v);
  47. for (int i=0; i<graph[v].size(); i++)
  48. {
  49. if (graph[v][i].second==p)
  50. continue;
  51. if (pre[graph[v][i].first])
  52. {
  53. low[v]=min(low[v], pre[graph[v][i].first]);
  54. }
  55. else
  56. {
  57. dfs1(graph[v][i].first, graph[v][i].second);
  58. low[v]=min(low[v], low[graph[v][i].first]);
  59. }
  60. }
  61. if (pre[v]==low[v])
  62. {
  63. b++;
  64. while(sta.back()!=v)
  65. {
  66. bic[sta.back()]=b;
  67. sta.pop_back();
  68. }
  69. bic[sta.back()]=b;
  70. sta.pop_back();
  71. }
  72. }
  73.  
  74. void dfs2(int v, int p)
  75. {
  76. l++;
  77. bpre[v]=l;
  78. conn[v]=c;
  79.  
  80. jump[v].push_back(p);
  81. while(jump[v].back())
  82. {
  83. p1=jump[v].size()-1;
  84. p2=jump[v].back();
  85. jump[v].push_back(jump[p2][min(p1, (int)jump[p2].size()-1)]);
  86. }
  87.  
  88. for (int i=0; i<bgraph[v].size(); i++)
  89. {
  90. if (bgraph[v][i]!=p)
  91. {
  92. dis[bgraph[v][i]]=dis[v]+1;
  93. dfs2(bgraph[v][i], v);
  94. }
  95. }
  96. l++;
  97. bpost[v]=l;
  98. }
  99.  
  100. int lca(int v, int u)
  101. {
  102. if (conn[v]!=conn[u])
  103. return 0;
  104. for (int i=jump[v].size()-1; i>=0; i--)
  105. {
  106. if (i<jump[v].size() && dis[jump[v][i]]>=dis[u])
  107. {
  108. v=jump[v][i];
  109. }
  110. }
  111. for (int i=jump[u].size()-1; i>=0; i--)
  112. {
  113. if (i<jump[u].size() && dis[jump[u][i]]>=dis[v])
  114. {
  115. u=jump[u][i];
  116. }
  117. }
  118. for (int i=jump[v].size()-1; i>=0; i--)
  119. {
  120. if (i<jump[v].size() && i<jump[u].size() && jump[v][i]!=jump[u][i])
  121. {
  122. v=jump[v][i];
  123. u=jump[u][i];
  124. }
  125. }
  126. if (v!=u)
  127. v=jump[v][0];
  128. return v;
  129. }
  130.  
  131. bool comp(int v, int u)
  132. {
  133. return bpre[v]<bpre[u];
  134. }
  135.  
  136. void filtr()
  137. {
  138. vector <int> vec2=vec;
  139. vec.clear();
  140. sort(vec2.begin(), vec2.end(), comp);
  141. for (int i=0; i<vec2.size(); i++)
  142. {
  143. if (!vec2[i])
  144. continue;
  145. if (!i || vec2[i]!=vec2[i-1])
  146. {
  147. vec.push_back(vec2[i]);
  148. }
  149. }
  150. }
  151.  
  152. void dfs3(int v, int p)
  153. {
  154. l++;
  155. pre[v]=l;
  156. low[v]=l;
  157. sta.push_back(v);
  158. for (int i=0; i<ngraph[v].size(); i++)
  159. {
  160. if (ngraph[v][i].second==p)
  161. continue;
  162. if (pre[ngraph[v][i].first])
  163. {
  164. low[v]=min(low[v], pre[ngraph[v][i].first]);
  165. }
  166. else
  167. {
  168. dfs3(ngraph[v][i].first, ngraph[v][i].second);
  169. low[v]=min(low[v], low[ngraph[v][i].first]);
  170. }
  171. }
  172. if (pre[v]==low[v])
  173. {
  174. b++;
  175. while(sta.back()!=v)
  176. {
  177. bic2[sta.back()]=b;
  178. sta.pop_back();
  179. }
  180. bic2[sta.back()]=b;
  181. sta.pop_back();
  182. }
  183. }
  184.  
  185. long long R;
  186.  
  187. int rotate(int element)
  188. {
  189. element=(element+R)%n;
  190. if (element==0)
  191. element=n;
  192. return element;
  193. }
  194.  
  195. int main()
  196. {
  197. scanf("%d%d%d", &n, &m, &q);
  198. for (int i=1; i<=m; i++)
  199. {
  200. scanf("%d%d", &p1, &p2);
  201. graph[p1].push_back(make_pair(p2, i));
  202. graph[p2].push_back(make_pair(p1, i));
  203. }
  204. for (int i=1; i<=n; i++)
  205. {
  206. if (!pre[i])
  207. {
  208. dfs1(i, 0);
  209. }
  210. }
  211. for (int i=1; i<=n; i++)
  212. {
  213. for (int j=0; j<graph[i].size(); j++)
  214. {
  215. if (bic[i]!=bic[graph[i][j].first])
  216. {
  217. bgraph[bic[i]].push_back(bic[graph[i][j].first]);
  218. }
  219. }
  220. }
  221. l=0;
  222. for (int i=1; i<=b; i++)
  223. {
  224. if (!bpre[i])
  225. {
  226. c++;
  227. dfs2(i, 0);
  228. }
  229. }
  230. for (int h=1; h<=q; h++)
  231. {
  232. subset.clear();
  233. edges.clear();
  234. vec.clear();
  235. scanf("%d%d", &p1, &p2);
  236. while(p1--)
  237. {
  238. scanf("%d", &p3);
  239. p3=rotate(p3);
  240. subset.push_back(bic[p3]);
  241. vec.push_back(bic[p3]);
  242. }
  243. while(p2--)
  244. {
  245. scanf("%d%d", &p3, &p4);
  246. p3=rotate(p3);
  247. p4=rotate(p4);
  248. edges.push_back(make_pair(bic[p3], bic[p4]));
  249. vec.push_back(bic[p3]);
  250. vec.push_back(bic[p4]);
  251. }
  252. sort(vec.begin(), vec.end(), comp);
  253. for (int i=vec.size()-1; i; i--)
  254. {
  255. vec.push_back(lca(vec[i], vec[i-1]));
  256. }
  257. filtr();
  258. c=vec.size();
  259. for (int i=0; i<c; i++)
  260. {
  261. trans[vec[i]]=i+1;
  262. ngraph[i+1].clear();
  263. pre[i+1]=0;
  264. low[i+1]=0;
  265. }
  266. l=0;
  267. sta.clear();
  268. for (int i=0; i<c; i++)
  269. {
  270. while(!sta.empty() && bpost[vec[i]]>bpost[sta.back()])
  271. {
  272. sta.pop_back();
  273. }
  274. if (!sta.empty())
  275. {
  276. l++;
  277. ngraph[trans[vec[i]]].push_back(make_pair(trans[sta.back()], l));
  278. ngraph[trans[sta.back()]].push_back(make_pair(trans[vec[i]], l));
  279. }
  280. sta.push_back(vec[i]);
  281. }
  282. sta.clear();
  283. for (int i=0; i<edges.size(); i++)
  284. {
  285. l++;
  286. ngraph[trans[edges[i].first]].push_back(make_pair(trans[edges[i].second], l));
  287. ngraph[trans[edges[i].second]].push_back(make_pair(trans[edges[i].first], l));
  288. }
  289. b=0;
  290. l=0;
  291. for (int i=1; i<=c; i++)
  292. {
  293. if (!pre[i])
  294. {
  295. dfs3(i, 0);
  296. }
  297. }
  298. res=1;
  299. for (int i=1; i<subset.size(); i++)
  300. {
  301. if (bic2[trans[subset[i]]]!=bic2[trans[subset[0]]])
  302. {
  303. res=0;
  304. }
  305. }
  306. if (res)
  307. {
  308. printf("YES\n");
  309. R+=h;
  310. }
  311. else
  312. {
  313. printf("NO\n");
  314. }
  315. }
  316. return 0;
  317. }
  318.  
  319.  
  320.  
  321.  
  322. // version2
  323.  
  324. import java.io.*;
  325. import java.util.*;
  326.  
  327. public class G_bm_ac {
  328. FastScanner in;
  329. PrintWriter out;
  330.  
  331. class Solver {
  332. int n;
  333.  
  334. Solver(int n) {
  335. this.n = n;
  336. par = new int[n];
  337. bl = new int[n];
  338. comp = new int[n];
  339. size = new int[n];
  340. u = new int[n];
  341. init(n);
  342. }
  343.  
  344. int[] par;
  345. int[] bl;
  346. int[] comp;
  347. int[] size;
  348.  
  349. void init(int n) {
  350. for (int i = 0; i < n; ++i) {
  351. bl[i] = comp[i] = i;
  352. size[i] = 1;
  353. par[i] = -1;
  354. }
  355. }
  356.  
  357. int get(int v) {
  358. if (v == -1) {
  359. return -1;
  360. }
  361. return bl[v] == v ? v : (bl[v] = get(bl[v]));
  362. }
  363.  
  364. int get_comp(int v) {
  365. v = get(v);
  366. return comp[v] == v ? v : (comp[v] = get_comp(comp[v]));
  367. }
  368.  
  369. void make_root(int v) {
  370. v = get(v);
  371. int root = v, child = -1;
  372. while (v != -1) {
  373. int p = get(par[v]);
  374. par[v] = child;
  375. comp[v] = root;
  376. child = v;
  377. v = p;
  378. }
  379. size[root] = size[child];
  380. }
  381.  
  382. int cu;
  383. int[] u;
  384.  
  385. void merge_path(int a, int b) {
  386. ++cu;
  387.  
  388. ArrayList<Integer> va = new ArrayList<>();
  389. ArrayList<Integer> vb = new ArrayList<>();
  390. int lca = -1;
  391. for (;;) {
  392. if (a != -1) {
  393. a = get(a);
  394. va.add(a);
  395.  
  396. if (u[a] == cu) {
  397. lca = a;
  398. break;
  399. }
  400. u[a] = cu;
  401.  
  402. a = par[a];
  403. }
  404.  
  405. if (b != -1) {
  406. b = get(b);
  407. vb.add(b);
  408.  
  409. if (u[b] == cu) {
  410. lca = b;
  411. break;
  412. }
  413. u[b] = cu;
  414.  
  415. b = par[b];
  416. }
  417. }
  418.  
  419. for (int i = 0; i < va.size(); ++i) {
  420. bl[va.get(i)] = lca;
  421. if (va.get(i) == lca)
  422. break;
  423. }
  424. for (int i = 0; i < vb.size(); ++i) {
  425. bl[vb.get(i)] = lca;
  426. if (vb.get(i) == lca)
  427. break;
  428. }
  429. }
  430.  
  431. void addEdge(int a, int b) {
  432. a = get(a);
  433. b = get(b);
  434. if (a == b)
  435. return;
  436.  
  437. int ca = get_comp(a), cb = get_comp(b);
  438. if (ca != cb) {
  439. if (size[ca] > size[cb]) {
  440. int tmp = a;
  441. a = b;
  442. b = tmp;
  443. tmp = ca;
  444. ca = cb;
  445. cb = tmp;
  446. }
  447. make_root(a);
  448. par[a] = comp[a] = b;
  449. size[cb] += size[a];
  450. } else {
  451. merge_path(a, b);
  452. }
  453. }
  454.  
  455. }
  456.  
  457. int R = 0;
  458. int n;
  459.  
  460. int rot(int x) {
  461. x = (x + R) % n;
  462. if (x == 0) {
  463. x += n;
  464. }
  465. return x;
  466. }
  467.  
  468. class Forest {
  469. ArrayList<Integer>[] g;
  470. final int M = 20;
  471. int[][] up;
  472. int n;
  473. boolean[] alive;
  474. int time;
  475. int[] tin, tout;
  476. Solver sol;
  477.  
  478. Forest(Solver sol) {
  479. this.sol = sol;
  480. this.n = sol.n;
  481. alive = new boolean[n];
  482. g = new ArrayList[n];
  483. for (int i = 0; i < n; i++) {
  484. g[i] = new ArrayList<>();
  485. }
  486. up = new int[M][n];
  487. for (int i = 0; i < n; i++) {
  488. alive[i] = sol.get(i) == i;
  489. }
  490. for (int i = 0; i < n; i++) {
  491. if (alive[i] && sol.par[i] != -1) {
  492. int fr = i;
  493. int to = sol.get(sol.par[i]);
  494. g[fr].add(to);
  495. g[to].add(fr);
  496. }
  497. }
  498. tin = new int[n];
  499. tout = new int[n];
  500. for (int i = 0; i < n; i++) {
  501. if (alive[i] && sol.par[i] == -1) {
  502. go(i, i);
  503. }
  504. }
  505. }
  506.  
  507. void go(int v, int p) {
  508. tin[v] = time++;
  509. up[0][v] = p;
  510. for (int i = 1; i < M; i++) {
  511. up[i][v] = up[i - 1][up[i - 1][v]];
  512. }
  513. for (int to : g[v]) {
  514. if (to == p) {
  515. continue;
  516. }
  517. go(to, v);
  518. }
  519. tout[v] = time - 1;
  520. }
  521.  
  522. boolean isAnc(int anc, int v) {
  523. return tin[anc] <= tin[v] && tout[anc] >= tout[v];
  524. }
  525.  
  526. int lca(int x, int y) {
  527. if (isAnc(x, y)) {
  528. return x;
  529. }
  530. if (up[M - 1][x] != up[M - 1][y]) {
  531. return -1;
  532. }
  533. for (int i = M - 1; i >= 0; i--) {
  534. if (!isAnc(up[i][x], y)) {
  535. x = up[i][x];
  536. }
  537. }
  538. return up[0][x];
  539. }
  540. }
  541.  
  542. class Vertex {
  543. int oldId, newId;
  544. ArrayList<Vertex> g;
  545.  
  546. public Vertex(int oldId) {
  547. super();
  548. this.oldId = oldId;
  549. g = new ArrayList<>();
  550. }
  551.  
  552. @Override
  553. public String toString() {
  554. return "Vertex [oldId=" + oldId + ", newId=" + newId + ", g=" + g
  555. + "]";
  556. }
  557.  
  558. }
  559.  
  560. class Contraction {
  561. ArrayList<Vertex> vertices;
  562. Solver solver;
  563. Forest f;
  564.  
  565. void addEdge(Vertex v, Vertex u) {
  566. v.g.add(u);
  567. u.g.add(v);
  568. }
  569.  
  570. public Contraction(final Forest f, ArrayList<Integer> interesting) {
  571. this.f = f;
  572. Collections.sort(interesting, new Comparator<Integer>() {
  573. @Override
  574. public int compare(Integer o1, Integer o2) {
  575. return Integer.compare(f.tin[o1], f.tin[o2]);
  576. }
  577. });
  578. ArrayList<Vertex> stack = new ArrayList<>();
  579. int prevV = -1;
  580. vertices = new ArrayList<>();
  581. for (int v : interesting) {
  582. if (v == prevV) {
  583. continue;
  584. }
  585. prevV = v;
  586. while (stack.size() > 0) {
  587. Vertex last = stack.get(stack.size() - 1);
  588. if (f.isAnc(last.oldId, v)) {
  589. break;
  590. }
  591. int lca = f.lca(last.oldId, v);
  592. if (stack.size() > 1) {
  593. Vertex last2 = stack.get(stack.size() - 2);
  594. if (f.isAnc(last2.oldId, v)) {
  595. if (last2.oldId != lca) {
  596. if (lca == -1) {
  597. throw new AssertionError();
  598. }
  599. System.err.println(lca + "!");
  600. Vertex newV = new Vertex(lca);
  601. vertices.add(newV);
  602. addEdge(last, newV);
  603. stack.remove(stack.size() - 1);
  604. stack.add(newV);
  605. } else {
  606. addEdge(last, last2);
  607. stack.remove(stack.size() - 1);
  608. }
  609. } else {
  610. addEdge(last, last2);
  611. stack.remove(stack.size() - 1);
  612. }
  613. } else {
  614. if (lca != -1) {
  615. Vertex newV = new Vertex(lca);
  616. vertices.add(newV);
  617. addEdge(last, newV);
  618. stack.remove(stack.size() - 1);
  619. stack.add(newV);
  620. } else {
  621. stack.remove(stack.size() - 1);
  622. }
  623. }
  624. }
  625. Vertex newV = new Vertex(v);
  626. stack.add(newV);
  627. vertices.add(newV);
  628. }
  629. while (stack.size() > 1) {
  630. Vertex v1 = stack.get(stack.size() - 1);
  631. Vertex v2 = stack.get(stack.size() - 2);
  632. addEdge(v1, v2);
  633. stack.remove(stack.size() - 1);
  634. }
  635. Collections.sort(vertices, new Comparator<Vertex>() {
  636. @Override
  637. public int compare(Vertex o1, Vertex o2) {
  638. return Integer.compare(o1.oldId, o2.oldId);
  639. }
  640. });
  641. for (int i = 0; i < vertices.size(); i++) {
  642. vertices.get(i).newId = i;
  643. }
  644. solver = new Solver(vertices.size());
  645. for (int i = 0; i < vertices.size(); i++) {
  646. for (Vertex toV : vertices.get(i).g) {
  647. int fr = vertices.get(i).newId;
  648. int to = toV.newId;
  649. if (fr < to) {
  650. solver.addEdge(fr, to);
  651. }
  652. }
  653. }
  654. }
  655.  
  656. int getNewId(int id) {
  657. int left = 0, right = vertices.size();
  658. while (right - left > 1) {
  659. int mid = (left + right) >> 1;
  660. if (vertices.get(mid).oldId > id) {
  661. right = mid;
  662. } else {
  663. left = mid;
  664. }
  665. }
  666. return left;
  667. }
  668.  
  669. void addEdge(int fr, int to) {
  670. fr = getNewId(fr);
  671. to = getNewId(to);
  672. solver.addEdge(fr, to);
  673. }
  674.  
  675. boolean allSame(int[] ids) {
  676. int res = -1;
  677. for (int x : ids) {
  678. int y = getNewId(x);
  679. y = solver.get(y);
  680. if (res != -1 && res != y) {
  681. return false;
  682. }
  683. res = y;
  684. }
  685. return true;
  686. }
  687. }
  688.  
  689. void solve() {
  690. n = in.nextInt();
  691. int m = in.nextInt();
  692. int q = in.nextInt();
  693. Solver solver = new Solver(n);
  694. for (int i = 0; i < m; i++) {
  695. int fr = in.nextInt() - 1;
  696. int to = in.nextInt() - 1;
  697. solver.addEdge(fr, to);
  698. }
  699. Forest f = new Forest(solver);
  700. for (int i = 0; i < q; i++) {
  701. int ni = in.nextInt();
  702. int mi = in.nextInt();
  703. int[] vertices = new int[ni];
  704. for (int j = 0; j < ni; j++) {
  705. vertices[j] = f.sol.get(rot(in.nextInt()) - 1);
  706. }
  707. int[][] edges = new int[2][mi];
  708. for (int j = 0; j < mi; j++) {
  709. for (int k = 0; k < 2; k++) {
  710. edges[k][j] = f.sol.get(rot(in.nextInt()) - 1);
  711. }
  712. }
  713. ArrayList<Integer> interestingVertices = new ArrayList<>();
  714. for (int v : vertices) {
  715. interestingVertices.add(v);
  716. }
  717. for (int j = 0; j < mi; j++) {
  718. for (int k = 0; k < 2; k++) {
  719. interestingVertices.add(edges[k][j]);
  720. }
  721. }
  722. Contraction contr = new Contraction(f, interestingVertices);
  723. for (int j = 0; j < mi; j++) {
  724. contr.addEdge(edges[0][j], edges[1][j]);
  725. }
  726. boolean ok = contr.allSame(vertices);
  727. out.println(ok ? "YES" : "NO");
  728. R += ok ? (1 + i) : 0;
  729. R %= n;
  730. }
  731. }
  732.  
  733. void run() {
  734. try {
  735. in = new FastScanner(new File("object.in"));
  736. out = new PrintWriter(new File("object.out"));
  737.  
  738. solve();
  739.  
  740. out.close();
  741. } catch (FileNotFoundException e) {
  742. e.printStackTrace();
  743. }
  744. }
  745.  
  746. void runIO() {
  747.  
  748. in = new FastScanner(System.in);
  749. out = new PrintWriter(System.out);
  750.  
  751. solve();
  752.  
  753. out.close();
  754. }
  755.  
  756. class FastScanner {
  757. BufferedReader br;
  758. StringTokenizer st;
  759.  
  760. public FastScanner(File f) {
  761. try {
  762. br = new BufferedReader(new FileReader(f));
  763. } catch (FileNotFoundException e) {
  764. e.printStackTrace();
  765. }
  766. }
  767.  
  768. public FastScanner(InputStream f) {
  769. br = new BufferedReader(new InputStreamReader(f));
  770. }
  771.  
  772. String next() {
  773. while (st == null || !st.hasMoreTokens()) {
  774. String s = null;
  775. try {
  776. s = br.readLine();
  777. } catch (IOException e) {
  778. e.printStackTrace();
  779. }
  780. if (s == null)
  781. return null;
  782. st = new StringTokenizer(s);
  783. }
  784. return st.nextToken();
  785. }
  786.  
  787. boolean hasMoreTokens() {
  788. while (st == null || !st.hasMoreTokens()) {
  789. String s = null;
  790. try {
  791. s = br.readLine();
  792. } catch (IOException e) {
  793. e.printStackTrace();
  794. }
  795. if (s == null)
  796. return false;
  797. st = new StringTokenizer(s);
  798. }
  799. return true;
  800. }
  801.  
  802. int nextInt() {
  803. return Integer.parseInt(next());
  804. }
  805.  
  806. long nextLong() {
  807. return Long.parseLong(next());
  808. }
  809.  
  810. double nextDouble() {
  811. return Double.parseDouble(next());
  812. }
  813. }
  814.  
  815. public static void main(String[] args) {
  816. new G_bm_ac().runIO();
  817. }
  818. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:552:3: error: stray '@' in program
   @Override
   ^
prog.cpp:573:5: error: stray '@' in program
     @Override
     ^
prog.cpp:636:5: error: stray '@' in program
     @Override
     ^
prog.cpp:324:1: error: 'import' does not name a type
 import java.io.*;
 ^
prog.cpp:325:1: error: 'import' does not name a type
 import java.util.*;
 ^
prog.cpp:327:1: error: expected unqualified-id before 'public'
 public class G_bm_ac {
 ^
stdout
Standard output is empty