Advertisement
coder0687

Untitled

May 13th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import static java.lang.System.*;
  4. import static java.lang.Math.*;
  5. /*
  6. * Hackerearth Question: Tree Query
  7. * Problem Link: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/tree-query-3-5d98588f/
  8. */
  9. // Time Complexity: For query=> O(log(n)), For update=> O(1)
  10. public class Tree_Query
  11. {
  12.  
  13.     List<Integer>[] tree;
  14.     int[] A,ST,tin,tout,parent;
  15.     int timer=0;
  16.     void solve() {
  17.         int tc=1;
  18.         StringBuilder sb = new StringBuilder();
  19.         while(tc-->0) {
  20.             int n=ni(),q=ni();
  21.             tree=new ArrayList[n+1];
  22.             A=new int[n+1];
  23.             parent=new int[n+1];
  24.             Arrays.fill(A,1);
  25.             tree[1]=new ArrayList<>();
  26.             for(int i=0;i<n-1;i++) {
  27.                 int u=ni(),v=ni();
  28.                 if(tree[u]==null) tree[u]=new ArrayList<>();
  29.                 if(tree[v]==null) tree[v]=new ArrayList<>();
  30.                 tree[u].add(v);
  31.                 tree[v].add(u);
  32.             }
  33.  
  34.             d_f_s(1,0);
  35.  
  36.             int v;
  37.             while(q-->0) {
  38.                 if(ni()==1) { v=ni(); A[v]^=1; }
  39.                 else {
  40.                     dfs(v=ni(),parent[v]);
  41.                     sb.append(value).append("\n");
  42.                     value=0;
  43.                 }
  44.             }
  45.         }
  46.         out.println(sb);
  47.         out.flush();
  48.         out.close();
  49.     }
  50.     int value=0;
  51.     void d_f_s(int cur, int par) {
  52.         if(v[cur]) return; else v[cur]=true;
  53.         parent[cur]=par;
  54.         for(int child:tree[cur])
  55.             if(child!=par) d_f_s(child,cur);
  56.     }    
  57.     void dfs(int cur, int par) {
  58.         if(A[cur]!=0) value++; else return;
  59.         for(int child:tree[cur])
  60.             if(child!=par)
  61.                 dfs(child,cur);
  62.     }
  63.  
  64.     public static void main(String[] args)throws Exception {
  65.             new Thread(null, new Tree_Query("ONLINE_JUDGE")::solve, "1", 1 << 26).start();
  66.     }
  67.  
  68.     FastReader sc=null;PrintWriter out = null;
  69.     public Tree_Query()throws Exception {
  70.         sc = new FastReader(new FileInputStream("input.txt"));
  71.         out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
  72.     }
  73.     public Tree_Query(String JUDGE) {
  74.         sc = new FastReader(System.in);
  75.         out = new PrintWriter(System.out);      
  76.     }
  77.    
  78.     String ns() { return sc.next(); }
  79.     int ni() { return sc.nextInt(); }
  80.     long nl() { return sc.nextLong(); }
  81.     int[] ni(int n) {
  82.         int a[]=new int[n];
  83.         for(int i=0;i<n;a[i++]=ni());
  84.         return a;
  85.     }
  86.     long[] nl(int n) {
  87.         long a[]=new long[n];
  88.         for(int i=0;i<n;a[i++]=nl());
  89.         return a;
  90.     }
  91.    
  92.     int[][] ni(int n,int m) {
  93.         int a[][]=new int[n][m];
  94.         for(int i=0;i<n;i++)
  95.             for(int j=0;j<m;j++)
  96.                 a[i][j]=ni();
  97.         return a;
  98.     }
  99.     long[][] nl(int n,int m) {
  100.         long a[][]=new long[n][m];
  101.         for(int i=0;i<n;i++)
  102.             for(int j=0;j<m;j++)
  103.                 a[i][j]=nl();
  104.         return a;
  105.     }
  106.     static class FastReader {
  107.         private InputStream stream;
  108.         private byte[] buf = new byte[1024];
  109.         private int curChar;
  110.         private int numChars;
  111.         private FastReader.SpaceCharFilter filter;
  112.         public FastReader(InputStream stream) {
  113.             this.stream = stream;
  114.         }
  115.  
  116.         public int read() {
  117.             if (numChars == -1) throw new InputMismatchException();
  118.             if (curChar >= numChars) {
  119.                 curChar = 0;
  120.                 try {
  121.                     numChars = stream.read(buf);
  122.                 } catch (IOException e) {
  123.                     throw new InputMismatchException();
  124.                 }
  125.                 if (numChars <= 0) return -1;
  126.             }
  127.             return buf[curChar++];
  128.         }
  129.  
  130.         public int nextInt() {
  131.             int c = read();
  132.             while (isSpaceChar(c)) c = read();
  133.             int sgn = 1;
  134.             if (c == '-') {
  135.                 sgn = -1;
  136.                 c = read();
  137.             }
  138.             int res = 0;
  139.             do {
  140.                 if (c < '0' || c > '9') throw new InputMismatchException();
  141.                 res *= 10;
  142.                 res += c - '0';
  143.                 c = read();
  144.             }
  145.             while (!isSpaceChar(c));
  146.             return res * sgn;
  147.         }
  148.        
  149.         public long nextLong() {
  150.             int c = read();
  151.             while (isSpaceChar(c)) c = read();
  152.             int sgn = 1;
  153.             if (c == '-') {
  154.                 sgn = -1;
  155.                 c = read();
  156.             }
  157.             long res = 0;
  158.             do {
  159.                 if (c < '0' || c > '9') throw new InputMismatchException();
  160.                 res = res*1L*10;
  161.                 res += c - '0';
  162.                 c = read();
  163.             }
  164.             while (!isSpaceChar(c));
  165.             return res *1L* sgn;
  166.         }
  167.        
  168.         public String next() {
  169.             int c = read();
  170.             while (isSpaceChar(c)) c = read();
  171.             StringBuilder res = new StringBuilder();
  172.             do {
  173.                 res.appendCodePoint(c);
  174.                 c = read();
  175.             } while (!isSpaceChar(c));
  176.             return res.toString();
  177.         }
  178.  
  179.         public boolean isSpaceChar(int c) {
  180.             if (filter != null) return filter.isSpaceChar(c);
  181.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  182.         }
  183.  
  184.         public interface SpaceCharFilter {
  185.             public boolean isSpaceChar(int ch);
  186.  
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement