fork(1) download
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4.  
  5. /**
  6.  * Created by Surya Vamsi on 21-Jul-15.
  7.  */
  8. class A {
  9. static Map<Integer,Set<Integer>>nodes;
  10. static boolean[] vis;
  11. static int end;
  12.  
  13. public static void main(String[] args) {
  14. InputReader in = new InputReader(System.in);
  15. solve(in, out);
  16. out.close();
  17. }
  18.  
  19. static void solve(InputReader in, PrintWriter out) {
  20. int n = in.nextInt();
  21. nodes = new HashMap<>();
  22. while (true) {
  23. String[] temp = in.readLine().split(" ");
  24. //System.out.println(Arrays.toString(temp));
  25. if (temp[0].equals("-1")) return;
  26. //System.out.println(temp[0]);
  27. if (temp[0].equals("C")) {
  28. int x = Integer.parseInt(temp[1]);
  29. int y = Integer.parseInt(temp[2]);
  30. addConnection(x,y);
  31. } else if (temp[0].equals("Q")) {
  32. int a = Integer.parseInt(temp[1]);
  33. int b = Integer.parseInt(temp[2]);
  34. if(isIn(a,b))out.println("Yes");
  35. else out.println("No");
  36. }
  37. }
  38. }
  39. static void addConnection(int a,int b){
  40. Set<Integer>setA=nodes.get(a);Set<Integer>setB=nodes.get(b);
  41. if(setA==null && setB==null){
  42. Set<Integer>set=new HashSet<>();set.add(a);set.add(b);
  43. nodes.put(a,set);nodes.put(b,set);
  44. }
  45. else if(setA==null){
  46. setB.add(a);
  47. nodes.put(a,setB);
  48. }
  49. else if(setB==null){
  50. setA.add(b);nodes.put(b,setA);
  51. }
  52. else if(setA!=setB){
  53. setA.addAll(setB);
  54. for(Integer x:nodes.keySet()){
  55. if(nodes.get(x)==setB){
  56. nodes.put(x,setA);
  57. }
  58. }
  59. }
  60. }
  61. static boolean isIn(int a,int b){
  62. return nodes.get(a).contains(b);
  63. }
  64.  
  65. /*static void print() {
  66.   for (int i = 1; i < list.length; i++) {
  67.   System.out.println(i + " " + list[i]);
  68.   }
  69.   }*/
  70.  
  71.  
  72. static class InputReader {
  73. private boolean finished = false;
  74.  
  75. private InputStream stream;
  76. private byte[] buf = new byte[1024];
  77. private int curChar;
  78. private int numChars;
  79. private SpaceCharFilter filter;
  80.  
  81. public InputReader(InputStream stream) {
  82. this.stream = stream;
  83. }
  84.  
  85. public int read() {
  86. if (numChars == -1)
  87. throw new InputMismatchException();
  88. if (curChar >= numChars) {
  89. curChar = 0;
  90. try {
  91. numChars = stream.read(buf);
  92. } catch (IOException e) {
  93. throw new InputMismatchException();
  94. }
  95. if (numChars <= 0)
  96. return -1;
  97. }
  98. return buf[curChar++];
  99. }
  100.  
  101. public int peek() {
  102. if (numChars == -1)
  103. return -1;
  104. if (curChar >= numChars) {
  105. curChar = 0;
  106. try {
  107. numChars = stream.read(buf);
  108. } catch (IOException e) {
  109. return -1;
  110. }
  111. if (numChars <= 0)
  112. return -1;
  113. }
  114. return buf[curChar];
  115. }
  116.  
  117. public int nextInt() {
  118. int c = read();
  119. while (isSpaceChar(c))
  120. c = read();
  121. int sgn = 1;
  122. if (c == '-') {
  123. sgn = -1;
  124. c = read();
  125. }
  126. int res = 0;
  127. do {
  128. if (c < '0' || c > '9')
  129. throw new InputMismatchException();
  130. res *= 10;
  131. res += c - '0';
  132. c = read();
  133. } while (!isSpaceChar(c));
  134. return res * sgn;
  135. }
  136.  
  137. public long readLong() {
  138. int c = read();
  139. while (isSpaceChar(c))
  140. c = read();
  141. int sgn = 1;
  142. if (c == '-') {
  143. sgn = -1;
  144. c = read();
  145. }
  146. long res = 0;
  147. do {
  148. if (c < '0' || c > '9')
  149. throw new InputMismatchException();
  150. res *= 10;
  151. res += c - '0';
  152. c = read();
  153. } while (!isSpaceChar(c));
  154. return res * sgn;
  155. }
  156.  
  157. public String readString() {
  158. int c = read();
  159. while (isSpaceChar(c))
  160. c = read();
  161. StringBuilder res = new StringBuilder();
  162. do {
  163. if (Character.isValidCodePoint(c))
  164. res.appendCodePoint(c);
  165. c = read();
  166. } while (!isSpaceChar(c));
  167. return res.toString();
  168. }
  169.  
  170. public boolean isSpaceChar(int c) {
  171. if (filter != null)
  172. return filter.isSpaceChar(c);
  173. return isWhitespace(c);
  174. }
  175.  
  176. public static boolean isWhitespace(int c) {
  177. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  178. }
  179.  
  180. private String readLine0() {
  181. StringBuilder buf = new StringBuilder();
  182. int c = read();
  183. while (c != '\n' && c != -1) {
  184. if (c != '\r')
  185. buf.appendCodePoint(c);
  186. c = read();
  187. }
  188. return buf.toString();
  189. }
  190.  
  191. public String readLine() {
  192. String s = readLine0();
  193. while (s.trim().length() == 0)
  194. s = readLine0();
  195. return s;
  196. }
  197.  
  198. public String readLine(boolean ignoreEmptyLines) {
  199. if (ignoreEmptyLines)
  200. return readLine();
  201. else
  202. return readLine0();
  203. }
  204.  
  205. public BigInteger readBigInteger() {
  206. try {
  207. return new BigInteger(readString());
  208. } catch (NumberFormatException e) {
  209. throw new InputMismatchException();
  210. }
  211. }
  212.  
  213. public char readCharacter() {
  214. int c = read();
  215. while (isSpaceChar(c))
  216. c = read();
  217. return (char) c;
  218. }
  219.  
  220. public double readDouble() {
  221. int c = read();
  222. while (isSpaceChar(c))
  223. c = read();
  224. int sgn = 1;
  225. if (c == '-') {
  226. sgn = -1;
  227. c = read();
  228. }
  229. double res = 0;
  230. while (!isSpaceChar(c) && c != '.') {
  231. if (c == 'e' || c == 'E')
  232. return res * Math.pow(10, nextInt());
  233. if (c < '0' || c > '9')
  234. throw new InputMismatchException();
  235. res *= 10;
  236. res += c - '0';
  237. c = read();
  238. }
  239. if (c == '.') {
  240. c = read();
  241. double m = 1;
  242. while (!isSpaceChar(c)) {
  243. if (c == 'e' || c == 'E')
  244. return res * Math.pow(10, nextInt());
  245. if (c < '0' || c > '9')
  246. throw new InputMismatchException();
  247. m /= 10;
  248. res += (c - '0') * m;
  249. c = read();
  250. }
  251. }
  252. return res * sgn;
  253. }
  254.  
  255. public boolean isExhausted() {
  256. int value;
  257. while (isSpaceChar(value = peek()) && value != -1)
  258. read();
  259. return value == -1;
  260. }
  261.  
  262. public String next() {
  263. return readString();
  264. }
  265.  
  266. public SpaceCharFilter getFilter() {
  267. return filter;
  268. }
  269.  
  270. public void setFilter(SpaceCharFilter filter) {
  271. this.filter = filter;
  272. }
  273.  
  274. public interface SpaceCharFilter {
  275. public boolean isSpaceChar(int ch);
  276. }
  277. }
  278.  
  279.  
  280. }
  281.  
Success #stdin #stdout 0.09s 320320KB
stdin
5
C 1 2
Q 1 2
C 2 3
C 3 4
Q 1 2
Q 1 3
Q 3 2
Q 2 6
-1
stdout
Yes
Yes
Yes
Yes
No