Nevada112

Untitled

Jun 9th, 2021
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  // number of prime numbers less then or equal to x are  -->  x/ln(x)
  5.  public class spoj1 {
  6. static class FastScanner {
  7. InputStreamReader is;
  8. BufferedReader br;
  9. StringTokenizer st;
  10. public FastScanner() {
  11. is = new InputStreamReader(System.in);
  12. br = new BufferedReader(is);
  13. }
  14. String next() throws Exception {
  15. while (st == null || !st.hasMoreElements())
  16. st = new StringTokenizer(br.readLine());
  17. return st.nextToken();
  18. }
  19. int nextInt() throws Exception {
  20. return Integer.parseInt(next());
  21. }
  22. long nextLong() throws Exception {
  23. return Long.parseLong(next());
  24. }
  25. int[] readArray(int num) throws Exception {
  26. int arr[]=new int[num];
  27. for(int i=0;i<num;i++)
  28. arr[i]=nextInt();
  29. return arr;
  30. }
  31. String nextLine() throws Exception {
  32. return br.readLine();
  33. }
  34. }
  35.  public static boolean power_of_two(int a)
  36. {
  37.   if((a&(a-1))==0)
  38. {
  39.  return true;
  40. }
  41. return false;
  42. }
  43. static boolean PS(double x)
  44. {
  45. if (x >= 0) {
  46. double i= Math.sqrt(x);
  47. if(i%1!=0)
  48. {
  49. return false;
  50. }
  51. return ((i * i) == x);
  52. }
  53. return false;
  54. }
  55. public static int[] ia(int n)
  56. {
  57.     int ar[]=new int[n];
  58.     return ar;
  59. }
  60. public static long[] la(int n)
  61. {
  62.     long ar[]=new long[n];
  63.     return ar;
  64. }
  65. public static void print(int ans,int t)
  66. {
  67.     System.out.println("Case"+" "+"#"+t+":"+" "+ans);
  68. }
  69. static long mod=1000000007;
  70. static int max=Integer.MIN_VALUE;
  71. static int min=Integer.MAX_VALUE;
  72.  
  73. //****   M A I N      C O D E     ****
  74.  static ArrayList<Integer> tree[];
  75. public static void main(String args[]) throws java.lang.Exception
  76. {
  77. FastScanner sc=new FastScanner();
  78. int n=sc.nextInt();
  79. int q=sc.nextInt();
  80. String s=sc.next();
  81. int ar[]=new int[n];
  82. for(int i=0;i<n;i++)
  83. {
  84.     ar[i]=s.charAt(i)=='1'?1:0;
  85. }
  86. tree=new ArrayList[n];
  87. for(int i=0;i<n;i++){
  88.     tree[i]=new ArrayList<>();
  89. }
  90. for(int i=1;i<n;i++)
  91. {
  92.     int a=sc.nextInt();
  93.     int b=sc.nextInt();
  94.     tree[a].add(b);
  95.     tree[b].add(a);
  96. }
  97. for(int i=0;i<q;i++)
  98. {
  99.     int type=sc.nextInt();
  100.     if(type==1)
  101.     {
  102.         int src=sc.nextInt();
  103.         int des=sc.nextInt();
  104.        System.out.println(find(ar,-1,src,des)==true?"Yes":"No");
  105.     }
  106. else{
  107.     int a=sc.nextInt();
  108.   ar[a]^=ar[a];
  109. }
  110. }
  111. }
  112.  public static boolean find(int ar[],int par,int src , int des)
  113.  {
  114.     boolean ans=false;
  115.     for(int i:tree[src])
  116.     {
  117.        if(i!=par)
  118.        {
  119.            if(ar[i]==ar[src])
  120.            {
  121.                if(i==des)
  122.                {
  123.             ans|=true;
  124.                }
  125.                ans|=find(ar,src,i,des);
  126.            }
  127. }
  128.     }
  129.     return ans;
  130. }
  131. public static  void display(int src , int par)
  132. {
  133.     System.out.println(src);
  134.     for(int i:tree[src])
  135.     {
  136. if(i!=par)
  137. {
  138.     display(i,src);
  139. }
  140.     }
  141. }
  142.  }
Advertisement
Add Comment
Please, Sign In to add comment