Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. class InputReader {
  5.  
  6. private InputStream stream;
  7. private byte[] buf = new byte[1024];
  8. private int curChar;
  9. private int numChars;
  10. private SpaceCharFilter filter;
  11.  
  12. public InputReader(InputStream stream) {
  13. this.stream = stream;
  14. }
  15.  
  16. public int read() {
  17. if (numChars == -1)
  18. throw new InputMismatchException();
  19. if (curChar >= numChars) {
  20. curChar = 0;
  21. try {
  22. numChars = stream.read(buf);
  23. } catch (IOException e) {
  24. throw new InputMismatchException();
  25. }
  26. if (numChars <= 0)
  27. return -1;
  28. }
  29. return buf[curChar++];
  30. }
  31.  
  32. public int readInt() {
  33. int c = read();
  34. while (isSpaceChar(c))
  35. c = read();
  36. int sgn = 1;
  37. if (c == '-') {
  38. sgn = -1;
  39. c = read();
  40. }
  41. int res = 0;
  42. do {
  43. if (c < '0' || c > '9')
  44. throw new InputMismatchException();
  45. res *= 10;
  46. res += c - '0';
  47. c = read();
  48. } while (!isSpaceChar(c));
  49. return res * sgn;
  50. }
  51.  
  52. public String readString() {
  53. int c = read();
  54. while (isSpaceChar(c))
  55. c = read();
  56. StringBuilder res = new StringBuilder();
  57. do {
  58. res.appendCodePoint(c);
  59. c = read();
  60. } while (!isSpaceChar(c));
  61. return res.toString();
  62. }
  63.  
  64. public boolean isSpaceChar(int c) {
  65. if (filter != null)
  66. return filter.isSpaceChar(c);
  67. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  68. }
  69.  
  70. public String next() {
  71. return readString();
  72. }
  73.  
  74. public interface SpaceCharFilter {
  75. public boolean isSpaceChar(int ch);
  76. }
  77. }
  78.  
  79. class OutputWriter {
  80. private final PrintWriter writer;
  81.  
  82. public OutputWriter(OutputStream outputStream) {
  83. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  84. }
  85.  
  86. public OutputWriter(Writer writer) {
  87. this.writer = new PrintWriter(writer);
  88. }
  89.  
  90. public void print(Object...objects) {
  91. for (int i = 0; i < objects.length; i++) {
  92. if (i != 0)
  93. writer.print(' ');
  94. writer.print(objects[i]);
  95. }
  96. }
  97.  
  98. public void printLine(Object...objects) {
  99. print(objects);
  100. writer.println();
  101. }
  102.  
  103. public void close() {
  104. writer.close();
  105. }
  106.  
  107. public void flush() {
  108. writer.flush();
  109. }
  110.  
  111. }
  112.  
  113. class IOUtils {
  114.  
  115. public static int[] readIntArray(InputReader in, int size) {
  116. int[] array = new int[size];
  117. for (int i = 0; i < size; i++)
  118. array[i] = in.readInt();
  119. return array;
  120. }
  121.  
  122. }
  123. class Solution {
  124.  
  125. static int i,j,n,k,l,m,tot, flag,h,r, K,x1,y1,x2,y2,x3,y3,mmx,mmy,x,y,z,timer,sz1,sz2;
  126. static int a[] = new int[100500];
  127. static int tin[] = new int[100500];
  128. static int tout[] = new int[100500];
  129. static int pr[] = new int[100500];
  130. static int b[] = new int[100500];
  131. static int d[] = new int[100500];
  132. static int up[][] = new int[100500][20];
  133. static int w[] = new int[100500];
  134. static int used[] = new int[100500];
  135. static int X1[] = new int[100500];
  136. static int Y1[] = new int[100500];
  137. static int X2[] = new int[100500];
  138. static int Y2[] = new int[100500];
  139. static List<List<Integer>> g=new ArrayList<>();
  140. static Map<Integer, Integer> trans = new HashMap<Integer, Integer>();
  141. static void dfs(int v, int lvl, int p)
  142. {
  143. tin[v] = timer;
  144. w[v] = lvl;
  145. up[v][0] = p;
  146. for (int i=1; i<=17; ++i)
  147. up[v][i] = up[up[v][i-1]][i-1];
  148. for(int i = 0; i < g.get(v).size(); i++)
  149. {
  150. int to = g.get(v).get(i);
  151. if (to != p)
  152. {
  153. dfs(to, lvl+1, v);
  154. pr[to] = v;
  155. }
  156. }
  157. tout[v] = timer++;
  158. }
  159. static boolean upper(int x, int y)
  160. {
  161. return tout[x] >= tout[y] && tin[x] <= tin[y];
  162. }
  163. static int lca (int a, int b) {
  164. if (upper (a, b)) return a;
  165. if (upper (b, a)) return b;
  166. for (int i=17; i>=0; --i)
  167. if (! upper (up[a][i], b))
  168. a = up[a][i];
  169. return up[a][0];
  170. }
  171. public static void main(String[] args) throws IOException{
  172. InputReader sc = new InputReader(System.in);//new File("input.txt"));
  173. OutputWriter pw = new OutputWriter(System.out);//new File("output.txt"));
  174. n = sc.readInt();
  175. m = sc.readInt();
  176. b[0] = 0;
  177. for (i = 1; i <= n; i++)
  178. {
  179. a[i] = sc.readInt();
  180. b[i] = 0;
  181. if (!trans.containsKey(a[i]))
  182. trans.put(a[i], j++);
  183. g.add(new ArrayList<>());
  184. }
  185. g.add(new ArrayList<>());
  186. for (i = 1; i <= n; i++)
  187. a[i] = trans.get(a[i]);
  188. for (i = 0; i < n-1; i++)
  189. {
  190. x = sc.readInt();
  191. y = sc.readInt();
  192. g.get(x).add(y);
  193. g.get(y).add(x);
  194. }
  195. dfs(1, 0, 1);
  196. for (i = 0; i < m; i++)
  197. {
  198. x1 = sc.readInt();
  199. y1 = sc.readInt();
  200. x2 = sc.readInt();
  201. y2 = sc.readInt();
  202. X1[i] = x1;
  203. Y1[i] = y1;
  204. X2[i] = x2;
  205. Y2[i] = y2;
  206.  
  207. int lc1 = lca(x1,y1);
  208. int lc2 = lca(x2,y2);
  209. int ans1 = 0;
  210. if (lc1 == lc2)
  211. {
  212.  
  213. int lc3 = lca(x1,x2);
  214. int lc4 = lca(x1,y2);
  215. int lc5 = lca(y1,x2);
  216. int lc6 = lca(y1,y2);
  217. ans1++;
  218. ans1 += w[lc3]-w[lc1];
  219. ans1 += w[lc4]-w[lc1];
  220. ans1 += w[lc5]-w[lc1];
  221. ans1 += w[lc6]-w[lc1];
  222. } else
  223. if (w[lc1] < w[lc2])
  224. {
  225.  
  226. int lc3 = lca(x1,x2);
  227. int lc4 = lca(x1,y2);
  228. int lc5 = lca(y1,x2);
  229. int lc6 = lca(y1,y2);
  230. if (upper(lc2,x1) && upper(lc1,lc2))
  231. {
  232. ans1 += Math.abs(w[lc3]-w[lc4])+1;
  233. }
  234. if (upper(lc2,y1) && upper(lc1,lc2))
  235. {
  236. ans1 += Math.abs(w[lc5]-w[lc6])+1;
  237. }
  238. } else
  239. if (w[lc1] > w[lc2])
  240. {
  241.  
  242. int lc3 = lca(x1,x2);
  243. int lc4 = lca(x1,y2);
  244. int lc5 = lca(y1,x2);
  245. int lc6 = lca(y1,y2);
  246. if (upper(lc1,x2) && upper(lc2,lc1))
  247. {
  248. ans1 += Math.abs(w[lc3]-w[lc5])+1;
  249. }
  250. if (upper(lc1,y2) && upper(lc2,lc1))
  251. {
  252. ans1 += Math.abs(w[lc4]-w[lc6])+1;
  253. }
  254. }
  255. d[i] = ans1;
  256. /*if (w[x1]+w[y1]-w[lc1]>w[x2]+w[y2]-w[lc2])
  257. {
  258. x3 = x1; x1 = x2; x2 = x3;
  259. y3 = y1; y1 = y2; y2 = y3;
  260. x3 = lc1; lc1 = lc2; lc2 = x3;
  261. }*/
  262. }
  263. for (i = 0; i < m; i++)
  264. {
  265. x1 = X1[i];
  266. y1 = Y1[i];
  267. x2 = X2[i];
  268. y2 = Y2[i];
  269. int ans = 0;
  270. x3 = x1; y3 = y1;
  271. while (!upper(x1,y1))
  272. {
  273. ++b[a[x1]];
  274. x1 = pr[x1];
  275. }
  276. while (y1 != x1)
  277. {
  278. ++b[a[y1]];
  279. y1 = pr[y1];
  280. }
  281. b[a[x1]]++;
  282.  
  283. while (!upper(x2,y2))
  284. {
  285. ans+=b[a[x2]];
  286. x2 = pr[x2];
  287. }
  288. while (y2 != x2)
  289. {
  290. ans+=b[a[y2]];
  291. y2 = pr[y2];
  292. }
  293. ans+=b[a[x2]];
  294.  
  295. int tmp = x1;
  296. x1 = x3; y1 = y3;
  297. while (x1 != tmp)
  298. {
  299. b[a[x1]] = 0;
  300. x1 = pr[x1];
  301. }
  302. while (y1 != tmp)
  303. {
  304. b[a[y1]] = 0;
  305. y1 = pr[y1];
  306. }
  307. b[a[x1]] = 0;
  308. pw.printLine(ans-d[i]);
  309. }
  310. pw.close();
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement