Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.lang.*;
- import java.util.*;
- // number of prime numbers less then or equal to x are --> x/ln(x)
- public class spoj1 {
- static class FastScanner {
- InputStreamReader is;
- BufferedReader br;
- StringTokenizer st;
- public FastScanner() {
- is = new InputStreamReader(System.in);
- br = new BufferedReader(is);
- }
- String next() throws Exception {
- while (st == null || !st.hasMoreElements())
- st = new StringTokenizer(br.readLine());
- return st.nextToken();
- }
- int nextInt() throws Exception {
- return Integer.parseInt(next());
- }
- long nextLong() throws Exception {
- return Long.parseLong(next());
- }
- int[] readArray(int num) throws Exception {
- int arr[]=new int[num];
- for(int i=0;i<num;i++)
- arr[i]=nextInt();
- return arr;
- }
- String nextLine() throws Exception {
- return br.readLine();
- }
- }
- public static boolean power_of_two(int a)
- {
- if((a&(a-1))==0)
- {
- return true;
- }
- return false;
- }
- static boolean PS(double x)
- {
- if (x >= 0) {
- double i= Math.sqrt(x);
- if(i%1!=0)
- {
- return false;
- }
- return ((i * i) == x);
- }
- return false;
- }
- public static int[] ia(int n)
- {
- int ar[]=new int[n];
- return ar;
- }
- public static long[] la(int n)
- {
- long ar[]=new long[n];
- return ar;
- }
- public static void print(int ans,int t)
- {
- System.out.println("Case"+" "+"#"+t+":"+" "+ans);
- }
- static long mod=1000000007;
- static int max=Integer.MIN_VALUE;
- static int min=Integer.MAX_VALUE;
- //**** M A I N C O D E ****
- static ArrayList<Integer> tree[];
- public static void main(String args[]) throws java.lang.Exception
- {
- FastScanner sc=new FastScanner();
- int n=sc.nextInt();
- int q=sc.nextInt();
- String s=sc.next();
- int ar[]=new int[n];
- for(int i=0;i<n;i++)
- {
- ar[i]=s.charAt(i)=='1'?1:0;
- }
- tree=new ArrayList[n];
- for(int i=0;i<n;i++){
- tree[i]=new ArrayList<>();
- }
- for(int i=1;i<n;i++)
- {
- int a=sc.nextInt();
- int b=sc.nextInt();
- tree[a].add(b);
- tree[b].add(a);
- }
- for(int i=0;i<q;i++)
- {
- int type=sc.nextInt();
- if(type==1)
- {
- int src=sc.nextInt();
- int des=sc.nextInt();
- System.out.println(find(ar,-1,src,des)==true?"Yes":"No");
- }
- else{
- int a=sc.nextInt();
- ar[a]^=ar[a];
- }
- }
- }
- public static boolean find(int ar[],int par,int src , int des)
- {
- boolean ans=false;
- for(int i:tree[src])
- {
- if(i!=par)
- {
- if(ar[i]==ar[src])
- {
- if(i==des)
- {
- ans|=true;
- }
- ans|=find(ar,src,i,des);
- }
- }
- }
- return ans;
- }
- public static void display(int src , int par)
- {
- System.out.println(src);
- for(int i:tree[src])
- {
- if(i!=par)
- {
- display(i,src);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment